Essential 8051 Microcontroller Assembly Language Programs
Data Transfer Between Specified Memory Locations
This program loads the values 30H, 40H, and 05H into registers R0, R1, and R7, respectively. It then moves the data at the address pointed to by R0 to the accumulator (A), moves the content of A to the address pointed to by R1, increments R0 and R1, and decrements R7. This process repeats until R7 becomes zero.
MOV R0,#30H
MOV R1,#40H
MOV R7,#05H
BACK: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R7,BACK
STOP: SJMP STOP
Data Exchange Between Specified Memory Locations
This program sets the source address 30H to R0, 40H to R1, and loads 05H to R7. It then moves the data pointed to by R0 to register A, exchanges the content of A with the data in R1, stores the data in A to the address pointed by R1, increments R0 and R1, and decrements R7. This process repeats until R7 becomes zero.
MOV R0,#30H
MOV R1,#40H
MOV R7,#05H
BACK: MOV A,@R0
XCH A,@R1
MOV @R0,A
INC R0
INC R1
DJNZ R7,BACK
STOP: SJMP STOP
Finding the Smallest/Largest Number in a Series
This program loads R0 with 50H, R1 with 0AH, and B with FFH (or 00H to find the smallest number). It then compares the data at the address pointed to by R0 with the value in register B. If they are not equal, it jumps to the label ‘LOOP’. If they are equal, it moves the value of B to A, increments R0, and decrements R1. This process repeats until R1 becomes zero.
MOV R0, #50H
MOV R1, #00AH
MOV B, #0FFH ; For largest (use #00H for smallest)
BACK: MOV A,@R0
CJNE A, B, LOOP
LOOP: JNC LOOP1
MOV B, A
LOOP1: INC R0
DJNZ R1, BACK
SJMP STOP1
STOP1: SJMP STOP1
Sorting in Ascending/Descending Order
Load R4 with 0V, Load R3, Load R0 with 20, Clear the carry bit, Move data pointed by R0 to A register, MOve the value from RA to R7, Increment R0, Move data pointed by R register to A register, Subtract A and R7, Jump to step 18 if no carry, Else move data ointed by R0 to A, Decrement R0, Store dataof A to R0, Move data of A to R1, Increment R0, Store data of A register in A, Decrement R0, Decrement R4 and jump to step 3 if not equal MOV R4,#04H AGAIN: MOV R3, #04H MOV R0, #20H CLR C UP: MOV A,@R0 MOV R1, A INC R0 MOV A,@R0 SUBB A, R1 JNC SKIP (JC SKIP) MOV A,@R0 DEC R0 MOV @R0, A MOV A, R1 INC R0 MOV @R0, A SKIP: DJNZ R3,UP DJNZ R4, AGAIN Here: SJMP Here
WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DO ADDITION/SUBTRACTION OF 8BIT DATA Load 20 to R0 and load 30 to R1, The content address pointed bt R0 move to A, Move A to R5, Clear register R4 to store carry, Point to the location, The content address pointed by R0 move to A, Add R5 with A and store to A (Subtract R5 from A and store to A register), Jump to steo 14 if no carry, Increment to R0 to get carry, Move carry to B register, Store the carry first, Increment R1, Store the result MOV R0,#20H MOV R1,#30H MOV A,@R0 MOV R5,A MOV R4,#00H INC R0 MOV A,@R0 ADD A,R5 (SUBB A,R5) JNC SAVE INC R4 MOV B,R4 MOV @R1,B INC R1 SAVE: MOV @R1,A HALT: SJMP HALT WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DO MULTIPLICATION /DIVISION OF 8BIT DATA Load 20 to R0, The content address pointed by R0 is moved to A, Move the value from A to R5, point the next location, The content address pointed by R0 move to A Multiply A and B and store to A register MOV R0,#20H MOV A,@R0 MOV B,A INC R0 MOV A,@R0 MUL AB (DIV AB) STOP:SJMP STOP WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DO ADDITION/SUBTRACTION OF 16BIT DATA Make CY=O, Lower byteof operand 1 in A, add lower byte of operand 2 with A, stores LSB of result in R1, Higher byte of operand 2 in A, Add with higher byte of operand 1, store msb of result in R0 CLR C MOV A,#20H ADD A,#DEH (SUBB A,#0DEH) MOV R1,A MOV A,#65H ADDC A,#0ABH (SUBB A,#0ABH) MOV R0,A STOP:SJMP STOP WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE SUM OF SERIES 8 BIT DATA Load 50 to R0 and load 05 to R2, Clear A, Move A to R7, Add A and the constant address pointed by R0 then force the value in A register, Jump to step 8 if no carry, Increment R7, Increment R0, Decrement R2 and jump to step 5 if R2 is non zero MOV R0,#50H MOV R2,#05H CLR A MOV R7,A Loop: ADD A,@R0 JNC NEXT INC R7 NEXT: INC R0 DJNZ R2,Loop STOP:SJMP STOP
SQUARE OF 8 BIT DATA Set source address 50 to R0, Take the value from source to A register, Move vaue to B register from A register, Multiply A and B registers MOV R0,#50H MOV A,@R0 MOV B,A MUL AB STOP:SJMP STOP CUBE OF 8 BIT DATA Set souce address 20H in R1 register, Move the value from R1 to A register, Move the value to B register from A register, Move the value of R0 to A register, Multiply A and B register, Move the value of B to address 30H, Move the value of R0 to B register, Multiply A and B registers, Move value of A and B to address 21H and 31H respectively, Move value in 30H and R0 to A and B register respectively, Multiply A and B registers, Add A register and value in 31H along with carry bit, Move value in B register to A register, Move value in A register to 23H MOV R1,#20H MOV A,@R1 MOV B,A MOV R0,A MUL AB MOV 30H,B MOV B,R0 MUL AB MOV 21H,A MOV 31H,B MOV A,30H MOV B,R0 MUL AB ADDC A,31H MOV 22H,A MOV A,B MOV 23H,A HERE:SJMP HERE SQUARE ROOT OF 8 BIT DATA Set source address 40H in R0 register, Set R2 register with 00, Move value of R0 with A registrer, Subtract A and R1 registers, Increment R2, Jump to step 3 if zero, Jump to step 12 if carry, Increment R1, Increment R1, Short Jump to step 5, Move OFFH to the address 41H, Move the value of R2 register to 41H MOV R0,#40H MOV R1,#01H MOV R2,#00H MOV A,@R0 LOOP: SUBB A,R1 INC R2 JZ ANSWER JC FALSE INC R1 INC R1 SJMP LOOP FALSE: MOV 41H, #0FFH ANSWER: MOV 41H,R2 STOP:SJMP STOP