Lec_05 Arithmetic, Logic Instructions, and Programs
Lec_05 Arithmetic, Logic Instructions, and Programs
Course
ME4162: Microprocessor AVR Microcontrollers
Semester 1, 2023 Arithmetic and Logic
Lecturer: Dr. Duong Van Lac Dr. Duong Van Lac
Department of Mechatronics, HUST
Email: [email protected]
3 4
3 4
1
10/6/2024
1
ADD Rd,Rr ;Rd = Rd + Rr ( Direct or immediate are not supported)
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.
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:
Solution:
;R2:R1 = 3B 8D
;R4:R3 = 3C E7
LDS R2,0x400 ;R2 = 33H (location 0x400 of RAM)
LDI R21,0x55 ;R21 = 55
ADD R3,R1 ;R3 = R3 + R1 = E7 + 8D = 74 and C = 1
ADD R21,R2 ;R21 = R21 + R2 = 55H + 33H = 88H, C = 0
ADC R4,R2 ;R4 = R4 + R2 + carry, adding the upper byte
;with carry from lower byte
;R4 = 3C + 3B + 1 = 78H (all in hex)
Notice the use of ADD for the lower byte and ADC for the higher byte.
7 8
7 8
2
10/6/2024
SUB Rd,Rr ;Rd = Rd – Rr (immediate values are not supported) SBC Rd,Rr ;Rd = Rd – Rr – C (immediate are not supported)
SUBI Rd,K ; Rd = Rd – K SBCI Rd,Rr ;Rd = Rd – K – C
27 62 (H)
- 11 96 (H)
Show the steps involved in the following. ---------------
11 CC (H)
LDI R20, 0x23 ;load 23H (35) into R20 ;R26) = 62 (
LDI R21, 0x3F ;load 3FH (63) into R21
;R27) = 27(
SUB R21, R20 ;R21 <- R21-R20
9 10
9 10
Prepared by Duong Van Lac. Email: [email protected]
11 12
11 12
3
10/6/2024
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 • BRVC is used to branch when oVerflow is clear to zero
• BRVS is used to branch when oVerflow is set to one
13 14
13 14
Prepared by Duong Van Lac. Email: [email protected]
In ROR, as bits are rotated from left to right, the carry flag enters the MSB
In ROL, as bits are shifted from right to left, the carry flag enters the LSB and the
and the LSB exits to the carry flag. In other words, in ROR the C is moved to
MSB exits to the carry flag. In other words, in ROL the C is moved to the LSB,
the MSB, and the LSB is moved to the C.
and the MSB is moved to the C.
15 16
15 16
4
10/6/2024
this instruction multiplies content of the register by 2 assuming that after this instruction divides content of the register by 2 and carry flag contains
LSL the carry flag is not set. the remainder of division.
In the next code you can see what happens to 00100110 after running 3 LSL In the next code you can see what happens to 0010 0110 after running 3 LSR
instructions. instructions.
CLC ;make C = 0
LDI R20 , 0x26 ;R20 = 0010 0110(38) c = 0 LDI R20,0x26 ;R20 = 0010 0110 (38)
LSL R20 ;R20 = 0100 1100(74) C = 0 LSR R20 ;R20 = 0001 0011 (19) C = 0
LSL R20 ;R20 = 1001 1000(148) C = 0 LSR R20 ;R20 = 0000 1001 (9) C = 1
LSL R20 ;R20 = 0011 0000(98) C = 1; since C=1, content of R20 LSR R20 ;R20 = 0000 0100 (4) C = 1
;is not multiplied by 2
17 18
17 18
Prepared by Duong Van Lac. Email: [email protected]
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 ASCII and BCD Codes for Digits 0–9
ASR R20 ;R20 = 1111 1110(-1) C = 1
19 20
19 20
5
10/6/2024
21
21
Prepared by Duong Van Lac. Email: [email protected]