Basic Assembly Language Programming Examples for Microcontrollers

Basic Assembly Language Programming Examples for Microcontrollers\

Microcontroller Assembly Language Programming

1. Write a program to implement the logical function “P1.4 = P1.0 ∨ (P1.1 ∧ P1.2) ∨ P1.3” using bit manipulation instructions.

MOV C,P1.1
ANL C,P1.2
ORL C,P1.0
ORL C,P1.3
MOV P1.4,C

2. Write a program that jumps to the LABLE storage unit if the content of accumulator A meets the following conditions. Assume A contains an unsigned number.

(1) A ≥ 10; (2) A > 10; (3) A ≤ 10.

(1)

CJNE A,#10,NEXT
LJMP LABLE
NEXT:
JNC LABLE

(2)

CJNE A,#10,NEXT
LJMP NEXT2
NEXT:
JNC LABLE
NEXT2:

(3)

CJNE A,#10,NEXT
LJMP LABLE
NEXT:
JC LABLE

3. Write a program to check if the data 55H exists in the internal RAM units from 30H to 50H. If found, set the 51H unit to FFH; if not found, clear the 51H unit to 0.

MOV R0,29H
NEXT:
INC R0
CJNE R0,#51H,NEXT2
MOV 51H,#0FFH
AJMP OVER
NEXT2:
CJNE @R0,#55H,NEXT
MOV 51H,#0
OVER:

4. Write a program to count the number of occurrences of 0 in the internal RAM units from 30H to 50H, and store the result in the 51H unit.

MOV R0,30H
MOV 51H,#0
NEXT:
CJNE @R0,#00H,NEXT2
INC 51H
NEXT2:
INC R0
CJNE R0,#51H,NEXT

5. There is a data block in external RAM containing several characters and numbers, starting at address SOURCE. Transfer this data block to the internal RAM area starting at DIST, stopping when the character “$” is encountered (the “$” should also be transferred, its ASCII code is 24H).

MOV DPTR,#SOURCE
MOV R0,#DIST
NEXT:
MOVX A,@DPTR
MOV @R0,A
INC DPTR
INC R0
CJNE A,#24H,NEXT

6. The internal RAM units 30H and 31H store a 16-bit binary number, with the high byte first and the low byte last. Write a program to compute its complement and store it back in the original location.

CLR C
MOV A,#0
SUBB A,31H
MOV 31H,A
MOV A,#0
SUBB A,30H
MOV 30H,A

7. There are two 4-byte compressed BCD numbers stored in the internal RAM, one in units 30H–33H and the other in units 40H–43H. Write a program to add them, storing the result in BCD format in units 30H–33H.

CLR C
MOV R0,#30H
MOV R1,#40H
MOV R2,#4
NEXT:
MOV A,@R0
ADDC A,@R1
MOV @R0,A
INC R0
INC R1
DJNZ R2,NEXT

8. Write a program to transfer 16 bytes of data stored in external RAM starting from 2000H to the internal RAM starting from 30H.

MOV DPTR,#2000H
MOV R0,#30H
MOV R1,#0
NEXT:
MOVX A,@DPTR
MOV @R0,A
INC DPTR
INC R0
INC R1
CJNE R1,#16H,NEXT

This article is reproduced from the internet, copyright belongs to the original author. If you find it unsatisfactory, please contact us for removal!

Basic Assembly Language Programming Examples for Microcontrollers

Leave a Comment