0% found this document useful (0 votes)
940 views

Exp No.:5 Date: Arithmetic Operations

The document describes assembly language programs to perform various arithmetic operations on 8051 microcontrollers using an EdSim simulator. It includes programs to add, subtract, multiply, and divide numbers stored in registers and memory locations. It also includes programs to increment, decrement, clear, complement, and swap bits in registers. The programs demonstrate performing these operations on integers and BCD encoded numbers.

Uploaded by

Abhijit Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
940 views

Exp No.:5 Date: Arithmetic Operations

The document describes assembly language programs to perform various arithmetic operations on 8051 microcontrollers using an EdSim simulator. It includes programs to add, subtract, multiply, and divide numbers stored in registers and memory locations. It also includes programs to increment, decrement, clear, complement, and swap bits in registers. The programs demonstrate performing these operations on integers and BCD encoded numbers.

Uploaded by

Abhijit Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

GOA COLLEGE OF ENGINEERING

Exp No.:5 Date:

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

PROGRAMS AND OUTPUT:


1. Add the bytes in RAM locations 34h and 35h: put the result in register R5 (LSB) and
R6 (MSB).
mov 34H,#12
mov 35H,#132
mov A,34H
ADD A,35H
mov R6,A ;only LSB is moved
mov a,#0
mov R5,#0
addc a,#0
mov R5,a ;MSB is moved

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

3. Add the number 84h to RAM locations 17h and 18h.


mov 18H,#0
mov 17H,#255
mov 18H,#10
mov A,17H
ADD A,#84H
mov 17H,A
mov A,#0
addc A,18H
mov 18H,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

;save MSB in 00C1


mov dptr,#00C1h
mov r0,a

anl a,#0F0h

161105067
GOA COLLEGE OF ENGINEERING

swap a
movx @dptr,a

;save LSB in 0000


mov dptr,#0000h
mov a,r0
anl a,#00Fh
movx @dptr,a

5. Repeat Problems 1-4, assuming the numbers are in BCD format.


mov 34h,#00010000b
mov 35h,#01000110b
mov a,34h
add a,35h

;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

;save MSB IN 4Bh


mov r0,a
anl a,##11110000b

swap a
mov 4Bh,a

;save LSB in 4Ah


mov a,r0
anl a, #00001111b
mov 4Ah,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

;save MSB in 00C1


mov dptr,#00C1h
mov r0,a
anl a, #11110000b
swap a
movx @dptr,a

;save LSB in 0000


mov dptr,#0000h
mov a,r0
anl a, #00001111b
movx @dptr,a

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

14. Increment TLI by 10h.


MOV R0,#42H
MOV TL1,#10H
LOOP:
INC TL1
DJNZ R0,LOOP

15. Increment external RAM locations 0100h and 0200h.


MOV DPTR,#0100H
INC DPTR
MOV DPTR,#0200H
INC DPTR

16. Add a 1 to every external RAM address from 00h to 06h.


mov dptr,#00h
mov r0,#06h
loop:
movx a,@dptr
add a,#01h
movx @dptr,a
clr a
inc dptr
djnz r0,loop

161105067
GOA COLLEGE OF ENGINEERING

17. Add a 1 to every external RAM address from 0100h to 0106h.


mov dptr,#0100h
mov r0,#6h
loop:
movx a,@dptr
add a,#1h
movx @dptr,a
clr a
inc dptr
djnz r0,loop

18. Decrement TL0, THO, TL1, and TH l.


mov th0,#12h
mov tl0,#24h
mov th1,#35h
mov tl1,#42h
dec th0
dec tl0
dec th1
dec tl1

19. Decrement external RAM locations 0l23h and 01 Bdh.

;decrementing content of 0123h


mov dptr,#0123h
movx a,@dptr
dec a
movx @dptr,a

;decrementing content of 01BDh


mov dptr,#01BDh
movx a,@dptr
dec a
movx @dptr,a

20. Decrement external RAM locations 45h and 46h.


;decrementing content of 45h
mov dptr,#45h
movx a,@dptr
dec a
movx @dptr,a

;decrementing content of 46h


mov dptr,#46h
movx a,@dptr
dec a
movx @dptr,a

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

;save MSB in 1Ah


mov r0,a
anl a,#0F0h
swap a
mov 1Ah,a

;save LSB in 19h


mov a,r0
anl a,#00Fh
mov 19h,a

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

;divide 13h by 14h


mov a,13h
mov b,14h
div ab
mov 13h,a

;restore original data


mov a,13h
mov b,14h
mul ab
mov 13h,a

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

mov r0,#20h ;location of stored data


mov r1,#30h ;destination for sum
mov r4,#8 ;count
clr a ;clear a for adding
mov r6,#00h ;carry
addition:
mov b,@r0

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

28. Modify Problem 3 to make the result 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 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

29. Write a program to


a) Write the value 55H to RAM locations 40H-4FH, and
b) Add all these RAM location contents together, and save the result in RAM
locations 60H and 61H.
mov r0,#40h
mov r1,#16
;load 55 from 40-4F
loop:
mov @r0,#55h
inc r0
djnz r1,loop

;addition of data from 40-4F


mov r0,#40h ;location of stored data
mov r1,#30h ;destination for sum
mov r4,#15 ;count
clr a ;clear a for adding
mov r6,#00h ;carry
addition:
mov b,@r0
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,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

;add mid bits (with carry)


mov r1,#60h
mov r6,#00h ;carry
mov a,11h
add a,21h

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

CONCLUSION:The experiment was executed successfully.

161105067

You might also like