Chapter 4 8051Assembly Programming 99
END
The END directive signals the end of the assembly module. It indicates the end of the program to the
assembler. Any text in the assembly file that appears after the END directive is ignored. If the END
statement is missing, the assembler will generate an error message.
4.3 ASSEMBLY LANGUAGE PROGRAMS
EXAMPLE 4.4
Write a program to add the values of locations 50H and 51H and store the result in locations 52H and
53H.
ALGORITHM
Step 1: Load the memory contents 50H into A
Step 2: ADD the memory contents 51H with contents of A
Step 3: Store the contents of A in 52H
Step 4: Store the contents of carry flag in 53H.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,50H ; Load the contents of memory location 50H into A
ADD A,51H ; Add the contents of memory location 51H with contents of A
MOV 52H,A ; Save the least significant byte of the result in location
52H
MOV A, #00 ; Load 00H into A
ADDC A, #00 ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53
END
EXAMPLE 4.5
Write a program to subtract the values of locations 51H from 50H and store the result in location 52H.
If the result is positive, store 00H, else store 01H in 53H.
ALGORITHM
Step 1: Load the memory contents 50H into A A
Step 2: Clear the carry flag
Step 3: Subtract the memory contents 51H from the contents of A A
Step 4: Store the contents of A A in 52H
Step 5: Check the contents of carry flag, if the contents of carry flag
is 0 (result is positive) store 00H, else if the contents of
carry flag is 1 (result is negative) store 01H in 53H.
100 8051 Microcontroller: Hardware, So ware & Applications
ORG 0000H ; Set program counter 0000H
MOV A,50H ; Load the contents of memory location 50H into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Subtract the contents of memory location 51H from
content of A
MOV 52H,A ; Store the result in location 52H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53
END
EXAMPLE 4.6
Write a program to store data FFH into RAM memory locations 50H to 58H using direct addressing
mode.
ORG 0000H ; Set program counter 0000H
MOV A, #0FFH ; Load FFH into A
MOV 50H,A ; Store contents of A in location 50H
MOV 51H,A ; Store contents of A in location 51H
MOV 52H,A ; Store contents of A in location 52H
MOV 53H,A ; Store contents of A in location 53H
MOV 54H,A ; Store contents of A in location 54H
MOV 55H,A ; Store contents of A in location 55H
MOV 56H,A ; Store contents of A in location 56H
MOV 57H,A ; Store contents of A in location 57H
MOV 58H,A ; Store contents of A in location 58H
END
EXAMPLE 4.7
Write a program to store data FFH into RAM memory locations 50H to 58H using indirect addressing
mode.
ORG 0000H ; Set program counter 0000H
MOV A, #0FFH ; Load FFH into A
MOV R0, #50H ; Load pointer, R0=50H
MOV R5, #08H ; Load counter, R5=08H
Start: MOV @R0,A ; Copy contents of A to internal data RAM pointed
by R0
INC R0 ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
Chapter 4 8051Assembly Programming 101
EXAMPLE 4.8
Write a program to add two 16 bit numbers stored at locations 51H–52H and 55H–56H and store the
result in locations 40H, 41H and 42H. Assume that the least significant byte of data and the result is
stored in low address and the most significant byte of data or the result is stored in high address.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,51H ; Load the contents of memory location 51H into A
ADD A,55H ; Add the contents of memory location 55H with contents of A
MOV 40H,A ; Save the least significant byte of the result in location
40H
MOV A,52H ; Load the contents of memory location 52H into A
ADDC A,56H ; Add the contents of 56H and cy flag with contents of A
MOV 41H,A ; Save the second byte of the result in location 41H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data 00H and the contents of carry flag
to A
MOV 42H,A ; Save the most significant byte of the result in location 42H
END
EXAMPLE 4.9
Write a program to subtract a 16 bit number stored at locations 51H–52H from 55H–56H and store
the result in locations 40H and 41H. Assume that the least significant byte of data or the result is
stored in low address. If the result is positive, then store 00H, else store 01H in 42H.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,55H ; Load the contents of memory location 55H into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Sub the contents of memory location 51H from contents of A
MOV 40H,A ; Save the least significant byte of the result in location 40H
MOV A,56H ; Load the contents of memory location 56H into A
SUBB A,52H ; Sub the content of memory location 52H from the contents of A
MOV 41H,A ; Save the most significant byte of the result in location 41H
MOV A, #00 ; Load 00H into A
ADDC A, #00 ; Add the immediate data and the contents of carry flag to A
MOV 42H, A ; If result is positive, store 00H, else store 01H in location 42H
END
102 8051 Microcontroller: Hardware, So ware & Applications
EXAMPLE 4.10
Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H and 61H
and store the result in BCD at memory locations 52H and 53H. Assume that the least significant byte
of the result is stored in low address.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,60H ; Load the contents of memory location 60H into A
ADD A,61H ; Add the contents of memory location 61H with contents of A
DA A ; Decimal adjustment of the sum in A
MOV 52H, A ; Save the least significant byte of the result in location 52H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53
END
EXAMPLE 4.11
Write a program to clear 10 RAM locations starting at RAM address 1000H.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV DPTR, #1000H ; Copy address 1000H to DPTR
CLR A ; Clear A
MOV R6, #0AH ; Load 0AH to R6
again: MOVX @DPTR,A ; Clear RAM location pointed by DPTR
INC DPTR ; Increment DPTR
DJNZ R6, again ; Loop until counter R6 = 0
END
EXAMPLE 4.12
Write a program to clear 10 RAM locations starting at RAM address 10H.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV R0, #10H ; Copy address 10H to R0
CLR A ; Clear A
MOV R6,#0AH ; Load 0AH to R6
Chapter 4 8051Assembly Programming 103
again: MOV @R0,A ; Clear RAM location pointed by R0
INC R0 ; Increment R0
DJNZ R6, again ; Loop until counter R6 = 0
END
EXAMPLE 4.13
Write a program to compute 1 + 2 + …. + N (say 15) and save the sum at 70H
The program is as follows
ORG 0000H ; Set program counter 0000H
N EQU 15
MOV R0, #00 ; Clear R0
CLR A ; Clear A
again: INC R0 ; Increment R0
ADD A, R0 ; Add the contents of R0 with contents of A
CJNE R0, #N, again ; Loop until counter, R0 = N
MOV 70H,A ; Save the result in location 70H
END
EXAMPLE 4.14
Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the result
at memory locations 52H and 53H. Assume that the least significant byte of the result is stored in low
address.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,70H ; Load the contents of memory location 70H into A
MOV B,71H ; Load the contents of memory location 71H into B
MUL AB ; Perform multiplication
MOV 52H,A ; Save the least significant byte of the result in location 52H
MOV 53H,B ; Save the most significant byte of the result in location 53
END
EXAMPLE 4.15
Write a program to divide contents of 70H from contents of 71H (Assume contents of 70H is greater or
equal to contents of 71H). Store the remainder at memory location 53H and the quotient at memory
location 52H.
104 8051 Microcontroller: Hardware, So ware & Applications
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV A,70H ; Load the contents of memory location 70H into A
MOV B,71H ; Load the contents of memory location 71H into B
DIV AB ; Perform division
MOV 52H,A ; Save the quotient in location 52H
MOV 53H,B ; Save the remainder in location 53H
END
EXAMPLE 4.16
Ten 8 bit numbers are stored in internal data memory from location 50H. Write a program to
increment the data.
SOLUTION
Assume that ten 8 bit numbers are stored in internal data memory
from location 50H, hence R0 or R1 must be used as a pointer.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV R0,#50H ; Load pointer, R0=50H
MOV R3,#0AH ; Load counter, R3=0AH
Loop1: INC @R0 ; Increment contents of internal data RAM pointed
by R0
INC R0 ; Increment pointer
DJNZ R3, loop1 ; Repeat until R3 is zero
END
EXAMPLE 4.17
Write a program to store four 8 bit numbers in internal data RAM. Add these numbers and store the
result in 55H and 56H. Assume that the least significant byte of the result is stored in low address.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV 41H,#55H ; Store the first number in location 41H
MOV 42H,#76H ; Store the second number in location 42H
MOV 43H,#1AH ; Store the third number in location 43H
MOV 44H,#9FH ; Store the fourth number in location 44H
MOV R0,#41H ; Store the first number address 41H in R0
MOV R5,#04H ; Store the number 04H in R5
CLR C ; Clear the carry flag
MOV 56H,#00H ; Store the number 00H in location 56H
Chapter 4 8051Assembly Programming 105
MOV 55H,#00H ;
Store the number 00H in location 55
Loop: MOV A,55H ;
Store the contents of location 55H in A
ADD A,@R0 ;
Add contents of memory with the contents of A
MOV 55H,A ;
Store the contents of A in location 55H
MOV A,#00H ;
Store the number 00H in A
ADDC A,56H ;
Add contents of 56H and the contents of carry flag
to A
MOV 56H,A ; Store the contents of A in location 56H
INC R0 ; Increment contents of register R0
DJNZ R5,Loop ; Decrement R5, if it is not zero, branch to loop
END
EXAMPLE 4.18
Write a program to find the average of five 8 bit numbers. Store the result in 55H. (Assume that after
adding five 8 bit numbers, the result is 8 bit only)
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV 40H,#05H ; Store the first number in location 40H
MOV 41H,#55H ; Store the second number in location 41H
MOV 42H,#06H ; Store the third number in location 42H
MOV 43H,#1AH ; Store the fourth number in location 43H
MOV 44H,#09H ; Store the fifth number in location 44H
MOV R0,#40H ; Store the first number address 40H in R0
MOV R5,#05H ; Store the number 05H in R5
MOV B,R5 ; Store the number 05H in B
CLR A ; Clear the A
Loop: ADD A,@R0 ; Add contents of memory with the contents of A
INC R0 ; Increment contents of register R0
DJNZ R5,Loop ; Decrement R5, if it is not zero, branch to loop
DIV AB ; Divide the result by 5 (A)/(B)
MOV 55H,A ; Save the quotient in location 55H
END
EXAMPLE 4.19
Write a program to find the cube of an 8 bit number.
The program is as follows
ORG 0000H ; Set program counter 0000H
MOV R1,#N ; Load number to R1
MOV A,R1 ; Load number to A
MOV B,R1 ; Load number to B