-www.iamtechnical.com-

SECTION III-2: Instructions involving the WREG and the Access Bank

You cannot move literal (immediate) values directly into the general purpose RAM locations in the PIC18. They must be moved there via WREG.

There is a group of logic and arithmetic instructions that involve both the WREG and a location in the file register. The ADDWF instruction is one of them. The ADDWF instruction adds together the contents of WREG and a file register location. The file register location can be one of the special function registers SFRs or a general purpose register GPR. The destination for the result can be the WREG or the file register. The following format indicates the destination.

 

ADDWF fileReg, D

 

Where fileReg is the file register location and D indicates the destination bit. The D bit can be 0 or 1. If D = 0, it means that the destination is WREG. If D = 1, then the result will be placed in the file register.

 

The following will first put value 22H into GP RAM locations 5, 6 and 7, then add them together and put the result in WREG
MOVLW 22H ;WREG = 22H
MOVWF 5H ;move (copy) WREG contents to location 5H
MOVWF 6H ;move (copy) WREG contents to location 6H
MOVWF 7H ;move (copy) WREG contents to location 7H
ADDWF 5H, 0 ;add w and location 5, result in WREG (WREG = 22H + 22H  = 44H
ADDWF 6H, 0 ;add w and location 6, result in WREG (WREG = 44H + 22H  = 66H
ADDWF 7H, 0 ;add w and location 7, result in WREG (WREG = 66H + 22H  = 88H

 

Address Data
005 22
006 22
007 22
GPR after the execution upto "MOVWF 7H"  WREG = 22H

 

Address Data
005 22
006 22
007 22
GPR after the execution upto "ADDWF 7H, 0"  WREG = 88H

 

 

Look at the same program where the result is put into file register location 7:
MOVLW 22H ;WREG = 22H
MOVWF 5H ;move (copy) WREG contents to location 5H
MOVWF 6H ;move (copy) WREG contents to location 6H
MOVWF 7H ;move (copy) WREG contents to location 7H
ADDWF 5H, 0 ;add w and location 5, result in WREG (WREG = 22H + 22H  = 44H
ADDWF 6H, 0 ;add w and location 6, result in WREG (WREG = 44H + 22H  = 66H
ADDWF 7H, 1

;add w and location 7, result in lacation 7H

;now location 7 has 88H and WREG = 66H

 

 

Address Data
005 22
006 22
007 22
GPR after the execution upto "MOVWF 7H"  WREG = 22H

 

Address Data
005 22
006 22
007 22
GPR after the execution upto "ADDWF 7H, 1"  WREG = 66H

 

To make things less confusing as far as the D bit is concerned, the PIC assembler allows us to use the letters W or F instead of 0 or 1 to indicate the destination.

 

ADDWF fileReg, w ;add WREG and fileRef. WREG = the result
ADDWF fileReg, f ;add WREG and fileReg ;fileReg = the result

 

This format is much easier  and will help us to avoid confusion about the destination. Look at the rewrite of the last program with the new format for the ADDWF instruction.

 

Look at the same program where the result is put into file register location 7:
MOVLW 22H ;WREG = 22H
MOVWF 5H ;move (copy) WREG contents to location 5H
MOVWF 6H ;move (copy) WREG contents to location 6H
MOVWF 7H ;move (copy) WREG contents to location 7H
ADDWF 5H, W ;add w and location 5, result in WREG (WREG = 22H + 22H  = 44H
ADDWF 6H, W ;add w and location 6, result in WREG (WREG = 44H + 22H  = 66H
ADDWF 7H, F

;add w and location 7, result in lacation 7H

;now location 7 has 88H and WREG = 66H

 

The above concept is important and must be understood since there are a large number of instructions with this format.

 

State the contents of file register RAM locations 12H and WREG after the following program
MOVLW 0 ;move 0 WREG to clear it (WREG = 0)
MOVWF 12H ;move WREG to location 12 to clear it
MOVLW 22H ;load WREG with value 22H
ADDWF 12H, F

;add WREG to location 12H, location 12H = 22+0 = 22

;location 12 = sum

ADDWF 12H, F

;add WREG to location 12H, location 12H = 22+22 = 44

;location 12 = sum

ADDWF 12H, F

;add WREG to location 12H, location 12H = 22H+44 = 66

;location 12 = sum

ADDWF 12H, F

;add WREG to location 12H, location 12H = 22H+66 = 88

;location 12 = sum

 

The program clears above both the WREG and RAM location 12H in the file register. Then it loads WREG with value 22H. Then it adds the WREG register and location 12 together and saves the result in location 12H. It does that four times. At the end, location 12H of GP RAM has the value of 88H (4 X 22H = 88H) and WREG = 22H

After each "ADDWF 12, F" instruction

Address Data
011  
012 22
013  
WREG = 22H
Address Data
011  
012 44
013  
WREG = 22H
Address Data
011  
012 66
013  
WREG = 22H
 
Address Data
011  
012 88
013  
WREG = 22H

 

Rewrite the above example to place the sum in WREG as you add the file register locations and the WREG register
MOVLW 0 ;move 0 WREG to clear it (WREG = 0)
MOVWF 12H ;move WREG to location 12 to clear it
MOVLW 22H ;load WREG with value 22H
ADDWF 12H, W

;add WREG to location 12H, WREG = 22+0 = 22

;WREG = sum

ADDWF 12H, W

;add WREG to location 12H, WREG = 22+22 = 44

;WREG = sum

ADDWF 12H, W

;add WREG to location 12H, location 12H = 44H+22 = 66

;location 12 = sum

ADDWF 12H, W

;add WREG to location 12H, location 12H = 66H+22 = 88

;location 12 = sum

 

The program adds WREG and location 12H together and saves the result in WREG each time. At the end, location 12H has a value of 22H amd WREG = 88H

After each 'ADDWF 12, W" instruction:

 

Address Data
011  
012 22
013  
WREG = 22H
Address Data
011  
012 22
013  
WREG = 44H
Address Data
011  
012 22
013  
WREG = 66H
Address Data
011  
012 22
013  
WREG = 88H

 

More From Iamtechnical.com

Facebook Like For Iamtechnical.com !