Arithmetic Instructions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

ARITHMETIC

INSTRUCTIONS
ADDITION

ADD A, <scr-byte> ; Add scr-byte to ‘A’


ADD A, #data
ADDC A, <scr-byte> ; Add with carry
ADDC A, #data

“scr-byte” can be Rn, Direct, Immediate data and


@Ri where n= 0,1….7 and i= 0 or 1
ADDITION OF TWO 8-BIT NUMBERS-
EXAMPLE

A program to add two numbers (56H and 4DH)

MOV A, #56H ; load 1st number in A


MOV R0, #4DH ; load 2nd number in R0
ADD A, R0 ; add two numbers

What will be the content of A and carry flag CY after


execution of last instruction
A= 0A3h CY = 0
ADDITION OF TWO 8-BIT NUMBERS-
EXAMPLE
A program to add two numbers stored at memory
location 40h & 41h

MOV A, 40H ; load 1st number in A


MOV R0, 41H ; load 2nd number in R0
ADD A, R0 ; add two numbers
ADDITION OF TWO 8-BIT NUMBERS-
EXERCISE
1. Write a program to add two numbers 39h and 4fh
and store the result on memory location 40h

2. Write a program to read port 1 and port 2, add


these two numbers and display the result on port
0
3. Suppose dip-switches are connected to port 1 & 2
and LEDs are connected to port 0, design proteus
simulation for question # 2.
ADDITION OF 16-BIT NUMBERS- EXAMPLE
A program to add two 16-bit numbers (3CE7H, 3B8DH).

CLR C ; make CY=0


MOV A, #0E7H ; load the low byte now A=E7H
ADD A, #8DH ; add the low byte now A=74H
; and CY=1
MOV R6, A ; save the low byte of the sum in
; R6
MOV A, #3CH ; load the high byte
ADDC A, #3BH ; add with the carry
; 3B + 3C + 1 = 78 (all in hex)
MOV R7, A ; save the high byte of the sum

Answer = CY-R7-R6 = 0-78-74 --- 7874h


ADDITION- EXERCISE
Write a program to add two 24-bit numbers 897F9AH to
34BC48H and save the result in RAM memory location
starting at 40H. Also display the result on port P0, P1 and
P2.
ORG 0 MOV P1, A
MOV A, #9AH MOV A, #89H
ADD A, #48H ADDC A, #34H
MOV 40h, A MOV 42h, A
MOV P0, A MOV P2, A
MOV A, #7FH SJMP $
ADDC A, #0BCH END
MOV 41H, A
SUBTRACTION

SUBB A, <scr-byte> ; subtract from A scr-byte


; with borrow
SUBB A, #data

“scr-byte” can be Rn, Direct, Immediate data and


@Ri where n= 0,1….7 and i= 0 or 1
SUBTRACTION-EXAMPLE

CLR C ; CY=0
MOV A, #62H ; A=62H
SUBB A, #96H ; 62H-96H=CCH with
; CY=1
MOV R7, A ; save the result
MOV A, #27H ; A=27H
SUBB A, #12H ; 27H-12H-1=14H
MOV R6, A ; save the result
SUBTRACTION-EXERCISE

If CY=1, A=95H and B=4FH prior to the execution


of “SUBB A, B”, what will be the contents of A
and B after the subtraction?

A=
B=
MULTIPLICATION & DIVISION

MUL AB ; multiply A&B

;Result: A has low byte and B has high byte

DIV AB ; divide A by B

;Result: A has quotient and B has remainder


MULTIPLICATION & DIVISION-EXAMPLE
The following program will get a byte of hex data in
the range of 00- FFH from P1 and convert it to
decimal. Save the digits in R7, R6 and R5, where
the least significant digit is in R7.

MOV A, P1 ; read data from P1


MOV B, #10 ; B=0A hex (10 dec)
DIV AB ; divide by 10
MOV R7, B ; save lower digit
MOV B, #10
DIV AB ; divide by 10 once more
MOV R6, B ; save the next digit
MOV R5, A ; save the last digit
MULTIPLICATION & DIVISION-EXERCISE

Perform 77 x 34 and save the Perform 77 / 3 and save the


result in R6 and R7 result in R6 and R7

ORG 0 ORG 0
MOV A, #77 MOV A, #77
MOV B, #34 MOV B, #3
MUL AB DIV AB
MOV R6, A MOV R6, A
MOV R7, B MOV R7, B
SJMP $ SJMP $
END END
LOGICAL & COMPARE
INSTRUCTIONS
LOGICAL OPERATIONS
ANL <dest-byte>, <scr-byte> ; Logical AND for byte
ANL A, Rn ; where n= 0,1,…..,7
ANL A, direct
ANL A, @Ri ; here i = 0 or 1
ANL A, #data
ANL direct, A
ANL direct, #data

ANL C, bit ; Logical AND for bit


ANL C, /bit
LOGICAL OPERATIONS
ORL <dest-byte>, <scr-byte> ; Logical OR for byte
ORL A, Rn
ORL A, #data
ORL direct, A

ORL C, <scr-bit> ; Logical OR for bit


ORL C, bit
ORL C, /bit
LOGICAL OPERATIONS
CPL A : Logical NOT for byte
MOV A, #55h
CPL A ; A = 0AAh

CPL bit ; Logical NOT for bit


CLR C ;C=0
CPL C ;C=1
LOGICAL OPERATIONS
XRL <dest-byte>, <scr-byte> ; Logical X-OR
XRL A, Rn
XRL direct, #data

MOV A, #55h
XRL A, #0FFh ; A = 0AAh
XRL A, #0Fh ; A = 0A5h
LOGICAL OPERATIONS-EXERCISES
MOV A, #44h
ANL A, #0FFh ;A= 44h
ANL A, #0Fh ;A= 04h
ORL A, #40h ;A= 44h
ORL A, #0 ;A= 44h
CPL A ;A= BBh
XRL A, #33h ;A= 88h
XRL A, #77h ;A= FFh
ROTATE INSTRUCTION

RR A ; Rotate right
RRC A ; rotate right through carry
RL A ; Rotate left
RLC A ; rotate left through carry
ROTATE INSTRUCTION-EXAMPLE
The following program will find the number of 1s in a
given byte

MOV R1, #0 ; R1 keeps the number of 1s


MOV R7, #8 ; counter = 08, rotate 8 times
MOV A, #97H ; find the number of 1s in 97H
AGAIN:
RLC A ; rotate it through the CY
; once
JNC NEXT ; check for CY
INC R1 ; if CY = 1 then add one to
; count
NEXT:
DJNZ R7, AGAIN ; go through this 8 times
ROTATE INSTRUCTION-EXAMPLE
MOV A, #25H ;A= 25h
RR A ;A= 92h
RR A ;A= 49h
RR A ;A= A4h
RR A ;A= 52h

MOV A, #0A2H ;A= A2h


RL A ;A= 45h
RL A ;A= 8Ah
RL A ;A= 15h
RL A ;A= 2Ah
ROTATE INSTRUCTION-EXERCISE

CLR A ;A= 0
SETB C
RRC A ;A= 80h
SETB C
RRC A ;A= C0h

MOV A, #85H ;A= 85h


SWAP A ;A= 58h
ANL A, #0F0H ;A= 50h
RRC A ;A= 28h
RRC A ;A= 14h
EXERCISE
Assume that A has packed BCD. Write a program to convert
packed BCD to two ASCII numbers and display them on P1
and P2
I. Using Arithmetic Instructions II. Using Logical Instructions

ORG 0
MOV A, #69h ; Let A = 69h
MOV R0, A ; save original number
ANL A, #0Fh ; get lower BCD digit
ORL A, #30h ; convert it to ASCII
MOV P1, A ; display 1st number on P1
MOV A, R0 ; get original number
ANL A, #0F0h ; get upper BCD digit
SWAP A ; swap upper and lower nibbles
ORL A, #30h ; convert it to ASCII
MOV P2, A ; display 2nd number on P2
SJMP $
END
EXERCISE
Write a program that finds the position of the first high in
an 8-bit data item and display the result on P0. The data
is scanned from D7 to D0. Give the result for 68H
ORG 0
MOV A, #68h ; Let A = 68h
MOV R0, #8 ; initialize counter
AGAIN:
RLC A ; rotate left through carry
JC FND ; if MSB = 1, jump to FND
DJNZ R0, AGAIN ; repeat
MOV P0, R0 ; ‘1’ not found
SJMP $ ; stay here
FND:
MOV P0, R0 ; ‘1’ found, display its position
SJMP $
END
COMPARE INSTRUCTION
CJNE dest-byte, scr-byte, rel-address
; (Compare and jump if not equal)This instruction
compares dest-byte and scr-byte and jump to rel-
address if they are not equal

CJNE A, #34h, NEXT


CJNE R2, #40h, OVER
CJNE A, P1, WAIT
COMPARE INSTRUCTION

MOV A, #9 If (a==10)
cjne a, #10, next a++;
inc a Else if (a < 10)
jmp exit a = a+2;
next: jnc next2 Else
add a, #2 a = a +5;
jmp exit
next2: add a, #5
exit:
COMPARE INSTRUCTION - EXERCISE
Write a program to implement following C code
in assembly

if (a!=20)
{ if(b==10)
a = a*2;
else if (b < 10)
a = a+2;
else
a = a / 5;
}
ADDITION OF MORE THEN TWO NUMBERS-
EXAMPLE

MOV R0, #40H ; load pointer


MOV R2, #5 ; load counter
CLR A ; A=0
MOV R7, A ; clear R7
AGAIN:
ADD A, @R0 ; add the byte pointer to A
; by R0
JNC NEXT ; if CY=0 don't accumulate
; carry
INC R7 ; keep track of carries
NEXT:
INC R0 ; increment pointer
DJNZ R2, AGAIN ; repeat until R2 is zero
ADDITION OF MORE THEN TWO NUMBERS-
EXERCISE

Write a program to add the following numbers and


send the result to P1 and P2. The data is stored at
RAM addresses starting from 50H

(53, 49, 94, 56, 92, 65, 43, 83)

You might also like