Sunday, April 12, 2009

Learn PIC Assembly Language Part - 4

Assembly language program for Addition and Subtraction

Now you have studied the instruction set. Our next task is to write some simple programs based these instructions you have learned.

For doing majority of operations, we need to use the working register WREG of the PIC microcontroller. If you want to add two numbers, one operand should be in WREG and the other in any register file. So WREG is an intermediate register for data processing, which is available to the user.

Now I will teach you how to write a program for simple addition and subtraction and viewing the output using simulator MPLAB SIM.

Program2.asm  ADDITION

list p = 18F452

#include < P18F452.INC >

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x03

MOVWF 0x00,0

MOVLW 0x02

ADDWF 0x00,0,0

MOVWF 0x01,0

main :

goto main

end

1. list p = 18F452 tells the assembler what chip we are using.

2. #include < P18F452 >  gives microcontroller specific header file

3. org 00000 h  This is where the control goes on power on reset

4. goto Start  But we are writing our program from location 0002A h, so we use a goto instruction to go to that location.

5. Start :

1 MOVLW 0x03  Loads Working register WREG with hex value 0x03

2 MOVWF 0x00,0  Moving the contents of WREG , ie 0x03 to file register location 0x00 in the data memory.

3 MOVLW 0x02  Loads Working register WREG with hex value 0x02

4 ADDWF 0x00,0,0  Adding the contents of WREG and the contents of the register file location 0x00. i.e 0x03 + 0x02 = 0x05

5 Now the result will be in WREG. Next step is to move the value of WREG into another location in regitser file.

6 MOVWF 0x01,0  This moves the result 0x05 into register file location 0x01

7 main : goto main  This is an infinite loop. Your programs wait here

8 end  Assembler directive, which tells the program end here

Then ‘Build All’ your program using the ‘Build All’ icon or go to project and click “ Build All”. If ‘Build All icon’ is not there in the tool bar, then go to View  Tootlbar  Standard

Also select debuger as MPLAB SIM, for that go Project  Debugger  Select Memory Tool  MPLAB SIM.

On building the project if its succeeded, then you will get a new window with your program in it along with a thick green arrow and cursor blinking by it side.
Then go to ‘Debug’ tool bar. If debug tool bar is not there, then go to View  Toolbar Debug and check whather its ticked .

For viewing the output, go to View  File Register.

Then new window will appear by name File Register :1, which contains rows and columns in a matrix format. Row represents the Most Significant part of the address and column represents Least Significant part of the address. For finding the value 0x01, look on the row 0x000 and column 0x01.

The adjust the window in such a way that you can see both the file register :1 and your program simultaneosuly.Now you have to ‘Build All’ your project. Then click ‘Step Over’ {} on the debug window. You can see the green arow moving down. On clicking ‘Step Over’ for the first time, you can see the green arrow moving to the OPCODE MOVLW 0x03.

Again clicking ‘Step Over’ moves green arrow to the OPCODE MOVWF 0x00,0. Again clicking ‘Step Over’ for the third time, it has executed 2 opcodes and you can see value 0x03 in the memory location 0x01. You can see the value 0x01 in red colour.
Now click ‘Step Over’ for 3 times. Then you can see the result 0x05 at the memory location 0x01. You can see the value in red colour.

Now you have studied, how to write a simple program for addition in assembly language and using the simulator MPLAB SIM to see the output.
Before going further, I will write this program in another way...

Program3.asm  ADDITION

list p = 18F452

#include < P18F452.INC >

MEM1 equ 0x00

MEM2 equ 0x01

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x03

MOVWF MEM1,0

MOVLW 0x02

ADDWF MEM1,0,0

MOVWF MEM2,0

main :

goto main

end

The difference between program1 and program2 is that we have used assmebler directive in the latter case.

I have added two new statements in the second program program2.asm
MEM1 equ 0x00
MEM2 equ 0x01

This associates memory location 0x00 with the name MEM1 and 0x01 with name MEM2. This gives more readabilty to your program.

Similary we can write assembly language program for multiplication, subtraction and division.

Program4.asm  SUBTRACTION

list p = 18F452

#include < P18F452.INC >

MEM1 equ 0x00

MEM2 equ 0x01

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x03 ; WREG  0x03

MOVWF MEM1,0 ; MEM1  WREG

MOVLW 0x02 ; WREG  0x02

SUBWF MEM1,0,0 ; WREG = Contents( MEM1) – WREG

MOVWF MEM2,0 ; WREG  MEM2

main :

goto main

end

For seeing the output, follows the previous steps what I mentioned. You can write your comments after putting a semicolon.

Program5.asm  MULTIPLICATION

list p = 18F452

#include < P18F452.INC >

MEM1 equ 0x00

MEM2 equ 0x01

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x03 ; WREG  0x03

MOVWF MEM1,0 ; MEM1  WREG

MOVLW 0x02 ; WREG  0x02

MULWF MEM1,0 ; PROD( PRODH : PRODL ) = WREG * MEM1

main :

goto main

end

Explanation :

The multiplication program is little different from addition and subtraction. The result of multiplication is not stored in working register WREG. Becasue when you multiply two 8- bit numbers, the result will be 16 – bit, if there is no overflow.
So the result is availabe in two special function resgiters PRODH and PRODL, which contain MSB and LSB of the result. You can watch both MSB and LSB simulatenosuly in PROD resgister. There regitsers are different from register files. These are grouped under special function registers (SFRs).

For viewing PRODH and PRODL, go to View  Special Function Register . Now a new window will appear by the name ‘Special Function Resgiter’. Move down the scroll bar and locate PROD, PRODH and PRODL.

Then place the SFR window and your program side by side and do ‘Skip Over’ your program after building it. After the last instruction, you can see the value, 0x06 in the PRODL register.

i.e 0x03 * 0x02 = 0x06

Please note that all arithmetics are done with hex value only.

I hope that now you have understood something about PIC programming in assembly. In my next article, I will explain you about testing your assembly language program in a microcontroller, rather than using the simulator.

No comments: