Microcontroller Operation:Register Pair Operations

Register Pair Operations

Table 2.3 shows basic operations that can be applied to pairs of registers. The result is retained in one of them, the destination register. The data to be combined with the contents of the destination register is obtained from the source register, typically W or a literal (number supplied in the instruction). The source register contents generally remains unchanged after the operation.

The meaning of each type of instruction is explained below, with examples from the PIC instruction set. As noted above, there is an option to store the result in W, the working register, if that is the source. Note also that the PIC 16 instruction set does not provide moves directly between file registers; all data moves are via W.

Status bits are modified by specific register operations. The zero flag (Z) is invariably affected by arithmetic and logic instructions, and the carry flag (C) by arithmetic ones, including rotate. The effect on the source, destination and status registers of each instruction is specified in the instruction set, and this needs to be studied thoroughly before attempting to write assembler programs. The binary, hex and assembler code is given, together with the flag(s) affected, if any, in the following examples.

PIC Microcontrollers-1134

Move

The most commonly used instruction in any program simply moves data from one register to another. It is actually a copy operation, as the data in the source register remains unchanged until overwritten.

00 1000 0000 1100 080C MOVF 0C,W (Z)

This instruction moves the contents of register 0Ch (1210) into the working register. Note that bit 7, selecting the destination, is ‘0’ for W, which has to be specified in the instruction.

00 0000 1000 1100 008C MOVWF 0C

This instruction is the reverse move, from W to register 0Ch. Bit 7 is now ‘1’, and the zero flag is not affected, even if the data is zero.

An example of the move instruction is seen at line 46 in the keypad program, MOVWF PORTC, which outputs a binary code to operate the display.

Arithmetic

Add and subtract are the basic arithmetic operations, carried out on binary numbers. Some processors also provide multiply and divide in their instruction set, but these can be created if necessary by using shift, add and subtract operations.

00 0111 1000 1100 078C ADDWF 0C (C,Z)

This instruction adds the contents of W to register 0C. The carry flag will store a carry-out of the most significant bit (MSB), if the result is greater than the maximum value, FFh (25510), with the remainder left in the register. For example, if we add the decimal numbers 200 and 100, the result will be 300. The remainder will be 300 – 256 ¼ 44, with the carry flag representing 256 (result ¼ 1 0010 1100 in binary). If the sum is exactly 25610, the register result will be zero (Z flag set) and carry-out generated (C flag set).

The carry flag is also included when subtracting, so that numbers up to 51110 can be operated on. Rotate can be used to halve and double binary numbers, while increment and decrement are also available.

Logic

Logical operations act on the corresponding pairs of bits in a literal or source register, and destination. The result is normally retained in the destination, leaving the source unchanged. The result in each bit position is obtained as if the bits had been fed through the equivalent logical gate (see Appendix B).

11 1001 0000 0001 3901 ANDLW 01 (Z)

This instruction carries out an AND operation on the corresponding pairs of bits in the binary number in W and the binary number 00000001, leaving the result in W. In this example, the result is zero if the LSB in W is zero, so it forms a check on the state of that bit alone.

This type of operation can be used for bit testing if the processor does not provide a specific instruction, or masking to select a portion of the source data. The AND operation gives a result 1 if BOTH source bits are 1. The OR operation gives the result 1 if EITHER bit is 1. XOR gives result 1 if ONE of the bits is 1. This covers all the options for logical processing.

An example of a logic instruction is seen at line 45 in the keypad program, ANDLW 0F0, which masks one of the digits for output to the display.

Leave a comment

Your email address will not be published. Required fields are marked *