MCU Assembly Language Programming
1. Write a program using bit manipulation instructions to implement the logic function: P1.4 = P1.0 ∨ (P1.1 ∧ P1.2) ∨ P1.3.
MOV C, P1.1
ANL C, P1.2
ORL C, P1.0
ORL C, P1.3
MOV P1.4, C
This code evaluates the logical expression by first checking P1.1 and P1.2 with an AND operation, then OR-ing the result with P1.0 and P1.3, and finally setting P1.4 based on the outcome.
2. Write a program that branches to the label LABLE if the contents of accumulator A meet the following conditions:
(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
These programs use the CJNE instruction to compare the value in A with a constant and branch accordingly based on the condition.
3. Write a program to check if there is a byte with value 55H in the RAM range from address 30H to 50H. If found, set the byte at 51H to FFH; otherwise, clear it to 0.
MOV R0, #30H
NEXT:
CJNE R0, #51H, NEXT2
MOV 51H, #0FFH
AJMP OVER
NEXT2:
CJNE @R0, #55H, NEXT
MOV 51H, #0
OVER:
This loop checks each byte in the specified range and sets the flag accordingly.
4. Write a program to count the number of zeros in the RAM range from 30H to 50H, and store the result in 51H.
MOV R0, #30H
MOV 51H, #0
NEXT:
CJNE @R0, #00H, NEXT2
INC 51H
NEXT2:
INC R0
CJNE R0, #51H, NEXT
The program iterates through the memory range, increments the counter when a zero is found, and stores the total in 51H.
5. Transfer a block of data from external RAM starting at SOURCE to internal RAM starting at DIST, until the character $ (ASCII 24H) is encountered.
MOV DPTR, #SOURCE
MOV R0, #DIST
NEXT:
MOVX A, @DPTR
MOV @R0, A
INC DPTR
INC R0
CJNE A, #24H, NEXT
This loop copies data from external to internal RAM until the termination character is reached.
6. The 16-bit binary number stored in addresses 30H and 31H (with high bit in 30H and low bit in 31H) needs to be negated and saved back.
CLR C
MOV A, #0
SUBB A, 31H
MOV 31H, A
MOV A, #0
SUBB A, 30H
MOV 30H, A
This code uses subtraction with borrow to invert the bits of the 16-bit number.
7. Add two 4-byte compressed BCD numbers stored in internal RAM, one in 30H–33H and the other in 40H–43H, and save the result in 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
This program adds the BCD numbers byte by byte, handling carry correctly.
8. Transfer 16 bytes of data from external RAM starting at 2000H to internal RAM starting at 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 loop transfers the data byte by byte and stops after 16 bytes have been copied.
Wuxi Ark Technology Electronic Co.,Ltd. , https://www.arkledcn.com