8051-CH6-ArithLigic Instructns
8051-CH6-ArithLigic Instructns
8051-CH6-ArithLigic Instructns
Sections
6.1 Arithmetic instructions 6.2 Signed number concepts and arithmetic operations 6.3 Logic and compare instructions 6.4 Rotate instructions and data serialization 6.5 BCD, ASCII, and other application programs
Unsigned Numbers
Unsigned numbers are defined as data in which all the bits are used to represent data, and no bits are set aside for the positive or negative sign. For a 8-bit data, the operand can be between 00 and FFH.
MOV A,#25H ;load 25H into A ADD A,#34H ;add 34H to A, now A=59H The destination operand is always in A. The instruction could change CY,AC,OV and P.
6
Addition
ADD A, #N ADD A, Rr ADD A, add ADD A, @Rp
Example 6-1
Show how the flag register is affected by the following instructions. MOV A,#0F5H ;A=F5 hex ADD A,#0BH ;A=F5+0B=00 Solution:
F5H +0BH 100H 1111 0101 +0000 1011 0000 0000
After the addition, register A (destination) contains 00 and the flags are as follows: CY = 1 since there is a carry out from D7. P = 0 because the number of 1s is 0 (an even number), P is set to 0. AC = 1 since there is a carry from D3 to D4.
8
Example 6-2
Assume that RAM locations 40-44 have the following values. Write a program to find the sum of the values. At the end of the program, register A should contain the low byte and R7 the high byte. All values are in hex. 40=(7D) 41=(EB) 42=(C5) 43=(5B) 44=(30) Solution: MOV R0,#40H ;load pointer MOV R2,#5 ;load counter CLR A ;A=0 MOV R7,A ;clear R7 AGAIN:ADD A,@R0 ;add (R0) JNC NEXT ;jump to carry INC R7 ;keep track of carries NEXT: INC R0 ;increment pointer DJNZ R2,AGAIN ;repeat until R2 is zero
10
ADDC
ADD with carry To add two 16-bit data operands, Add the source operand to register A and put the result in A. ADDC A, source
MOV ADD MOV MOV ADDC MOV A,#E7H A,#8DH R6,A A,#3CH A,#3BH R7,A E7 8D 74 CY=1 ; A=A+operand+CY ; R7=78H R6=74H 3C + 3B 78
11
ADDC
ADDC A, #n ADDC A, add ADDC A, Rr ADDC A, @Rp
12
EXAMPLE
MOV A, #1Ch MOV R5, #0A1h ADD A, R5 ADD A,R5 ADDC A, #10h ADDC A, #10h
13
EXAMPLE
MOV A, #1Ch MOV R5, #0A1h ADD A, R5 ADD A,R5 ADDC A, #10h ADDC A, #10h A = 1Ch R5 = A1h A = BDh; C = 0, OV = 0 A = 5Eh; C=1, OV= 1 A = 6Fh; C = 0, OV=0 A = 7Fh; C=0; OV=0
14
EXAMPLE
MOV A, #1Ch MOV R5, #0A1h ADD A, R5 ADD A,R5 ADDC A, #10h ADDC A, #10h A = 1Ch R5 = A1h A = BDh; C = 0, OV = 0 A = 5Eh; C=1, OV= 1 A = 6Fh; C = 0, OV=0 A = 7Fh; C=0; OV=0
15
Example 6-3
Write a program to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH. Place that sum in R7 and R6; R6 should have the lower byte. Solution: CLR C MOV A,#0E7H ADD A,#8DH MOV R6,A MOV A,#3CH ADDC A,#3BH MOV R7,A
;make CY=0 ;load the low byte now A=E7H ;add the low byte,A=74H and CY=1 ;save the low byte in R6 ;load the high byte ;add with the carry ;save the high byte of the sum
16
SUBB
Subtraction with borrow SUBB A, source ; A = A source CY
CLR MOV MOV SUBB C A,#3FH R3,#23H A,R3
3 F - 2 3 1 C
17
18
3FH23H=1CH 1. A=A-CY=3FH 2. 23H -> DDH (2s complement) 3. 3FH+DDH=11CH, where CY=1,A=1CH. 4. Toggle & get 19 CY=0
Example 6-5
Show the steps involved in the following. CLR C ;make CY=0 MOV A,#3FH ;load 3FH into A (A=3FH) MOV R3,#23H ;load 23H into R3 (R3=23H) SUBB A,R3 ;A = A R3 Solution:
0011 1111 (A=A-CY) 1101 1101 (2s complement) 0001 1100 (after Toggle CY)
The flags would be set as follows: CY = 0, AC = 0 The programmer must look at the carry flag to determine if the result is positive or negative.
20
Example 6-6
Analyze the following program: CLR C MOV A,#4CH ;load A =4CH SUBB A,#6EH ;subtract 6E from A JNC NEXT ;jump not carry (CY=0) CPL A ;get 2s complement by INC A ; yourself NEXT:MOV R1,A ;save A in R1 Solution: 4C 0100 1100 0100 1100 - 6E 0110 1110 + 1001 0010 -22 0 1101 1110(A) CY=1, the result is negative. We borrow 100H and get positive result DEH. Get the 2s complement of A =0010 0010=22.
21
Example 6-7
Analyze the following program: CLR C MOV A,#62H 27 62 SUBB A,#96H - 12 96 MOV R7,A 14 CC MOV A,#27H SUBB A,#12H MOV R6,A Solution:
0110 0010 62H + 0110 1010 96Hs 2s 0 1100 1100 CY=1 after
toggle
27H-CY
12Hs 2s CY=0 after toggle
After the SUBB, A = 62H-96H = CCH and CY=1indicating there is a borrow. Since CY = 1, when SUBB is executed the second time A = 27H12H-1 =14H. Therefore, we have 2762H-1296H =14CCH.
22
23
MUL AB
The 8051 supports byte by byte multiplication only. Multiple A * B, result: B=high byte, A=low byte. MUL AB
MOV MOV MUL A,#25H B,#65H AB 25 65 0E 99 B=0EH: upper byte; A=99H: lower byte
24
25
DIV AB
The 8051 supports byte by byte division only. Divide A by B, result: B=remainder, A=quotient. DIV AB
MOV MOV DIV A,#95H B,#10H AB
;A=09H (quotient) ;B=05H (remainder) IF the numerator=B=0, then OV=1 indicates an error and CY=0.
26
Remainder
B
27
Example 6-8
Write a program to get hex data in the range of 00 FFH from port 1 and convert it to decimal. Save the digits in R7, R6 and R5, where the least significant digit is in R7. Solution of (a): MOV A,#0FFH MOV P1,A ;make P1 an input port MOV A,P1 ;read data from P1 MOV B,#10 ;B=0A hex (10 dec) DIV AB ;divide by 10 MOV R7,B ; MOV B,#10 ;P1 has max value 255 DIV AB ;3 bytes to save 3 decimals MOV R6,B ;Twice DIV are used MOV R5,A ;
29
Example 6-9
Analyze the program, assuming that P1 has a value of FDH for data. Solution of (b): In the case of an 8-bit binary = FDH = 253 in decimal. Q(A) R(B) FD / 0A= 19 3 (low digit R7=3) 19 / 0A= 2 5 (middle digit R6=5) (high digit R5=2)
Therefore, we have FDH = 253. In order to display this data it must be converted to ASCII, which is described in the next chapter.
30
31
32
33
7 8 F
F 6 5
The programmer must add 6 to the low digital such that a carry occurs.
If lower nibble>9 or AC=1, add 6 to lower nibble. If higher nibble>9 or CY=1, add 6 to higher nibble.
34
DA
Decimal adjust for addition DA instruction will add 6 to the lower nibble or higher nibble if needed. DA A
MOV MOV ADD DA A,#47H B,#55H A,B A 4 + 5 9 A CY=1 0 7 5 C 1. Lower nibble 2 2. Upper nibble 2
35
36
Signed Numbers
In everyday life, numbers are used that could be positive or negative. Usually, 2s complement is used for signed numbers.
37
sign
magnitude
The rest is used for the magnitude which is represented in its 2s complement.
38
Binary
1000 0000 1000 0001 : 1111 1110 1111 1111 0000 0000 0000 0001 0000 0010 : 0111 1111
Hexadecimal
80H = 100H-80H 81H = 100H-7FH FEH = 100H 2H FFH = 100H 1H 00H 2s complement 01H 02H
39
+127
7FH
Example 6-10
Show how the 8051 would represent -5.
Solution: Observe the following steps: 1. 0000 0101 5 in 8-bit binary 2. 1111 1010 invert each bit 3. 1111 1011 add 1 (which becomes FB in hex) Therefore -5 = FBH, the signed number representation in 2s complement for -5. We can get FBH=100H-05H=FBH, too.
40
Example 6-11
Show how the 8051 would represent -34H.
Solution:
Observe the following steps. 1. 0011 0100 128 in 8-bit binary 2. 1100 1011 invert each bit 3. 1100 1100 add 1 (which becomes CC in hex) Therefore -34 = CCH, the signed number representation in 2s complement for -34H. We can get FBH=100H-34H=CCH, too.
41
Example 6-12
Show how the 8051 would represent 128.
Solution:
Observe the following steps. 1. 1000 0000 128 in 8-bit binary 80H 2. 0111 1111 invert each bit 3. 1000 0000 add 1 (which becomes 80 in hex) Therefore -128 = 80H, the signed number representation in 2s complement for -128. We can get FBH=100H-80H=FBH, too.
42
Overflow Problem
The CPU understands only 0s and 1s and ignores the human convention of positive and negative numbers. The overflow flag (OV) is designed to indicate an overflow of the operations for the signed numbers. When is an overflow?
If the result of an operation on signed numbers is too large for the 8-bit register, an overflow has occurred and the programmer must be notified. The range 128 to 127 in decimal.
43
Example 6-13
Examine the following code and analyze the result. MOV A,#+96 MOV R1,#+70 ADD A,R1 Solution: 96 + 70 + 166 0110 0000 0100 0110 1010 0110 ;A=0110 0000(A=60H) ;R1=0100 0110(R1=46H) ;A=1010 0110 ;A=A6H=-90, INVALID!!
The signed value in the register A=A6H=-90 is wrong. Programmer must check it by themselves. Note: There is a carry from D6 to D7 but no carry out of D7
44
When is an Overflow?
In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs:
1. There is a carry from D6 to D7 but no carry out of D7. 2. There is a carry from D7 out but no carry from D6 to D7.
45
Example 6-14
Observe the following, noting the role of the OV flag. MOV A,#-128 MOV R4,#-2 ADD A,R4 Solution: ;A=1000 0000(A=80H) ;R1=1111 1110(R4=FEH) ;A=0111 1110(A=7EH=126)
-128 1000 0000 + -2 1111 1110 - 130 0111 1110 (CY=1,AC=0,OV=1) There is a carry from D7 out but no carry from D6 to D7. According to the CPU, there is an overflow (OV = 1). The sign number is wrong.
46
Example 6-15
Observe the following, noting the OV flag. MOV A,#-2 MOV R1,#-5 ADD A,R1 Solution: -2 + -5 - 7 1111 1110 1111 1011 1111 1001 ;A=1111 1110 (A=FEH) ;R1=1111 1011(R1=FBH) ;A=1111 1001 (A=F9H=-7,correct)
(CY=1,AC=1,OV=0)
There are carries from D7 out and from D6 to D7. According to the CPU, the result is -7, which is correct (OV = 0).
47
Programming Actions
Flags
C 0 0 1 1 OV 0 1 0 1 None Complement the sign None Complement the sign
Action
48
XRL
XRL destination-byte,source-byte
MOV XRL A,#35H A,#0FH ;0011 0101 ;0000 1111 unchanged toggled
No effect on any of the flags. XRL is often used (1) to clear a register, (2) to see if two registers have the same value or (3) to toggle bits of an X Y X XOR Y operands. 0 0 0 0 1 1 XOR 2 bits X and Y 1 0 1 1 1 0
49
ANL
ANL destination-byte,source-byte
MOV ANL A,#35H A,#0FH ;0011 0101 ;0000 1111 => A=0000 0101
No effect on any of the flags. ANL is often used to mask (set to 0) certain bits of an operands. X Y X AND Y 0 0 0 0 1 0 AND 2 bits X and Y 1 0 0 1 1 1
50
Example 6-17
Show the results of the following.
MOV ANL Solution: A,#35H A,#0FH ;A = 35H ;A = A AND 0FH (now A = 05)
0 0 1 1 0 1 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 1
51
ORL
ORL destination-byte,source-byte
MOV ORL A,#35H A,#0FH ;0011 0101 ;0000 1111 => A=0011 1111
No effect on any of the flags. ORL is often used to set certain bits of an operands to 1.
X OR 2 bits X and Y Y X OR Y
0 0 1 1
0 1 0 1
0 1 1 1
52
Example 6-19
Show the results of the following. MOV XRL Solution: A,#54H A,#78H
0 1 0 1 0 1 0 0 0 1 1 1 1 0 0 0 0 0 1 0 1 1 0 0
Example 6-20
The XRL instruction can be used to clear the contents of a register by XORing it with itself. Show how XRL A,A clears A, assuming that A = 45H. Solution:
45H 45H 00
54
Example 6-21
Read and test P1 to see whether it has the value 45H. If it does, send 99H to P2; otherwise, it stays cleared. Solution: MOV P1,#0FFH MOV P2,#00 MOV R3,#45H MOV A,P1 XRL A,R3 JNZ EXIT MOV P2,#99H EXIT: ... ;make P1 an input port ;clear P2 ;R3 = 45H ;read p1
;jump if A 0
55
56
Example 6-16
Examine the following, noting the role of OV.
MOV A,#+7 MOV R1,#+18 ADD A,R1 Solution: 7 0000 0111 + 18 0001 0010 25 0001 1001 No carry occurs. ;A=0000 0111 (A=07H) ;R1=0001 0010(R1=12H) ;A=0001 1001 (A=19H=+25,correct)
CY=0,AC=0 and 0V = 0
According to the CPU, the result is +25, which is correct (OV = 0).
57
58
59
Example 6-18
Show the results of the following. MOV A,#04 ORL A,#30H Solution: ;A = 04 ;A = 34H
04 OR 30 = 34H
60
Example 6-22
Find the 2s complement of the value 85H. Solution: MOV A,#85H 85H = 1000 0101 CPL A ;1s comp. 1s = 0111 1010 ADD A,#1 ;2s comp. + 1 0111 1011 = 7BH
61
CJNE (1/2)
Compare and Jump if Not Equal CJNE destination, source, relative address
MOV A,#55H CJNE A,#99H,NEXT ... ;do here if A=99H NEXT: ... ;jump here if A99H The compare instructions really a subtraction, except that the operands themselves remain unchanged. Flags are changed according to the execution of the SUBB instruction.
62
CJNE (2/2)
This instruction affects the carry flag only.
CJNE R5,#80,NEXT ... ;do here if R5=80 NEXT: JNC LAR ... ;do here if R5<80 LAR: ... ;do here if R5>80 Compare destination > source Table 7-1 destination < source Carry Flag CY = 0 CY = 1
63
Example 6-23
Examine the following code, then answer the following questions. (a) Will it jump to NEXT? (b) What is in A after the CJNE instruction is executed? MOV A,#55H CJNE A,#99H,NEXT ... NEXT: ...
Solution: (a) Yes, it jumps because 55H and 99H are not equal. (b) A = 55H, its original value before the comparison.
64
Example 6-24
Write code to determine if register A contains the value 99H. If so, make R1 = FFH; otherwise, make R1 = 0. Solution: MOV R1,#0 CJNE A,#99H,NEXT MOV R1,#0FFH NEXT:... OVER:... ;clear R1 ;if A99, then jump ;if A99, R1=FFH ;if A99, R1=0
65
Example 6-25
Assume that P1 is an input port connected to a temperature sensor. Write a program to read the temperature and test it for the value 75. According to the test results, place the temperature value into the registers indicated by the following. If T = 75 If T < 75 If T > 75 Solution: MOV MOV CJNE SJMP OVER: JNC MOV SJMP NEXT: MOV EXIT: ... then A = 75 then R1 = T then R2 = T
;make P1 an input port ;read P1 port ;jump if A75 ;A=75 ; ;A<75, save A R1 ; ;A>75, save A in R2
66
Example 6-26
Write a program to monitor P1 continuously for the value 63H. It should get out of the monitoring only if P1 = 63H.
Solution: MOV P1,#0FFH ;make P1 an input port HERE:MOV A,P1 ;get P1 CJNE A,#63,HERE ;keep monitoring unless ; P1=63H
67
Example 6-27
Assume internal RAM memory locations 40H 44H contain the daily temperature for five days, as shown below. Search to see if any of the values equals 65. If value 65 does exist in the table, give its location to R4; otherwise, make R4 = 0. 40H=7641H=7942H=6943H=6544H=62 Solution: MOV R4,#0 ;R4=0 MOV R0,#40H ;load pointer MOV R2,#05 ;load counter MOV A,#65 ;A=65, value searched for BACK:CJNE A,@R0,NEXT;compare RAM data with 65 MOV R4,R0 ;if 65, save address SJMP EXIT ;and exit NEXT:INC R0 ;increment pointer DJNZ R2,BACK ;keep check until count=0 68 EXIT: ...
69
RR (Rotate A Right)
RR A
A,#36H A A A A ;A=0011 ;A=0001 ;A=1000 ;A=1100 ;A=0110 0110 1011 1101 0110 0011
MOV RR RR RR RR
MSB
LSB
70
RL (Rotate A Left)
RL A
MOV RL RL RL RL A,#36H A A A A ;A=0011 ;A=0110 ;A=1101 ;A=1011 ;A=0110 0110 1100 1000 0001 0011
MSB
LSB
71
A
A,#36H A A A A ;A=0011 ;A=0001 ;A=0000 ;A=1000 ;A=1100 0110, 1011, 1101, 0110, 0011, CY=0 CY=0 CY=1 CY=1 CY=0
MSB
LSB
CY
72
A
A,#36H A A A A ;A=0011 ;A=0110 ;A=1101 ;A=1011 ;A=1001 0110, 1101, 1010, 0100, 1001, CY=1 CY=0 CY=0 CY=1 CY=1
CY
MSB
LSB
73
Serializing Data
One application of rotate operations is serializing data. Serializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller.
Way 1: using the serial port
The programmers have very limited control over the sequence of data transfer. See Chapter 10.
Serial Transfer
Sender Receiver
Parallel Transfer
Sender D0 Receiver
D7
75
Ex 6-28, 6-29
76
Example 6-28
Write a program to transfer value 41H serially (one bit at a time) via pin P2.1. Put two highs at the start and end of the data. Send the byte LSB first. Solution: MOV A,#41H SETB P2.1 ;first high at start SETB P2.1 ;second high at start MOV R5,#8 ;counter HERE: RRC A ; MOV P2.1,C ;send CY to P2.1 DJNZ R5,HERE ; SETB P2.1 ;first high at end SETB P2.1 ;second high at end
A D7 D0 CY P2.1 110100000111
1
77
Example 6-29
Write a program to bring in a byte of data serially one at a time via pin P2.7 and save it in register R2. The byte comes in with the LSB first. Solution: MOV R5,#8 ;counter HERE: MOV C,P2.7 ;bring a bit RRC A ; DJNZ R5,HERE ; MOV R2, A ;save it
01000001
1
P2.1
CY
A D7 D0
78
Single-bit Operations
Instructions that are used for single-bit operations are given in Table 8-1.
These instructions can be used for any bit. Some instructions that allow single-bit operations, but only along with the carry flag (CY).
In 8051, there are several instructions by which the CY flag can be manipulated directly.
79
Function
Set the bit (bit = 1) Clear the bit (bit = 0) Complement the bit (bit = NOT bit) Jump to target if bit = 1 (jump if bit)
JNB
JBC
bit,target
bit,target
80
81
Example 6-30
Write a program to save the status of bits P1.2 and P1.3 on RAM bit locations 6 and 7, respectively. Solution: CY is used as a bit buffer. MOV MOV MOV MOV C,P1.2 06,C C,P1.3 07,C ;save ;save ;save ;save status in RAM status in RAM of P1.2 on CY bit location 06 of P1.3 on CY bit location 07
82
Example 6-31
Assume that the bit P2.2 is used to control the outdoor light and bit P2.5 to control the light inside a building. Show how to turn on the outside light and turn off the inside one. Solution: STEB ORL MOV CLR ANL MOV C C,P2.2 P2.2,C C C,P2.5 P2.5,C ;CY=1 ;CY = P2.2 ORed with CY ;turn it on ;CY=0 ;CY=P2.5 ANDed with CY ;turn it off
83
Example 6-32
Write a program that finds the number of 1s in a given byte.
Solution:
MOV R1,#0 ;R1 keeps the number of 1s MOV R7,#8 ;counter=08 rotate 8 times MOV A,#97H ;A=10010111H AGAIN: RLC A ;rotate it through CY once JNC NEXT ;check for CY INC R1 ;if CY=1 then R1++ NEXT: DJNZ R7,AGAIN
84
SWAP A
SWAP A
;A=72H ;A=27H
before: D7 D4 D3 D0 after:
D3 D0 D7 D4
before: after:
0111 0010
0010 0111
85
Example 6-33
(a) Find the contents of register A in the following code. (b) In the absence of a SWAP instruction, how would you exchange the nibbles? Write a simple program to show the process. Solution: (a)
;A = 72H ;A = 27H
;A=0111 ;A=1110 ;A=1100 ;A=1001 ;A=0010 0010 0100 1001 0011 0111
86
88
RTC keep the time (hour, minute, second) and date (year, month, day) when the power is off. This data is provided in packed BCD. For this data to be displayed (ex: on an LCD), it must be in ASCII format. We show above instructions in the conversion of BCD and ASCII
89
91
94
switches
P1.0 P1.1 P1.2 P2
8
97
Checksum Operation
To perform the checksum operations:
1. add all the bytes, including the checksum byte 2. The result must be zero. If the result is not zero, there is some error occurred.
98
Solution:
(a) Find the checksum byte
99
25H+62H+3FH+52H+E8H=200H sum=00H Dropping the carries, we have 00H. Data is correct! (c) If the second byte 62H has been changed to 22H, show how checksum detects the error.
25H+22H+3FH+52H+E8H=1C0H sum=C0H Dropping the carries, we have C0H, which is not 00H. The ROM data is corrupted
100
101
102
104
105
108
110
Conversions
Packed BCD to ASCII
By mathematical computation Using look-up table
ASCII to packed BCD Binary to ASCII Calculate the checksum byte in ROM Perform checksum operation Checksum program in module
111
Homework
Problems 1,3,4,11,13,15,26,27,32,33,36,37,40,42,46 Note:
In the problem, please find the CY, AC and OV flags for each the following. You can do it by the simulation tools. Please write and compile the programs. Print the list files and paste them in your homework.
114