Exp No.:5 Date: Arithmetic Operations
Exp No.:5 Date: Arithmetic Operations
ARITHMETIC OPERATIONS
AIM: To write the following assembly language programs to perform arithmetic operations
using EdSim simulator.
THEORY:
1.The ADD and ADDC Instructions ADD A, source ; A = A + source ADDC A, source ; A =
A + source + C A register must be involved in additions The C flag is set to 1 if there is a
carry out of bit 7 The AC flag is set to 1 if there is a carry out of bit 3 ADD is used for
ordinary addition ADDC is used to add a carry after the LSB addition in a multi-byte
process.
2. SUBB A, #data The SUBB Instruction SUBB A, direct SUBB A, @Ri , where i =0 or 1
SUBB A, source SUBB A, Rn, where n =0,1,,7 No borrow: A = A – source With borrow: A =
A – source – carry (i.e. borrow) Note that the 8051 uses the 2’s complement method to do
subtraction After execution: The C flag is set to 1 if a borrow is needed into bit 7 The AC
flag is set to 1 if a borrow is needed into bit 3.
3.The MUL Instruction MUL AB Uses registers A and B as both source and destination
registers Numbers in A and B are multiplied, then put the lower-order byte of the product in
A and the high- order byte in B The OV flag is set to 1 if the product > FFh Note that the C
flag is 0 at all times.
4. The DIV Instruction DIV AB Similarly, it uses registers A and B as both source and
destination registers The number in A is divided by B. The quotient is put in A and the
remainder (if any) is put in B The OV flag is set to 1 if B has the number 00h (divide-by-zero
error) Note that the C flag is 0 at all times.
5. The INC and DEC Instructions To increment (INC) or decrement (DEC) the internal
memory location specified by the operand No change with all the arithmetic flags in this
operation e.g. INC 7Fh ; content in 7Fh increased by 1 DEC R1 ; content in R1 decreased by
1 INC A INC direct INC @Ri where i=0,or 1 INC Rn where n=0,,7.
6. The CLR and CPL Instructions CLR A All bits in register A are cleared CPLA All bits in
register A are complemented (inverted) Note that CLR and CPL instructions operate on
register A only.
7. The SWAP Instruction Swapping the lower-nibble (lower 4 bits) and the higher-nibble
(upper 4 bits) of register A. 7 6 5 4 3 2 1 0 High Nibble Low Nibble SWAP A Register A =
5Eh (original value) after SWAP Register A = E5h.
8. Comparison Operation CJNE destination, source, relative address Compare the source and
destination operands first Jump to the relative address (subroutine) if they are not equal Carry
flag = 1, if destination-byte is less than the source-byte, Otherwise, the carry flag is cleared.
161105067
GOA COLLEGE OF ENGINEERING
2. Add the bytes in registers R3 and R4; put the result in RAM location 4Ah (LSB) and
4Bh (MSB).
mov R3,#255
mov R4,#10
mov A,R3
ADD A,R4
mov 4AH,A
mov A,#0
mov 4BH,#0
addc A,#0
mov 4BH,A
4. Add the byte in external RAM location 02CDh to internal RAM location l9h; put the
result into external RAM location 0000h (LSB) and 00C1h (MSB).
mov dptr,#02CDh
mov 19h,#22h
movx a,@dptr
add a,19h
anl a,#0F0h
161105067
GOA COLLEGE OF ENGINEERING
swap a
movx @dptr,a
;save MSB in r6
mov r0,a
anl a,#11110000b
swap a
mov r6,a
;save LSB in r5
mov a,r0
anl a,#00001111b
mov r5,a
6. .
mov r3,#00100101b
mov r4,#01000010b
mov a,r3
add a,r4
swap a
mov 4Bh,a
7. .
mov r0,#10000100b
mov 17h,#00010001b
mov 18h,#00000101b
mov a,r0
add a,17h
161105067
GOA COLLEGE OF ENGINEERING
mov 17h,a
mov a,r0
add a,18h
mov 18h,a
8. .
mov dptr,#02CDh
mov 19h,#00100010b
movx a,@dptr
add a,19h
9. Subtract the contents of R2 from the number F3h; put the result in external RAM
location 028Bh.
mov R2,#0A2h
mov A,#0F3H
subb A,R2
mov dptr,#028Bh
movx @dptr,A
10. Subtract the contents of R l from R0; put the result in R7.
mov R1,#0A6h
mov R0,#81h
mov A,R0
subb A,R1
mov R7,A
11. Subtract the contents of RAM location 13h from RAM location 2Bh: put the result in
RAM location 3Ch.
MOV 13H,#0B6H
MOV 2BH,#71H
MOV A,2BH
SUBB A,13H
MOV 3CH,A
161105067
GOA COLLEGE OF ENGINEERING
12. Subtract the contents of TH0 from TH I: put the result in TL0.
mov TH0,#0B3h
mov TH1,#62h
MOV A,8DH
SUBB A,TH0
MOV 8AH,A
13. Increment the contents of RAM location 13h, 14h and 15h using indirect addressing
only.
mov 13h,#47h
mov 14h,#65h
mov 15h,#31h
;incrementing 13,14,15
mov r1,#13h
mov r2,#3
loop:
mov a,r1
mov r0,a
mov a,@r0
inc a
mov @r1,a
inc r1
161105067
GOA COLLEGE OF ENGINEERING
161105067
GOA COLLEGE OF ENGINEERING
21. Multiply the data in RAM location 22h by the data in RAM location l5h; put the
result in RAM locations 19h (low byte) and lAh (high byte).
;multiply contents of 22h and 15h
mov 22h,#5h
mov 15h,#4h
mov a,22h
mov b,15h
mul ab
22. Square the contents of R5; put the result in R0 (high byte), and RI (low byte).
;square the content of r5
mov r5,#5h
mov a,r5
mov b,r5
mul ab
;save MSB in r0
mov r2,a
anl a,#0F0h
swap a
mov r0,a
;save LSB in r1
mov a,r2
anl a,#00Fh
mov r1,a
23. Divide the data in RAM location 3Eh by the number l 2h; put the quotient in R4 and
the remainder in R5.
mov 3Eh,#20h
mov b,#12h
mov a,3Eh
div ab
mov r2,a
mov r5,b
161105067
GOA COLLEGE OF ENGINEERING
24. Divide the number in RAM location 15h by the data in RAM location 16h; put the
result in external RAM location 7Ch.
mov 15h,#45h
mov 16h,#13h
mov a,15h
mov b,16h
div ab
mov dptr,#07Ch
movx @dptr,a
25. Divide the data in RAM location 13h by the data in RAM location 14h, then restore
the original data in l3h by multiplying the answer by the data in 14h.
mov 13h,#55h
mov 14h,#11h
26. Write a program to add the following numbers and save the result in R2, R3. The data
is stored in the on-chip ROM. ORG 250H MYDATA: DB 3, 94, 56, 92, 74, 65, 43,
23, 83.
org 0
mov dptr,#mydata
mov r0,#20h
mov r4,#9
;load the data into RAM
back:
clr a
movc a,@a+dptr
mov @r0,a
inc dptr
inc r0
djnz r4,back
161105067
GOA COLLEGE OF ENGINEERING
add a,b
jnc save ;save in @r1 if no carry is
generated
inc r6 ;else increment carry
mov 70h,r6
mov @r1,70h ;save carry into
location
inc r1 ;point to next destination
save:
mov @r1,a
inc r0 ;point to next data
djnz r4,addition
;save MSB in r2
mov a,31h
;save MSB in r2
mov a,31h
mov r2,a
;save LSB in r3
mov a,32h
mov r3,a
here:
sjmp here
;data in ROM
org 250h
mydata:
db 3,94,56,92,74,65,43,23,83
end
27. Write a program to add all the digits of your ROLLNO number and save the result in
R3. The result must be in BCD.
org 0
mov dptr,#mydata
mov r0,#20h
mov r2,#9
back:
clr a
movc a,@a+dptr
mov @r0,a
inc dptr
inc r0
djnz r2,back
mov r0,#20h
mov r2,#8
addition:
mov b,@r0
add a,b
mov r3,a
161105067
GOA COLLEGE OF ENGINEERING
inc r0
djnz r2,addition
here:
sjmp here
org 200h
mydata:
db 1,6,1,1,0,5,0,6,7
end
mov r0,#20h
mov r2,#8
addition:
mov b,@r0
add a,b
mov r5,a
inc r0
djnz r2,addition
;hex to bcd conversion
mov b,#64h
div ab
mov r1,a
mov a,b
mov b,#0Ah
div ab
mov r2,a
mov r3,b
here:
sjmp here
org 200h
mydata:
db 1,6,1,1,0,5,0,6,7
end
161105067
GOA COLLEGE OF ENGINEERING
;save MSB in r2
mov a,33h
mov 60h,a
;save LSB in r3
mov a,34h
mov 61h,a
161105067
GOA COLLEGE OF ENGINEERING
30. Write a program to add 897F9AH to 34BC48H and save the result in RAM memory
locations starting at 40H.
;first 32bit
mov 10h,#89h
mov 11h,#7Fh
mov 12h,#9Ah
;second 32bit
mov 20h,#34h
mov 21h,#0BCh
mov 22h,#48h
;add LSBs
mov a,12h
add a,22h
mov 42h,a
inc r6
mov 70h,r6
mov @r1,70h
inc r1
mov @r1,a
mov 41h,61h
;add MSBs (+carry)
mov a,10h
add a,20h
add a,60h
mov 40h,a
161105067