ALP Programs
ALP Programs
ALP Programs
1) Program to exchange the contents of Register B and external RAM Address 02CFh
MOV B,#20H
MOV DPTR,#02CFH ; 02CFH= 40H
MOV R0,B ;R0=20H
MOVX A,@DPTR ;A=40H
XCH A,R0 ;A=20H, R0= 40H
MOVX @DPTR, A ; 02CFH=20,
MOV B,R0 ; B=40H
END
2) Rotate the bytes in registers R0 to R3: copy the data in R0 to R1, R1 to R2 and R2 to R3 & R3 to R0
MOV R0,#22H
MOV R1,#33H
MOV R2,#44H
MOV R3,#55H
MOV A,R0 ;A=22H
XCH A,R1 ;A=33H, R1=22H
XCH A,R2 ;A=44H, R2=33H
XCH A,R3 ;A=55H, R3=44H
XCH A,R0 ;A=22H, R0=55H
END
5) Store the Program bytes from location 0100h-0102h to internal RAM location 20h-22h
MOV DPTR,#0100H
MOV R0,#20H
MOVC A,@A+DPTR
MOV @R0,A
MOV DPTR,#0101H
1
MOV R0,#21H
MOVC A,@A+DPTR
MOV @R0,A
MOV DPTR,#0102H
MOV R0,#22H
MOVC A,@A+DPTR
MOV @R0,A
END
6) Swap the nibbles of R0 and R1 so that low nibble of R0 swaps with high nibble of R1 and high nibble
of R1 and high nibble of R0 swaps with Low nibble of R1 (Input: R0=24h, r1=78h
Output: R0=87h, R1=42h)
MOV R0,#24H
MOV R1,#78H
MOV A,R0 ; A=24H
SWAP A ;A=42H
MOV R0,A ;R0=42H
MOV A,R1 ;A=78H
SWAP A ;A=87H
XCH A,R0 ;R0=87H, A=42H
MOV R1,A ; R1=42H
END
Logical Instructions
7) Complement the lower nibble of RAM location 2Ah (Input 2Ah=55h, Output:2Ah=5Ah)
MOV 2AH,#55H
XRL 2AH, #0FH
END
8) Make the low nibble of R5 the complement of high nibble of R6 (Input R6=72h, R5=34h, Output:
R6=72h, R5=38h)
10) Store the most significant nibble of A in both nibbles of RAM address 3Ch
MOV A,#34H
MOV R2,A ;R2=34H
ANL A, #0F0H ;A=30H
MOV R3,A ;R3=30H
SWAP A ;A=03H
ORL A,R3 ;A=33H
MOV 3CH,A ;3CH=33H
END
11) Set the carry flag to 1 if the number in A is even else set it to 0
MOV A,#43H
ANL A,#01H
ORL C,/0E0H
END
OR
MOV A,#45H
ORL C,/0E0H
END
12) Treat Regsiters R0 and R1 as 16-bit Registers and rotate them one place to the left
MOV R0,#0B4H
MOV R1,#074H
MOV A,R0 ;A=B4H (10110100)
RLC A ;A=68H (A=01001000, C=1)
MOV 00H,C ;00H=1
MOV R0,A ;R0=68H
MOV A,R1 ;A=74 (A=01110100)
CLR C ;C=0
RLC A ;A=E8 (A=11101000, C=0)
MOV R1,A ;R1=E8H
MOV A,R0 ;A=68H(A=0110100)
MOV ACC.0,C ;A=68H
MOV R0,A ;R0=68H
MOV C,00H ;C=1
MOV A,R1 ;A=E8H
MOV ACC.0,C ;A=E8H
MOV R1,A
END
3
Loop Control Instructions
13) Store natural numbers from M to N in BCD format in memory location. Read the limit N from
location.
14) Put a random number in address 20h and increment it until it equals a random number put in R5
15) Compare the numbers read from Ports 0 & 1 and put the largest number in R1
MOV A, P0
MOV R0, P1
CLR C
SUBB A, R0
JC NEXT ; if R0>A (P1>P0) jump to Next
MOV R1, P0
JMP LAST
NEXT: MOV A, R0
MOV R1, A
LAST: NOP
END
4
MOV A,B
JZ NOPRIME ; if remainder is 0 then the no. is not prime
INC R1 ; else increment R1 and continue dividing
MOV A, R1
CJNE A, 30H, BACK
MOV R2, #0FFH
NOPRIME: NOP
END
18) Sum of Array Elements & Store the sum in memory location 40h
MOV R0,#30H ; READ THE ARRAY SIZE
CLR C
BACK: INC R0
ADDC A,@R0
DA A
DJNZ 30H,BACK ;DECREMENT THE COUNT AND ADD REMAINING ELEMENTS
MOV 40H,A ; STORE THE RESULT IN MEMORY
END