BIOEN 442: Microprocessor
Chapter 5 – ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
1
Week 8
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia
Today's topic
0- Course 1- Pic 2- PIC Architecture PIC
Overview /Introduction Microcontrollers:history Programming using
to Computing and features Assembly Language
5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs
17- Motor Control:
12-LCD and Keypad 13- ADC, DAC and
Relay, PWM, DC and
Interfacing sensor interfacing
Stepper Motors
2
Today’s lecture
Code addition instructions for unsigned data
Perform addition of BCD data
Using DAW command
3
ARITHMETIC INSTRUCTIONS
Unsigned numbers are the data in which all the bits are used to
represent data, and no bits are set aside for the positive or
negative sign.
The operand can be between 00 and FFH (0 to 255 decimal) for
8-bit data.
4
Addition of unsigned numbers
In PIC 18, WREG register is used to add the numbers together.
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, DC, Z, N, or OV bits of the status
register, depending on the operands involved.
The effect of the ADDLW instruction on N and OV bits are
relevantly signed number operations. (Discussed later)
5
Example 5-1
Show how the flag register is affected by the following
instructions.
6
ADDWF and addition of individual bytes
"ADDWF fileReg, d"
Allows the addition of WREG and individual
bytes residing in RAM locations of the file
register
The carry flag should be checked after the
addition of each operand.
Example 5-2 uses location 7 of the file register
to accumulate carries as the operands are
added to WREG. (see the next slide)
7
8
ADDWFC and addition of 16-bit
numbers
In adding two 16-bit data operands, the propagation of a carry from the
lower byte to the higher byte occurs.
This is called multibyte addition to distinguish it from the addition of
individual bytes.
The instruction ADDWFC (ADDW and fileReg with carry) is used
0011 1100 1110 0111
0011 1011 1000 1101
0111 1000 0111 0100
When the first byte is added, there is a carry (E7 + 8D = 74, CY = I).
The carry is propagated to the higher byte, which results in 3C + 3B + 1 =
78 (all in hex). 9
Example 5-3
Write a program to add two 16-bit numbers. The numbers are 3CE7H
and 3B8DH. Assume that fileReg location 6 = (8D) and location 7 =
(3B). Place the sum in fileReg locations 6 and 7; location 6 should
have the lower byte.
0011 1100 (3C) 1110 0111 (E7)
0011 1011 (3B) 1000 1101 (8D)
0111 1000 (78) 0111 0100 (74)
Notice the use of ADDWF for the lower byte and ADDWFC for
the higher byte. 10
BCD (binary coded decimal) number system
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 (see Figure 5-1) .
In computer literature, one encounters two
terms for BCD numbers:
(1) Unpacked BCD
(2) Packed BCD.
11
Unpacked BCD
In unpacked BCD, the lower 4 bits of the number represent the BCD
number, and the rest of the bits are 0
Example:"0000 1001" and "0000 0101" are unpacked BCD for 9
and 5, respectively. Unpacked BCD requires 1 byte of memory, or an
8-bit register, to contain it.
Packed BCD
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 1 byte of memory is needed to store the packed BCD operands
12
Problem with adding BCD numbers
The problem is that after adding packed BCD numbers, the result is no
longer BCD, and must be corrected . Look at the following,
MOVLW 0 x 17 0001 0111
ADDLW 0 x 28 0010 1000
0011 1111 (3FH) which is not BCD !
A BCD number can only have digits from 0000 to 1001 (or 0 to 9). In
other words, adding two BCD numbers must give a BCD result
The result above should have been 17 + 28 = 45 (0100 0101). To
correct this problem
The programmer must add 6 (0110) to the low digit: 3F + 06 = 45H
13
Count.....
MOVLW 0 x 17 0001 0111
ADDLW 0 x 28 0010 1000
0011 1111
0000 0110 (add 06H)
0100 0101 (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
52H 0101 0010
87H 1000 0111
1101 1001
(add 06H) 0110 0000
1 0011 1001 (139H) 14
DAW instruction
The DAW (decimal adjust WREG) instruction corrects the 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). 15
Summary of DAW action
After any instruction
1. If the lower nibble (4 bits) is greater than 9, or if DC = 1,
add 0110 (6H) to the lower 4 bits.
2. If the upper nibble is greater than 9, or if C = I, add 0110
(6H) to the upper 4 bits
In reality there is no use for the DC (auxiliary carry) flag bit
other than for BCD addition and correction
16
Examine the case of adding 55H and 77H. This will result in
CEH, which is incorrect as far as BCD is concerned
The PIC does not require the use of arithmetic instructions prior
to execution of the "DAW" instruction.
Look at the following case where no arithmetic instruction is used
(0C)
17
Example 5-4
Assume that 4 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.
18