ALP Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Data Transfer Instructions

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

3) Store DPTR in RAM locations 0123h(DPL) and 02BCh(DPH)


MOV DPTR, #1234H
MOV A,DPH ; A=12H
MOV B,DPL ;B=34H
MOV DPTR,#02BCH
MOVX @DPTR,A ;02BCH=12H
MOV A,B ;A=34H
MOV DPTR,#0123H
MOVX @DPTR,A ;0123H=34H
END

4) Exchange both low nibbles of Registers R0 & R1


MOV R0,#34H
MOV R1,#56H
MOV A, R0 ;A=34H
MOV R0,#01H
XCHD A,@R0 ;A=54H, R1=36
MOV R0,A ;R0=36H
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)

MOV R6, #72H


MOV R5, #34H
XRL 06H, #0F0H ;R6=82H
MOV A,R6 ;A=82H
SWAP A ;A=28H
ANL A, #0Fh ;A=08H
MOV R2,A ;R2=08H
MOV A,R5 ;A=34H
ANL A, #0F0H ;A=30H
ORL A, R2 ;A=38H
MOV R5, A ;R5=38H
END

9) Mov the bit 6 of R0 to bit 3 of Port 3


2
MOV R0,#04H ;P0=00000100
MOV A,R0 ;A=04H
MOV C,ACC.6 ;Mov the 6th Bit of A to C
MOV P0.3,C ;
END

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.

MOV R0, #30H ;STORE THE LIMIT N IN ADDRESS 30H


MOV 02H,@R0 ;STORE THE LIMIT N IN R2 REG
MOV A, #20H ;ASSIGN A TO M=20H
BACK: INC R0 ;INCREMENT THE ADDRESS POINTER
ADD A,#1
DA A
MOV @R0, A
DJNZ R2, BACK ;DECREMENT THE COUNT TILL 0
END

14) Put a random number in address 20h and increment it until it equals a random number put in R5

MOV A, 20H ;STORE A RANDOM NUMBER IN A FROM LOCATION 20H


MOV R5, #50H ;STORE A LIMIT IN R5
MOV R0, #21H
BACK: INC A
MOV @R0, A
INC R0
CJNE A, 05H, BACK
END

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

16) Check if a given number is prime or not

MOV A, #20H ;No to be checked is moved to A


MOV 30H, A ;Store the no. in memory location 30h
MOV R1, #02H ;Initialize R1=2 (to divide the no from 2 to n-1)
BACK: MOV A, 30H
MOV B, R1 ; move the divisor to B
DIV AB

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

17) To search an element in an array (Using Linear Search Algorithm)

MOV R0,#30H ;READ THE SIZE OF THE ARRAY


BACK: INC R0
MOV A,@R0
CJNE A,#02H,NEXT ;COMPARE THE ARRAY ELEMENTS WITH KEY #02H
MOV R4,#02H ; IF FOUND STORE THE KEY IN R4 REG
JMP LAST
NEXT: DJNZ 30H,BACK ; ELSE DECREMENT THE COUNT AND COMPARE THE NEXT ELEMENT
LAST: 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy