You are on page 1of 36

MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING

Part 3 Arithmetic And Logic Instruction

Describe Arithmetic Instruction and Operation


PIC supports only two arithmetic instructions- addition and subtraction. Flags C, DC, Z are automatically set depending on the results of addition or subtraction. The only exception is the flag C. Since subtraction is performed as addition with negative value, the flag C is inverted after subtraction.

Can perform operations on W, F and 8-bit literals. Save the result in W or F. In general, affect all flags

a) Addition and subtraction


In order to add numbers together in the PIC, the WREG register must be involved. One form of the ADD instruction is ADDLW K ;WREG = WREG + K The sum is stored in the WREG register. The instruction could change any of the C, ~C, Z, N, or OV bits of the status register, depending on the operands involved.

Assume that file register RAM locations 40-43H have the following hex values. Write a program to find the sum of the values. At the end of the program, location 6 of the file register should contain the low byte and location 7 the high byte of the sum. 40 = (7D) 41 = (EB) 42 = (C5) 43 = (5B)

Solution:

After the execution of SUB, if N = 0 (or C = I), the result is positive; if N = I (or C = 0), the result is negative and the destination has the 2's complement of the result. Normally, the result is left in 2's complement, but the NEGF (negate, which is 2's complement) instruction can be used to change it.

After SUBWF, we have N = I (or C = 0), and the result is negative, in 2's complement. Then it falls through and NEGF will be executed. The NEGF instruction will take the 2's complement, and we have MYREG = 22H.

After the SUBWF, loc 6 has = 62H - 96H = CCH and the carry flag is set to 0, indicating there is a borrow (notice, N = I). Because C = 0, when SUBWFB is executed the fileReg location 7 has = 27H - 12H - I = 14H. Therefore, we have 2762H - 1296H = 14CCH. In the PlCI8, ifC = 0, the result is negative. That is the reason in subtract with borrow we have F = F - W - b. Use the MPLAB simulator to gain additional insight into this important issue.

b) Multiplication and Division


The PIC supports byte-by-byte multiplication only. The bytes are assumed to be unsigned data. The syntax is as follows: MULLW K ;W x K and 16-bit is result is in PRODH:PRODL

In byte-by-byte multiplication, one of the operands must be in the WREG register, and the second operand must be a literal K value. After multiplication, the result is in the special function registers PRODH and PRODL; the lower byte is in PRODL, and the upper byte is in PRODH.

There is no single instruction for the division of bytelbyte numbers in the PIC 18. We can write a program to perform division by repeated subtraction. In dividing a byte by a byte, the numerator is placed in a fileReg and the denominator is subtracted from it repeatedly.

The quotient is the number of times we subtracted and the remainder is in fileReg upon completion. See the following example.

Assume that file register location Oxl5 has value FD (hex). Write a program to convert it to decimal. Save the digits in locations Ox22, Ox23, and Ox24, where the least-significant digit is in Ox22

c) Addition of BCD data


BCD stands for binary coded decimal. BCD is needed because in everyday life we use the digits 0 to 9 for numbers, not binary or hex numbers. Binary representation of 0 to 9 is called BCD . In computer literature, one encounters two terms for BCD numbers: (i) unpacked BCD, and (ii) packed BCD.

In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest of the bits are O. Example: "0000 100 I" and "0000 0101" are unpacked BCD for 9 and 5, respectively. Unpacked BCD requires I byte of memory, or an S-bit register, to contain it. In packed BCD, a single byte has two BCD numbers in it: one in the lower 4 bits, and one in the upper 4 bits. For example, "0101 1001" is packed BCD for 59H. Only I byte of memory is needed to store the packed BCD operands. One reason to use packed BCD is that it is twice as efficient in storing data.

There is a problem with adding BCD numbers, which must be corrected. The problem is that after adding packed BCD numbers, the result is no longerBCD. Look at the following. MOVLW Ox17 ADDLW Ox28 Adding these two numbers gives 0011 IIII B (3FH), which is not BCD. A BCD number can only have digits from 0000 to 1001 (or 0 to 9).

The result above should have been 17 + 28 = 45 (01000101). To correct this problem, the programmer must add 6 (0 II 0) to the low digit: 3F + 06 = 45H. The same problem could have happened in the upper digit (for example, in 52H + 87H = D9H). Again, 6 must be added to the upper digit (D9H + 60H = 139H) to ensure that the result is BCD (52 + 87 = 139).

This problem is so pervasive that most microprocessors such as the PIC 18 have an instruction to deal with it. In the PIC 18 instruction "DAW" is designed to correct the BCD addition problem.

The DAW (decimal adjust WREG) instruction in the PIC 18 is provided to correct the aforementioned problem associated with BCD addition. The mnemonic "DAW" works only with an operand in the WREG register. The DAW instruction will add 6 to the lower nibble or higher nibble if needed; otherwise, it will leave the result alone. The following example will clarify these points.

After the program is executed, register WREG will contain 72H (47 + 25 = 72). Note that the "DAW" instruction works only on WREG. If the lower nibble (4 bits) is greater than 9, or if DC = I, add 0110 to the lower 4 bits. If the upper nibble is greater than 9, or ifC = I, add 0110 to the upper 4 bits.

Assume that 5 BCD data items are stored in RAM locations starting at 40H, as shown below. Write a program to find the sum of all the numbers. The result must be in BCD.
40 = (71) 41 = (88) 42 = (69) 43 = (97 )

After this code executes, fileReg location 6 = (03), and WREG = 25 because 71 + 88 + 69 + 97 = 325H. We can use the register indirect addressing mode and looping to do this program much more efficiently.

Logic Instruction
Apart from I/O and arithmetic instructions, logic instructions are some of most widely used instructions. Boolean logic instructions such as AND, OR, Exclusive-OR (XOR), and complement. Logical operations are useful for looking for array elements with certain properties (e.g., divisible by power of 2) and manipulating I/O pin values (e.g., set certain pins to high, clear a few pins, toggle a few signals, and so on).

Bit Manipulation

Example

Serializing data
Serializing data is a way of sending a byte of data one bit at a time through a single pin of the microcontroller. There are two ways to transfer a byte of data serially: 1. Using the serial port. In using the serial port, programmers have very limited control over the sequence of data transfer. 2. transfer data one bit at a time and control the sequence of data and spaces between them. In many new generations of devices such as LCD, ADC, and ROM, the serial versions are becoming popular because they take less space on a printed circuit board.

Data Serialization Example


Transfer value 41H serially (one bit at a time) via pin RBI.

End chapter 2. Please read PIC18 datasheet for detail description assembly language

You might also like