5 Arithmetic and Logic v21
5 Arithmetic and Logic v21
Chapter 5
www.NicerLand.com
Objectives
• The concept of signed numbers and
2’complement
• Addition and subtraction instructions
• Carry and overflow
• Logical instruction and masking
• Compare instruction and branching
• Shift, Rotate and Data serialization
• BCD, Packed BCD and ASCII conversion.
2
Some ideas
• 100 – 34 = ?
• 99 – 34 = ?
• 100 – 34 = (99-34) + 1
• 34 – 19 = ?
• 34 +100 -19 – 100 = 34 + (99-19)+1 -100
3
• 100000000 – 00101101 = ?
• =011111111 – 00101101 + 1 = A
• 010110110 – 00101101 =
• 010110110 + A – 100000000 =
4
ADD instruction
Solution:
F5H 1111 0101
+ 0BH + 0000 1011
100H 0000 0000
After the addition, register R21 contains 00 and the flags are as follows:
C = 1 because there is a carry out from D7.
Z = 1 because the result in destination register (R21) is zero.
H = 1 because there is a carry from D3 to D4.
5
ADD instruction
Assume that RAM location 400H has the value of 33H. Write a program to find the sum
of location 400H of RAM and 55H. At the end of the program, R21 should contain the sum.
Solution:
6
ADC instructions
1
3C E7
+ 3B 8D
78 74
Write a program to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH.
Place the sum in R3 and R4; R3 should have the lower byte.
Solution:
;R2:R1 = 3B 8D
;R4:R3 = 3C E7
Notice the use of ADD for the lower byte and ADC for the higher byte.
7
SUB instructions
SUB Rd,Rr ;Rd = Rd – Rr (immediate values are not supported)
SUBI Rd,K ; Rd = Rd – K
Solution:
The flags would be set as follows: N = 0, C = 0. (Notice that there is a carry but C = 0.
We will discuss this more in the next section.) The programmer must look at the N (or
C) flag to determine if the result is positive or negative.
8
SBC instruction
SBC Rd,Rr ;Rd = Rd – Rr – C ( immediate are not supported)
SBCI Rd,Rr ;Rd = Rd – K – C
27 62 (H)
- 11 96 (H)
---------------
11 CC (H)
;R26 = (62)
;R27 = (27)
After the SUB, R26 has = 62H - 96H = CCH and the carry flag is set to 1, indicating
there is a borrow (notice, N = 1). Because C = 1, when SBC is executed R27 has
27H - 12H - 1 = 14H. Therefore, we have 2762H - 1296H = 14CCH.
9
Multiplication and Division
Multiplication:
MUL Rd,Rr ; R1:R0 = Rd × Rr (Multiply Unsigned)
MULS Rd,Rr ; R1:R0 = Rd × Rr (Multiply signed)
MULSU Rd,Rr ; R1:R0 = Rd × Rr (Multiply signed with unsigned)
Division:
.DEF NUM = R20
.DEF DENOMINATOR = R21
.DEF QUOTIENT = R22
10
Logic Instructions
AND Rd,Rr ;Rd = Rd AND Rr
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd,Rr ;Rd = 1’s Complement of Rd (11111111 – Rd)
NEG Rd,Rr ;Rd = 2’s Complement of Rd (100000000 – Rd)
Solution:
11
Setting and Clearing bits
AND Rd,Rr ;Rd = Rd AND Rr
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd,Rr ;Rd = 1’s Complement of Rd (11111111 – Rd)
NEG Rd,Rr ;Rd = 2’s Complement of Rd (100000000 – Rd)
35H 0 0 1 1 0 1 0 1 04H 0 0 0 0 0 1 0 0
AND 0FH 0 0 1 1 0 1 0 1 OR 30H 0 0 1 1 0 0 0 0
05H 0 0 0 0 0 1 0 1 34H 0 0 1 1 0 1 0 0
12
Branch and CP Instructions
13
ROR instruction
ROR Rd ;Rotate Right
In ROR, as bits are rotated from left to right, the carry flag enters the MSB
and the LSB exits to the carry flag. In other words, in ROR the C is moved to
the MSB, and the LSB is moved to the C.
14
ROL instruction
ROL Rd ;Rotate Left
In ROL, as bits are shifted from right to left, the carry flag enters the LSB and the
MSB exits to the carry flag. In other words, in ROL the C is moved to the LSB,
and the MSB is moved to the C.
SEC ;make C = 1
LDI R20,0x15 ;R20 = 0001 0101
ROL R20 ;R20 = 0010 1011 C = 0
ROL R20 ;R20 = 0101 0110 C = 0
ROL R20 ;R20 = 1010 1100 C = 0
ROL R20 ;R20 = 0101 1000 C = 1
15
LSL instruction
LSL Rd ;Logical Shift Left
In the next code you can see what happens to 00100110 after running 3 LSL
instructions.
CLC ;make C = 0
LDI R20 , 0x26 ;R20 = 0010 0110(38) c = 0
LSL R20 ;R20 = 0100 1100(74) C = 0
LSL R20 ;R20 = 1001 1000(148) C = 0
LSL R20 ;R20 = 0011 0000(98) C = 1; since C=1, content of R20
;is not multiplied by 2
16
LSR instruction
LSR Rd ;Logical Shift Right
this instruction divides content of the register by 2 and carry flag contains
the remainder of division.
In the next code you can see what happens to 0010 0110 after running 3 LSR
instructions.
17
ASR Instruction
ASR Rd ;Arithmetic Shift Right
In the next code you can see what happens to 0010 0110 after running 5 ASR
instructions.
LDI R20, 0xD0 ;R20 = 1101 0000(-48) C = 0
ASR R20 ;R20 = 1110 1000(-24) C = 0
ASR R20 ;R20 = 1111 0100(-12) C = 0
ASR R20 ;R20 = 1111 1010(-6) C = 0
ASR R20 ;R20 = 1111 1101(-3) C = 0
ASR R20 ;R20 = 1111 1110(-1) C = 1
18
BCD, Packed BCD and ASCII conversion.
••ASCII
BCD Codes
BCD Codes
Packed BCD
BCD1 BCD0
19
Packed BCD to ASCII conversion
To convert packed BCD to ASCII:
• you must first convert it to unpacked BCD.
• Then the unpacked BCD is tagged with 011 0000
(30H).
20