You are on page 1of 11

Lecture 10: Arithmetic Instructions

Muhammad Saqib Bhatti

MICROPROCESSOR
AND
MICROCONTROLLE
R
SPRING 2013

Agenda

Addition of unsigned numbers


Addition of 16-bit numbers
Addition of BCD numbers
Subtraction of unsigned numbers

Adding unsigned
numbers

ADD A, operand
Addressing mode for Operand
Immediate ADD A,#25H
Register
ADD A,R3
Direct

ADD A,30H
Register Indirect ADD A,@R0

CY is set if result exceeds FF H

Lecture 10

Adding a set of unsigned bytes

Add 4 numbers
which are saved in
memory at following
addresses
Address: 40H = Data:

7D
Address: 41 = Data:
EB
Address: 42 = Data:
C5
Address: 43 = Data:
5B

MOV R0, #40H


MOV R2, #4
CLR A
MOV R7, A
AGAIN: ADD A, @R0
; A has lower byte
JNC NEXT
INC R7 ; R7 has upper
byte
NEXT: INC R0
DJNZ R2, AGAIN
Lecture 10

Adding 16-bit Unsigned numbers

ADDC A, operand
Addressing mode for
Operand
Immediate ADD A,#25H
Register

ADD A,R3
Direct ADD A,30H
Register Indirect ADD
A,@R0

Add 3CE7 and 3B8D

CLR C
MOV A, #0E7H
ADD A, #8DH
MOV R6, A
MOV A, #3CH
ADDC A, #3BH
MOV R7, A

Lecture 10

Adding BCD Numbers

Unpacked BCD
Lower 4-bits represent BCD number
Upper 4-bits all zeros
BCD 5 is 00000101
Packed BCD
Single Byte has two BCD numbers
BCD 56 is 01010110

Lecture 10

Adding BCD Numbers

Adding packed BCD numbers results in


error
BCD 56 + BCD 37 should be BCD 93
BCD 56 + BCD 37 is 8D
DA A instruction adds 6 to correct
BCD.Why 6???
8D + 6 = 93
MOV A, #56H
ADD A, #37H
DA A
Lecture 10

Subtraction of Unsigned
Numbers

SUBB A, operand A = A source CY


Addressing mode for Operand
Immediate SUBB A,#25H
Register SUBB A,R3
Direct SUBB A,30H
Register Indirect

SUBB A,@R0

SUBB instruction performs


takes 2s complement of operand
adds 2s complement to A
Inverts CY
Lecture 10

Subtraction of Unsigned
Numbers

CLR C
MOV A, #4CH
MOV R3, #6EH
SUBB A, R3
A = 0100 1100 0100 1100
R3 = 0110 1110
C 1001 0010 2s com
1101 1110
0 1101 1110
1
Lecture 10

Subtraction of Unsigned
Numbers

Subtract 1296 from 2762


CLR C
MOV A, #62H
SUBB A, #96H ; 62H 96H = CCH CY=1
MOV R7, A
MOV A, #27H
SUBB A, #12H ; 27H 12H 1 = 14H
MOV R6, A

Lecture 10

10

Special Thanks to
Dr. Waseem Ikram for the lectures

11

You might also like