Assembly Language: Overview
• Assembly language is a low-level programming language that provides direct control over
microcontroller hardware.
• It uses mnemonics (short codes) to represent machine-level instructions.
• Commonly used in embedded systems, device drivers, and real-time applications.
Advantages (Pros) of Assembly Language
• Fast Execution Speed
• Precise Hardware Control
• Low Memory Usage
• Real-Time Performance
• Better Understanding of Hardware
Disadvantages (Cons) of Assembly Language
• Less Portability
• Time-Consuming Development
• Hard to Maintain
Steps to Learn Assembly Language
• Understanding Microcontroller Basics
• Learning the Instruction Set
• Learning Addressing Modes
• Practicing Branching and Looping Instructions
• Mastering Timing and Delay Calculations
• Building Real-Time Embedded Projects
Topics Covered
1.LED Control Using 8051 Microcontroller
2. LCD Display
3. 0–9 Counter on 7-Segment Display
4. DC Motor Control
5. Stepper Motor Rotation
6. Relay Control for Lamp
7.Square Wave Generation Using Timer0
8.UART-based LED Control and LCD Display using Virtual Terminal
Project 1: LED Control Using 8051 Microcontroller
In this project, I used 8051 Assembly language to control 3 LEDs (connected to Port 1) based on
the state of a switch (connected to Port 3). The goal was to understand low-level hardware control, port
manipulation, and execution timing.
ORG 0000H
MAIN:
MOV P1, #00H
SETB P3.0
LOOP:
JB P3.0, LED_ON
CLR P1.0
CLR P1.1
CLR P1.2
SJMP LED_OFF
LED_ON:
MOV P1, #07H
ACALL DELAY
SJMP LOOP
LED_OFF:
MOV P1, #00H
ACALL DELAY
SJMP LOOP
DELAY:
MOV R2, #250
D1: MOV R1, #200
D2: DJNZ R1, D2
DJNZ R2, D1
RET
END
Explanation:
MOV P1, #00H → All LEDs OFF initially.
SETB P3.0 → Initialize switch (optional if hardware pull-up used).
In LOOP:
• If P3.0 = 1 → Jump to LED_ON → Turn ON P1.0, P1.1, P1.2 (binary 00000111).
• If P3.0 = 0 → Clear LEDs → Go to LED_OFF.
In both LED states, a delay subroutine is called to create visible LED toggling.
Execution Time Calculation:
Assuming 8051 standard clock = 12 MHz → 1 machine cycle = 1 µs
Instruction Execution Times
MOV → 1 cycle → 1 µs SETB / CLR → 1 cycle → 1 µs
JB / SJMP → 2 cycles → 2 µs ACALL → 2 cycles → 2 µs
RET → 2 cycles → 2 µs
Inner Loop: DJNZ R1, D2 → 200 × 2 cycles = 400 cycles = 400 µs
Outer Loop: 250 times → Total = 250 × 401 cycles = 100,250 cycles = 100,250 µs ≈ 100 ms
Adding ACALL and RET → Total Delay ≈ 100.25 ms
Output: When switch is ON → All 3 LEDs (P1.0, P1.1, P1.2) turn ON; when switch is OFF → All 3 LEDs turn OFF.
Project 2: LCD Display Using 8051 Microcontroller
In this project, an LCD 16x2 display is interfaced with the 8051 microcontroller using Assembly
language. The goal is to initialize the LCD and display the message “HELLO WORLD” on the screen.
ORG 0000H
RS EQU P3.0
RW EQU P3.1
EN EQU P3.2
MAIN:
ACALL INIT_LCD
ACALL DISPLAY_STRING
HERE: SJMP HERE
INIT_LCD:
MOV P2, #38H
ACALL LCD_COMMAND
MOV P2, #0CH
ACALL LCD_COMMAND
MOV P2, #01H
ACALL LCD_COMMAND
MOV P2, #06H
ACALL LCD_COMMAND
RET
DISPLAY_STRING:
MOV DPTR, #MESSAGE
NEXT_CHAR:
CLR A
MOVC A, @A+DPTR
JZ END_DISPLAY
MOV P2, A
ACALL LCD_DATA
INC DPTR
SJMP NEXT_CHAR
END_DISPLAY:
RET
LCD_COMMAND:
CLR RS
CLR RW
SETB EN
ACALL DELAY
CLR EN
ACALL DELAY
RET
LCD_DATA:
SETB RS
CLR RW
SETB EN
ACALL DELAY
CLR EN
ACALL DELAY
RET
DELAY:
MOV R2, #255
D1: MOV R1, #255
D2: DJNZ R1, D2
DJNZ R2, D1
RET
MESSAGE:
DB 'HELLO WORLD', 00H
END
Explanation
The LCD is initialized using function set, display control, clear display, and entry mode commands.
The string “HELLO WORLD” is stored in memory and displayed on the LCD using the DISPLAY_STRING
routine
Each character is sent to the LCD through LCD_DATA subroutine.
A simple software delay is used for proper timing between commands.
Total Execution Time
• Inner loop: DJNZ R1, D2 → 255 times × 2 cycles = 510 cycles → 510 µs
• Outer loop: 255 times → Total delay = 255 × (1 cycle for MOV R1 + 510 cycles + 2 cycles for
DJNZ R2)
= 255 × (1 + 510 + 2) = 255 × 513 = 130,815 cycles
Total Delay Time:
130,815 cycles × 1 µs = 130,815 µs ≈ 130.8 ms
Output: LCD displays → "HELLO WORLD" on the first line.
Project 3: 7-Segment Display Using 8051 Microcontroller
In this project, a common cathode 7-segment display is controlled using 8051 assembly language.
The digits 0 to 9 are displayed sequentially with a delay between each digit.
ORG 0000H
MAIN:
MOV DPTR, #LOOKUP
MOV R3, #0
DISPLAY_LOOP:
MOV A, R3
MOVC A, @A+DPTR
MOV P1, A
ACALL DELAY
INC R3
CJNE R3, #10, DISPLAY_LOOP
SJMP MAIN
;---------------------------------------
LOOKUP:
DB 3FH
DB 06H
DB 5BH
DB 4FH
DB 66H
DB 6DH
DB 7DH
DB 07H
DB 7FH
DB 6FH
;---------------------------------------
DELAY:
MOV R7, #20
D1: MOV R6, #255
D2: MOV R5, #255
D3: DJNZ R5, D3
DJNZ R6, D2
DJNZ R7, D1
RET
END
DELAY Subroutine Time:
• Inner loop: DJNZ R5 → 255 × 2 cycles = 510 cycles (510 µs)
• Middle loop: 255 × (MOV + DJNZ) = 255 × (~512 cycles)
• Outer loop: 20 × (MOV + DJNZ + nested loops)
• Approximate total: 20 × 255 × 255 × 2 cycles ≈ several million cycles → results in delay of a
few hundred milliseconds.
This delay ensures visible digit change on the display.
Output: 7-segment display shows digits 0 to 9 one by one repeatedly.
Project 4: Relay Control for Lamp ON/OFF Using 8051 and ULN2003
This project uses an AT89C51 microcontroller and a ULN2003 relay driver to control a 12V relay that
switches a lamp ON and OFF at regular intervals.
ORG 0000H
MAIN:
MOV P1, #00H
LOOP:
SETB P1.0
ACALL LONG_DELAY
CLR P1.0
ACALL LONG_DELAY
SJMP LOOP
LONG_DELAY:
MOV R3, #50
DELAY_LOOP:
ACALL SMALL_DELAY
DJNZ R3, DELAY_LOOP
RET
SMALL_DELAY:
MOV R2, #255
D1: MOV R1, #255
D2: DJNZ R1, D2
DJNZ R2, D1
RET
END
Program Execution Flow:
1. The microcontroller turns P1.0 HIGH to activate the relay → Lamp turns ON.
2. After a software delay (~5 seconds), it turns P1.0 LOW to deactivate the relay → Lamp turns
OFF.
3. The process repeats indefinitely with alternating ON and OFF periods.
Output: The lamp toggles ON and OFF every 5 seconds.
Project 5: DC Motor Forward-Reverse Control Using 8051 and L293D
This project uses an AT89C51 microcontroller with an L293D motor driver to rotate a DC motor in
both forward and reverse directions. The direction control is achieved by toggling two pins (P1.0 and
P1.1) connected to the motor driver inputs.
ORG 0000H
START:
MOV P1, #00H
MAIN_LOOP:
SETB P1.0
CLR P1.1
ACALL DELAY_HIGH
CLR P1.0
SETB P1.1
ACALL DELAY_HIGH
CLR P1.0
CLR P1.1
ACALL DELAY_LOW
SJMP MAIN_LOOP
DELAY_HIGH:
MOV R7, #10
DH1: MOV R6, #255
DH2: MOV R5, #255
DH3: DJNZ R5, DH3
DJNZ R6, DH2
DJNZ R7, DH1
RET
DELAY_LOW:
MOV R7, #5
DL1: MOV R6, #255
DL2: MOV R5, #255
DL3: DJNZ R5, DL3
DJNZ R6, DL2
DJNZ R7, DL1
RET
END
Program Execution Flow:
1. Initially both control pins are low, motor is OFF.
2. Motor Forward:
o P1.0 = 1, P1.1 = 0 ⇒ Motor rotates forward.
3. Motor Reverse:
o P1.0 = 0, P1.1 = 1 ⇒ Motor rotates in reverse.
4. Motor OFF:
o Both P1.0 and P1.1 low ⇒ Motor stops.
5. Software delays control how long the motor stays in each state.
Output: The DC motor runs forward, reverse, and stops cyclically.
Project 6: Stepper Motor Rotation Using 8051 and ULN2003
This project uses an AT89C51 microcontroller with a ULN2003A driver IC to rotate a unipolar
stepper motor. The motor is rotated by sending sequential pulses to its coils through Port 1.
ORG 0000H
MAIN:
SJMP LOOP
LOOP:
ACALL STEP1
ACALL DELAY
ACALL STEP2
ACALL DELAY
ACALL STEP3
ACALL DELAY
ACALL STEP4
ACALL DELAY
SJMP LOOP
STEP1:
MOV P1, #01H
RET
STEP2:
MOV P1, #02H
RET
STEP3:
MOV P1, #04H
RET
STEP4:
MOV P1, #08H
RET
DELAY:
MOV R7, #7
D1: MOV R6, #255
D2: MOV R5, #255
D3: DJNZ R5, D3
DJNZ R6, D2
DJNZ R7, D1
RET
END
Program Execution Flow:
1. The program uses four subroutines (STEP1 to STEP4), each energizing a single coil of the
stepper motor.
2. These four steps are called in sequence with delays, creating a rotating magnetic field.
3. The motor rotates in one direction as long as the sequence continues.
Output: The Stepper Motor rotates continuously in the forward direction.
Project 7: 8051 Square Wave Generation Using Timer0 (1 kHz)
This project generates a 1 kHz square wave (500 µs ON, 500 µs OFF) on P1.0 using Timer0 Mode 1
(16-bit timer) of the AT89C51 microcontroller. The square wave is visualized on both an LED and a
Digital Oscilloscope.
ORG 0000H
MOV TMOD, #01H
MAIN_LOOP:
CPL P1.0
MOV TH0, #0FCH
MOV TL0, #066H
SETB TR0
WAIT_1:
JNB TF0, WAIT_1
CLR TR0
CLR TF0
SJMP MAIN_LOOP
END
Program Execution Flow:
1. P1.0 toggles (HIGH → LOW → HIGH) to create a square wave.
2. Timer0 is loaded with values for 500 microseconds delay.
3. Once the timer overflows, P1.0 toggles again, repeating forever.
Output: A 1 kHz square wave is observed on the oscilloscope and LED blinks rapidly.
Output: A 1 kHz square wave is observed on the oscilloscope and LED blinks rapidly.
Project 8: 8051 UART-Controlled LED via Virtual Terminal
8051 Assembly Language code for controlling an LED using UART communication where:
• If you send '1' from the keyboard (through serial terminal ), the LED turns ON.
• If you send '0', the LED turns OFF.
ORG 0000H
SJMP START
ORG 0030H
START:
MOV TMOD, #20H ; Timer1 Mode2 (8-bit auto reload)
MOV TH1, #0FDH ; Baud rate 9600 for 11.0592MHz crystal
MOV SCON, #50H ; Serial mode 1, REN enabled
SETB TR1 ; Start Timer1
MAIN_LOOP:
JNB RI, MAIN_LOOP ; Wait until character received
MOV A, SBUF ; Read received data into A
CLR RI ; Clear Receive Interrupt flag
CJNE A, #'1', CHECK_ZERO ; If not '1', check for '0'
SETB P1.0 ; Turn LED ON (P1.0 high)
SJMP MAIN_LOOP
CHECK_ZERO:
CJNE A, #'0', MAIN_LOOP ; If not '0', ignore and loop
CLR P1.0 ; Turn LED OFF (P1.0 low)
SJMP MAIN_LOOP
END
Concept Explanation:
UART (Serial Communication) Basics in 8051:
• The 8051 microcontroller has a built-in UART for serial communication via TXD (P3.1) and
RXD (P3.0) pins.
• Baud rate is typically set using Timer 1 in mode 2 (auto-reload mode).
Receiving Data:
• When a character is sent from the PC, it is stored in SBUF register.
• When receive is complete, RI (Receive Interrupt flag) in SCON register is set.
Processing Received Data:
• If received data is '1' (ASCII 0x31), set the LED pin high (turn ON).
• If data is '0' (ASCII 0x30), clear the LED pin (turn OFF).
LED Connection:
• LED is connected to P1.0 (you can change the pin as needed).
Output:
Typed '1' → LED ON.
Typed '0' → LED OFF.
Project 9: LCD Display using UART via Virtual Terminal
Interfacing a 16x2 LCD with an 8051 microcontroller and displayed messages received via UART
(Serial Communication). The system reads characters from a virtual terminal and displays them on
the LCD in real-time.
ORG 0000H
SJMP MAIN
ORG 0030H
RS EQU P3.5
RW EQU P3.6
EN EQU P3.7
MAIN:
MOV TMOD, #20H
MOV TH1, #0FDH
MOV SCON, #50H
SETB TR1
ACALL LCD_INIT
MAIN_LOOP:
JNB RI, MAIN_LOOP
MOV A, SBUF
CLR RI
ACALL LCD_DATA_CHAR
SJMP MAIN_LOOP
LCD_INIT:
CLR RS
CLR RW
MOV A, #38H
ACALL LCD_CMD
MOV A, #0CH
ACALL LCD_CMD
MOV A, #01H
ACALL LCD_CMD
MOV A, #06H
ACALL LCD_CMD
RET
LCD_CMD:
MOV P2, A
CLR RS
CLR RW
SETB EN
ACALL DELAY
CLR EN
ACALL DELAY
RET
LCD_DATA_CHAR:
MOV P2, A
SETB RS
CLR RW
SETB EN
ACALL DELAY
CLR EN
ACALL DELAY
RET
DELAY:
MOV R7, #255
D1: MOV R6, #255
D2: DJNZ R6, D2
DJNZ R7, D1
RET
END
Explanation:
• Configuring Timer 1 and Serial Control (SCON) for UART
• Sending and receiving data through SBUF
• Command & Data handling for LCD using control pins RS, RW, EN
• Writing efficient Assembly Language for embedded systems
How it works:
UART initialized using Timer1 for 9600 baud rate.
LCD is initialized with basic commands.
Each received character via UART (SBUF) is displayed on the LCD in real-time.
Software delay ensures proper LCD timing.
Characters received via UART are displayed on LCD in real-time using 8051 Assembly
Language.
Project 10: UART-Based Switch Status Transmission
Concept:
8051 microcontroller continuously monitors a switch connected to pin P1.0.
Based on the switch state:
• When the switch is pressed (logic 1) → The message “ALERT: Alarm!” is transmitted via
UART to the Virtual Terminal.
• When the switch is released (logic 0) → The message “System Normal” is transmitted.
ORG 0000H
SJMP MAIN
ORG 0030H
MAIN:
MOV TMOD, #20H
MOV TH1, #0FDH
MOV SCON, #50H
SETB TR1
SETB P1.0
MOV R5, #1
MAIN_LOOP:
MOV A, P1.0
ANL A, #01H
CJNE A, R5, STATE_CHANGED
SJMP MAIN_LOOP
STATE_CHANGED:
MOV R5, A
JNB P1.0, SEND_ALERT
ACALL UART_NORMAL
SJMP MAIN_LOOP
SEND_ALERT:
ACALL UART_ALERT
SJMP MAIN_LOOP
UART_ALERT:
MOV DPTR, #UART_MSG_ALERT
ALERT_LOOP:
CLR A
MOVC A, @A+DPTR
JZ ALERT_DONE
ACALL UART_SEND
INC DPTR
SJMP ALERT_LOOP
ALERT_DONE:
RET
UART_NORMAL:
MOV DPTR, #UART_MSG_NORMAL
NORMAL_LOOP:
CLR A
MOVC A, @A+DPTR
JZ NORMAL_DONE
ACALL UART_SEND
INC DPTR
SJMP NORMAL_LOOP
NORMAL_DONE:
RET
UART_SEND:
MOV SBUF, A
WAIT_TI:
JNB TI, WAIT_TI
CLR TI
RET
UART_MSG_ALERT:
DB 'ALERT: Alarm!',0
UART_MSG_NORMAL:
DB 'System Normal',0
END
Key Highlights:
• Uses UART serial communication at 9600 baud.
• Simple state memory technique using registers to control one-time alerts.
• Simulated using Proteus and tested in Keil uVision.
Switch Pressed (P1.0 = 1) ➔ Virtual Terminal shows:
ALERT: Alarm!