Intro PIC18 Assembly Language
Intro PIC18 Assembly Language
Ref.: PIC Microcontroller and Embedded Systems by M. Mazidi, R. Mckinlay, D. Causey, Prentice Hall, 2008
WREG register
In the CPU, registers are used to store information temporarily. That information could be a byte of data to be processed, or an address pointing to the data to be fetched. The vast majority of PIC registers are 8-bit registers. In the PIC there is only one data type: 8-bit. The range goes from the MSB (mostsignificant bit) D7 to the LSB least-significant bit) D0. With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit chunks before it is processed. The 8-bit WREG register is the most widely used register in the PIC micro controller. WREG stands for working register, as there is only one. The WREG register is the same as the accumulator in other microprocessors. The WREG register is used for all arithmetic and logic instructions. To understand the use of the WREG register, we will show it in the context of two simple instructions: MOVE and ADD.
MOVLW Instruction
The MOVLW instruction moves 8-bit data into the WREG register. It has the following format:
MOVLW K ;move literal value K into WREG
K is an 8-bit value that can range from 0-255 in decimal, or 00-FF in hex. The L stands for literal,
which means, literally, a number must be used. In other words, if we see the word literal in any instruction, we are dealing with an actual value that must be provided right there with the instruction. This is similar to the immediate value we see in other microprocessors. Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG), which means "move a literal value to WREG, " the destination. The following instruction loads the WREG register with a literal value of 25H (i.e., 25 in hex).
MOVLW 25H ; move value 25H into WREG (WREG = 25H)
The following instruction loads the WREG register with value 87H (87 in hex).
MOVLW 87H ; load 87H into WREG (WREG = 87H)
The following instruction loads the WREG register with value 15H (15 in hex and 21 in decimal).
MOVLW 15H ; load 15H into WREG (WREG = 15H)
ADDLW Instruction
The ADDLW instruction has the following format:
ADDLW K ; ADD literal value K to WREG
The ADD instruction tells the CPU to add the literal value K to register WREG and put the result back in the WREG register. Notice that in ADDLW, first comes the letter L (literal) and then the letter W (WREG), which means "add a literal value to WREG," the destination. To add two numbers such as 25H and 34H, one can do the following:
MOVLW ADDLW 25H 34H ; load 25H into WREG ; add value 34 to W (W = W + 34H)
Executing the above lines results in WREG = 59H (25H + 34H = 59H) Figure 2-1 shows the literal value and WREG being fed to the PIC ALU.
The following program will add values 12H, 16H, 31H, and 43H: MOVLW ADDLW ADDLW ADDLW 12H 16H 11H 43H ; load value 12H into WREG (WREG = 12H) ; add 16 to WREG (WREG = 28H) ; add 11 to WREG (WREG = 39H) ; add 43 to WREG (WREG = 7CH)
When programming the WREG register of the PIC microcontroller with a literal value, the following points should be noted: Values can be loaded directly into the WREG register. There is no need for a preceding pound sign or dollar sign to indicate that a value is an immediate value as is the case with some other microcontrollers. If values 0 to F are moved into an 8-bit register such as WREG, the rest of the bits are assumed to be all zeros. For example, in "MOVLW 5H" the result will be WREG = 05H; that is, WREG = 00000101 in binary.
Moving a value larger than 255 (FF in hex) into the WREG register will truncate the upper byte and cause a warning in the .err file.
The following two points should be noted about special function registers (SFRs) addresses: 1. The special function registers have addresses between F80H and FFFH. These addresses are below FFFH, because the PIC18 starts assigning SFR addresses at FFFH and goes down until all SFRs
supported by that chip are assigned. Not all the members of the PIC18 family have the same peripherals; therefore, the number of locations used for SFRs varies among the PIC18 family. 2. Not all the address space of F80H to FFFH is used by the SFR. The unused .locations F80H to FFFH are reserved and must not be used by the PICI8 programmer.