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

8051 Basic Programs (Using Address)

This document contains assembly language code for several example programs that perform basic arithmetic and logic operations on 8-bit data. The programs demonstrate how to load data from memory, perform operations, and store results back to memory locations. Sample data and memory addresses are provided for each example program.

Uploaded by

Baishnabi Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

8051 Basic Programs (Using Address)

This document contains assembly language code for several example programs that perform basic arithmetic and logic operations on 8-bit data. The programs demonstrate how to load data from memory, perform operations, and store results back to memory locations. Sample data and memory addresses are provided for each example program.

Uploaded by

Baishnabi Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Assembler listing for example program 31.

1
PROGRAM TO ADD TWO 8-BIT DATA

2100 ORG 2100H specify program starting address.


2100 90 24 00 MOV DPTR,#2400H ; Load address of 1st data in DPTR.
2103 EO MOVX A, @DPTR Move the 1st data to A.
2104 F9 MOV R1,A Save the first data in R1.
2105 A3 INC DPTR Increment DPTR to point 2nd data.
2106 EO MOVX A, CDPTR Load 2nd data in A.
2107 78 00 MOV RO,#00H Clear RO to for carry.
account
10 2109 29 ADD A, R1 Get sum of data in A.
11
12 210A 50 01 JNC AHEAD ;Check carry flag.
13 210C 08 INC RO I f carry is set increment RO.
14
AssEMBLY LANGUAGE ProGRAMMING 8. 79
CHAPTER 8

210D A3 AHEAD: INC DPTR ;Increment DPTR.


15 ; Save sum in externa memorY
16 210E FO MOVX @DPTR, A
Increment DPTR.
210F A3 INC DPTR
18 2110 E8 MOV A, RO MOve Carry to A.
19 2111 FO MOVX DPTR,A Save carry 7n externa memory

2112 80 FE HALT: SJMP HALT Remain idle in infini te loo0. Program end.

22 2114 END ; ASsembly end.


Sample data
Memory address Content
Input Data Data-1 = F2
2400
Data-2 = 34,
2401 34
Output Data: Sum
Carry =01
264 2402 26
2403 01
Assembler listing for example program 31.2
; PROGRAM TO SUBTRACT TWO 8-BIT DATA

address.
2100 ORG 2100H Specify program starting
; Load address of minuend in
2100 DPTR.
90 24 00 MOV DPTR,#2400H
2103 EO MOVX A, @DPTR ; Movethe minuend to A.
2104 F9 MOV R1,A Save the minuend in R1.
7 2105 A3 INC DPTR ;Increment DPTR to point subtrahend.
8 2106 EO MOVX A, @DPTR ;Load subtrahend in A.
9 2107 c9 XCH A, R1 Get mi nuend inand subtrahend in R1.
A
10 2108 78 00 MOV RO, #00H Clear R0 to account for sign.
11 210A C3 CLR C Clear carry.
12 210B 99 SUBB A, R1 Subtract R1 from A.
13
14 210C 50 03 JNC AHEAD ;Check carry fl ag, If carry is set then,.
15 210E F4 CPL A get 2's complement of result in A.
16 210F 04 INC A
17 2110 08 INC RO ;Set RO as one to indicate negative result.
18 2111
19 2111 A3 AHEAD: INC DPTR Increment DPTR.
20 2112 FO MOVX @DPTR,A ;Save magni tude of result in external memory.
CHAPTER8 AsSEMBLY LANGUAGE PRoGRAMMING 8. 81

2113 A3 INC DPTR Increment DPTR


2114 ES MOV A, RO Move sign bit To A,

2115 FO MOVX aDPTR, A Save sign b1t in external memory

2116 80 FE HALT: SIMP HALT Remain idle in infinite l0op. Program end
END ; ASsembly end.
118
Sample dato Memory address Content
Input Data Minuend AC 2400 4C
subtrahend = F7 2401
Output Data : Difference =
AB 2402 AB

Sign bit 01, 2403 01


Assembler listing for example program 31.3
;PROGRAM TO MUL TIPLY TWO 8-BIT DATA

3 2100 ORG 2100H specify program starting address


4 2100 90 24 00 MOV DPTR,#2400H ; Load address of 1st data in DPTR
2103 EO MOVX A, aDPTR MOve the 1st data to A.
6 2104 F5 FO MOV B,A Save the first data in B.
2106 A3 INC DPTR Increment DPTR to point 2nd data.
2107 EO MOVX A, CDPTR Load 2nd data in A.
2108 A4 MUL AB Get the product in and
A B
10
11 2109 A3 INC DPTR ;Increment DPTR.
12 210A FO MOVX @DPTR,A ; Save l ow byte of product in external memory
13 2108 A3 INC DPTR Increment DPTR.
14 210C ES FO MOV A, B Move high byte of product to A,
15 210E FO MOVX QD PTR, A and save in external memory.
16
17 210F 80 FE HALT: SJMP HALT Remain idle in infinite loop. Program end.
18 211 END ASsemb ly end.
Sample data Memory address Content
Input Data Data -1 2400
=
C7, 2401 4A
Data -2 =
4A, 2402 86
Output Data: Product =
3986 2403 39

EXAMPLEPROGRAM 31.4 8-Bit Division


Assembler listing for example program 31.4
PROGRAM TO DIVIDE TWo 8-BIT DATA

2100 ORG 2100H ; specify program s t a r t i ng address


4 2100 90 24 00 MOV DPTR, #2400H ;Load address of dividend in DPTR
5 2103 EO MOVX A, CDPTR Load the dividend i n A.
6 2104 F8 MOV RO,A Save the divi dend in RO.
2105 A3 INC DPTR ;Let DPTR point to divsor.
8. 84 MICAOPROCESSORs AND MiCROCONTROLLERS

2106 EO MOVX A, @DPTR :Load the divisor in A.


9 2107 FS FO MOV B,A Move the divisor to B.
10 2109 ES MOV A, RO Move the dividend to A.
11 210A 84 DIV AB ;Divide the content of A by B
12
13 2108 A3 INC DPTR Increment DPTR
14 210c FO MOVX @DPTR,A ; Save quotient in external memoryy
15 210D A3 INC DPTR Increment DPTR
16 210E ES FO MOV A, B ;Move remainder to A,
7 2110 FO MOVX GDPTR, A and save in external memory.
18
19 2111 80 FE HALT: SJMP HALT Remain idle in infinite l0op. Program end
20 2113 END ASsembly end.
Sample data
Input Data : Dividend 64,
Memory addressContent
2400 64
Divisor 01
=07
2401 07
Output Data :
Quotient= OE,
Remainder = 02.
2402 OE
2403 02

EXAMPLEPROGRAM 31.5: Search for Smallest Data in an Array


Assembler listing for example progranm 31.5
;PROGRAM TO FIND SMALLEST DATA IN AN ARRAY

3 2100 ORG 2100H ; specify program starting address .


4 2100 90 24 00 MOV DPTR, # 2400H ;Set DPTR as pointer for array.
5 2103 EO MOVX A,@DPTR Get the count value in A.
2104 FS MOV RO0,A Set RO as counter for N-1 comparisons.
2105 18 DEC RO
8 2106 A3 INC DPTR ;Let DPTR point to 1st element of array.
9 2107 EO MOVX A,@DPTR
10 2108 FC MOV R4,A ;Let 1st element be smallest,
11
and save it in R4.
12 2109 A3 AGAIN: INC DPTR
13
;Make DPTR to point next element of array.
210A EO MOVX A, CDPTR Get next element of array in A,
14 210DB FA MOV R2,A and save in Rz.
15 210c C3 CLR C
16 210D 9c
Clear carry flag.
SUBB A, R4 Subtract current sma1lest from A.
18 210E 50 02 JNC AHEAD ;Check for carry, If carry is set.
19 2110 EA MOV A, R2
20 2111
then save content of R2 as current smallest.
FC MOV R4 ,A
21 2112 D8 F5 AHEAD DINZ RO,AGAIN
22 Decrement count and go to AGAIN if count is
not zero, otherwi se go to next instruction.
23 2114 90 25 00 MOV
24 2117 EC
DPTR, #2500H ;Load the address of result in DPTR.
MOV A, R4 Move the smallest data to A,
25 2118 FO MOVX@DPTR,A and save in external memory.
26
27 2119 80 FE HALT SIMP HALT
28 211B
Remain idle in infinite loop. Program end.
END ASsembly end.
Sample data
Memory Content
Input Data Count 06 address
Array = 7F
2400 06 Count
1C 2401 7F
42 2402 1C
57 2403 42
2404 Array
13 57
2405 13
FE
2406 F
Output Data: 13 2500 13 Smal lest data
EXAMPLE PROGRAM 31.6: Search for Largest Data in an
Array
end.

Assembler listing for example program 31.6


; PROGRAM TO FIND LARGEST DATA IN AN ARRAY

3 2100 ORG 2100H Specify program starting address


2100 90 24 00 MOV DPTR,#2400H ;Set DPTR as p0inter for array.
2103 EO MOVX A, GDPTR Get the count value in A
2104 F8 MOV R0,A Set R0 as counter for N-1 compari sons

8 2105 18 DEC RO
CHAPTER 8 AssEMBLY LANGUaGE ProGRAMMING 8. 89

10 2106 A3 INC DPTR ;Let DPTR point to 1st element of array


11 2107 EO MOVX A, aDPTR
12 2108 FC MOV R4,A ;1et 1st element be 1argest,
13 and save it in R4.
14 2109 A3 AGAIN: INC DPTR Make DPTR to point next element of array
15 210A EO MOVX A, aDPTR Get next element of array in A,
16 2108 FA MOV R2,A and save in R2.
210c 3 CLR C Clear Carry Flag.
18 2100 SUBB A, R4 Subtract current largest from A
210E 40 02 JC AHEAD Check for carry, Ifcarry is set go to AHEAD
2110 EA MOV A, R2 I f carry is not set,
2111 FC MOV R4,A then save content of R2 as current largest.

2112 D8 F5 AHEAD: DINZ R0, AGAIN ;Decrement cournt and go to again if count is
not zero,otherwi se go to next instruction.

2114 90 25 000 MOV DPTR,#2500H ; Load the address of result in DPTR


2117 EC MOV A,R4 Move the largest data to A,
2118 FO MOVX GDPTR, A and save in external memory
2119 80 FE HALT: SJMP HALT ;Remain idle in infinite loop. Program end.
33 211B END ASsembly end.
Sample data
MemorY Content
addresSS
Input Data Count = 06,H
Array = /F 2400 06 Count
1C 2401 7F
H

42 H 2402 1c
57 2403 42
H
2404 57 Array
13 2405 13
FEN 2406 FE
Output Data FE 2500 FE Largest data

BAMPLE PROGRAM 31.7: Sorting an Array in Ascending Ordeer


Assembler listing for example program 31.7
PROGRAM TO SORT AN ARRAY OF DATA IN
ASCENDING ORDER
3 2100 ORG 2100H Specify program starting address.
2100 90 24 00 MOV DPTR, #2400OH ;Load the count value in A-register.
2103 EO MOVX A, aDPTR
2104 FA MOV R2,A Set counter for (N-1)
8 2105 1A DEC R2 repetitions
of (N-1) comparisons.
9
10 2106 90 24 00 LOOP2: MOV DPTR,#2400H Set
11 2109
pointer for array.
EO MOVX A, @DPTR Set count for (N-1) Compari sons.
12 210A F9 MOV R1,A
13 210B 19 DEC R1
14
15 210C A3 INC DPTR
16
Increment pointer.
210D EO LOOP1 MOVX A, @DPTR
17
;Get twoconsecutive data of array in
210F FB MOV R3,A R3 and A-register.
18 210F A3 INC DPTR
19 2110 EO MOVX A, @DPTR
20 2111 C3 CLR C Compare and R3-register.
A
21 2112 9B SUBB A, R3 If content of A is greater than
22 2113 50 07 NC AHEAD R3 then go to AHEAD.
23
24 2115 EO MOVX A, aDPTR I f the content of A is less than
25 2116 15 82 DEC DPTR the content of R3-register.
26 2118 FO MOVX aDPTR,A then exchange the content of memory
27 2119 A3 INC DPTR pointed by DPTR and previous location.
28 21lA EB MOV A, R3
29 211B FO MOVX @DPTR,A
30
211C D9 EF AHEAD: DJNZ R1, LOOP1 Repeat comparisons until RI count is zero.
211E
211E DA EG DINZ R2, LOOP2 Repeat until R2 Count is zero.
34
35 2120 S0 FE HALT SJMP HALT Remain idle in infinite l0op. Program end.
36
7 2122 END ;ASsembly end.

Sample data
Me
a dmory
dres Content
ontent Memory Content
address address
Input Data: 07 2400 07 output data :07 2400 07
AB 2401 AB 34 2401 34
92 2402 92 4F 2402 4F
84 2403 84 69 2403 69
4F 2404 4F 84 2404 84
69 2405 69 92 2405 92
F2 2406 F2 AB 2406 AB
34 2407 34 F2 2407 F2
(Before sorting) CAfter sorting)

EXAMPLEPROGRAM 31.8: Sorting an Array in Descending Order


Assembler listing for example program 31.8
PROGRAM TO SORT AN ARRAY OF
DATA IN DECENDING ORDER
2100 ORG 2100H
Specify program starting address.
2100 90 24 00
2103 EO
MOV DPTR,#240OH ;Load the count value in
A-register.
MOVX A, CDPTR
2104 FA
2105
MOV R2,A Set counter for (N-1) repetitions
1A DEC R2 ;of (N-1) comparisons.
10 2106 90 24 00 LOOP2: MOV
11 2109
DPTR,#
#2400H Set pointer for array.
EO MOVX A, CDPTR
12 Set count for (N-1) comparisons.
210A F9 MOV R1,A
13 2108 19 DEC R1
14
15 210c A3 INC DPTR
16 Increment pointer.
210D EO LOOP1: MOVX A, @DPTR Get two consecutive data of array in
1 210E FB MOV R3,A R3 and A-register.
18 210F A3 INC DPTR
19 2110 EO MOVX A, @DPTR
20 2111 C3 CLR C Compare A and R3-register.
21 2112 9B SUBB A, R3 I f content of A is less than
2113 40 07 JC AHEAD ; R3 then go to AHEAD.

24 2115 EO MOVX A, GDPTR I f the content of A is greater than


25 2116 15 82 DEC DPTR the content of R3-register.
2118 FO MOVX @DPTR, A then exchange the content of memory
2119 A3 INC DPTR pointed by DPTR and previous location.
28 211A EB MOV A, R3
29 211B FO MOVX @DPTR, A
30
211c D9 EF AHEAD: DINZ R1, LOOP1 ;Repeat compari sons until Rl count is zero.
32
33 211E DA E6 DJNZ R2, LOOP2 ;Repeat until R2 count is zero.
34
35 2120 80 FE HALT: SJMP HALT ;Remain idle in infinite loop. Program end
36
37
37 2122 END ASsembly end.
Sample data

Memory Content Memory Content


address address
Input Data: 07 4200 07 Output Data 07 4200 07
C4 4201 C4 FA 4201
84 4202 84 E2 4202 E2
9A 4203 9A c4 4203 CA
4204 7B B2 4204 32
7B
E2 4205 E2 9A 4205 9A
F4 4206 F4 84 4206 34
4207 B2 7B 4207 73
B2
(Before sorting) CAfter sorting)

You might also like