8051 Assembly Code Collection

Complete collection of 8051 microcontroller programs with copy functionality

⚙️ Addition Operation
AIM:
To perform addition
APPARATUS:
PC with Keil software
MOV A, #12h MOV R1, #23h ADD A, R1 here: SJMP here
OUTPUT:
A = 35h
Subtraction Operation
AIM:
To perform subtraction
APPARATUS:
PC with Keil software
MOV A, #45h MOV R1, #25h SUBB A, R1 here: SJMP here
OUTPUT:
A = 20h
✖️ Multiplication Operation
AIM:
To perform multiplication
APPARATUS:
PC with Keil software
MOV A, #06h MOV B, #07h MUL AB here: SJMP here
OUTPUT:
A = 2Ah (Lower byte of result)
B = 00h (Upper byte of result)
Division Operation
AIM:
To perform division
APPARATUS:
PC with Keil software
MOV A, #1Eh MOV B, #05h DIV AB here: SJMP here
OUTPUT:
A = 06h (Quotient)
B = 00h (Remainder)
📦 Move Block Data
AIM:
To write a program to move a block of data from one location to other
APPARATUS:
PC with Keil software
ORG 00H ; Set code starting address MOV R2, #05H ; R2 = counter = 5 bytes to move MOV R0, #50H ; R0 points to source block MOV R1, #70H ; R1 points to destination block loop: MOV A, @R0 ; Load value from source address into A MOV @R1, A ; Store value from A to destination address INC R0 ; Move to next source location INC R1 ; Move to next destination location DJNZ R2, loop ; Decrement counter and repeat if not zero here: SJMP here ; Infinite loop to end program END ; End of source code
OUTPUT:
Contents of memory from address 50H–54H are copied to 70H–74H
📡 Serial Communication (4800 Baud)
AIM:
To write a program to transmit letter 'N' serially at 4800 baud
APPARATUS:
PC with Keil software
MOV TMOD, #20H ; Timer1 in Mode2 (8-bit auto-reload) MOV TH1, #0FAH ; Load value for 4800 baud (TH1 = -6 = FAH) MOV SCON, #50H ; Serial mode1, 8-bit data, 1 stop bit, REN enabled SETB TR1 ; Start Timer1 AGAIN: MOV SBUF, #'N' ; Load letter 'N' into serial buffer HERE: JNB TI, HERE ; Wait until transmission complete CLR TI ; Clear TI flag SJMP AGAIN ; Repeat
💡 Stepper Motor Interface
AIM:
To demonstrate the stepper motor interface
APPARATUS:
8051 microcontroller, PC with Keil software
code segment assume cs:code start: mov ax, 0h ; Clear AX register mov dx, 0ffh ; Load DX with port address out dx, al ; Clear output port mov al, 0aah ; Load initial stepper pattern mov cx, 100 ; Load CX with step count g1: mov dx, 0ffh ; Load DX with port again out dx, al ; Output current pattern to port call delay ; Call delay subroutine rol al, 1 ; Rotate left to get next step pattern loop g1 ; Loop until 100 steps complete mov al, 100 ; Optional stop/cleanup signal g2: out dx, al ; Output to port call delay ; Final delay jmp g2 ; Infinite loop to halt ; DELAY SUBROUTINE delay: nop ; No operation (waste time) nop ; No operation (waste time) mov w, #ffh ; Load delay counter go: dec bw ; Decrement delay value jnz go ; If not zero, continue looping ret ; Return to caller
🧠 Logical Operations (AND, OR, XOR, NOT)
AIM:
To perform logical operations AND, OR, XOR, and NOT on 8051
APPARATUS:
PC with Keil software
; main.asm ; 8051 Logical Operations - AND, OR, XOR, NOT ORG 0000H MOV A, #0F0H ; A = 11110000b MOV R1, #0AAH ; R1 = 10101010b ANL A, R1 ; AND → A = A & R1 MOV 30H, A ; Result → 30H MOV A, #0F0H ORL A, R1 ; OR MOV 31H, A MOV A, #0F0H XRL A, R1 ; XOR MOV 32H, A MOV A, #0F0H CPL A ; NOT (complement) MOV 33H, A SJMP $ END
OUTPUT:
30H = A0H (AND result: 11110000 & 10101010 = 10100000)
31H = FAH (OR result: 11110000 | 10101010 = 11111010)
32H = 5AH (XOR result: 11110000 ^ 10101010 = 01011010)
33H = 0FH (NOT result: ~11110000 = 00001111)
🔌 DAC Interface - Square Wave Generator
AIM:
To write a program to generate a square wave using dual DAC interface
APPARATUS:
8051 Microcontroller
code segment assume cs:code start: mov al, 0FFh ; Load AL with high value (255) mov dx, 0FF0Ch ; DAC port address out dx, al ; Send high value to DAC b: mov al, 00h ; Load AL with low value (0) mov dx, 0FF0Ch ; DAC port address out dx, al ; Send low value to DAC call delay ; Short delay mov al, 0FFh ; Load AL with high value again mov dx, 0FF0Ch ; DAC port address out dx, al ; Send high value to DAC call delay ; Short delay jmp b ; Infinite loop to keep toggling int 3 ; Breakpoint (for debugging, optional) ; DELAY SUBROUTINE delay: mov cx, 64FFh ; Load delay count (random long value) d0: nop ; No operation (waste 1 cycle) loop d0 ; Decrement CX, loop till zero ret ; Return from subroutine