PROGRAMMING OF 8051 Introduction - Assembly language program –Conversion of 8-bit
binary number to its equivalent BCD – Addition of two 16-bit numbers - Subtraction of two
16- bit numbers – Finding the largest number from the given set of numbers – Arranging a set
of numbers in ascending order – Finding the average of N numbers -Finding the number of
positive and negative number
Introduction to 8051 Microcontroller Programming in Assembly Language
Assembly language is a low-level programming language that provides direct control over
hardware. It is used for:
Efficient memory utilization (since 8051 has limited memory).
Fast execution as instructions are optimized for hardware.
Precise timing control for real-time applications.
Every 8051 Assembly program follows this structure:
A. Header Section
Defines the processor and memory locations.
B. Data Section
Defines constants and memory variables.
C. Code Section
Contains the main program logic and execution flow.
Sample Format
ORG 0000H ; Start program from memory address 0000H
MOV A, #10H ; Example instruction
END ; End of program
1. Conversion of 8-bit Binary to BCD
Divide the binary number by 10 using repeated subtraction.
The quotient gives the MSD (Most Significant Digit).
The remainder gives the LSD (Least Significant Digit)
PROGRAM
ORG 0000H ; Start of the program
MOV A, #57H ; Load binary number (Example: 57H = 87 in decimal)
MOV B, #0AH ; Load divisor 10 in decimal
DIV AB ; A = Quotient (MSD), B = Remainder (LSD)
MOV 40H, A ; Store MSD at 40H
MOV 41H, B ; Store LSD at 41H
END ; End of the program
Explanation of the Program:
1. Loading the Number and Divisor:
The number 57H (which is 87 in decimal) is loaded into register A.
The divisor 0AH (which is 10 in decimal) is loaded into register B.
2. Division:
The DIV AB instruction divides the value in A by the value in B.
The quotient (MSD) is stored in register A.
The remainder (LSD) is stored in register B.
3. Storing the Results:
The quotient (MSD) is stored in memory location 40H.
The remainder (LSD) is stored in memory location 41H.
Example Calculation:
Let's take 57H (which is 87 in decimal) and divide it by 10 (decimal).
87 ÷ 10 = 8 (quotient, which will be stored in 40H).
Remainder 87 % 10 = 7 (remainder, which will be stored in 41H).
Final Memory Output:
Memory location 40H will store the most significant digit (MSD), which is 08H (8 in
decimal).
Memory location 41H will store the least significant digit (LSD), which is 07H (7 in
decimal).
2. Addition of Two 16-bit Numbers
Load the lower bytes of the two numbers.
Add them and store the result.
Load the higher bytes of the numbers.
Add them with the carry from the previous addition.
PROGRAM
ORG 0000H ; Start of the program
MOV 50H, #34H ; First 16-bit number LSB
MOV 51H, #12H ; First 16-bit number MSB
MOV 52H, #28H ; Second 16-bit number LSB
MOV 53H, #05H ; Second 16-bit number MSB
MOV A, 50H ; Load first LSB
ADD A, 52H ; Add second LSB
MOV 54H, A ; Store result LSB
MOV A, 51H ; Load first MSB
ADDC A, 53H ; Add second MSB with carry
MOV 55H, A ; Store result MSB
END ; End of the program
CALCULATION
Step-by-Step Explanation:
Step 1: Data Initialization
The first 16-bit number is stored in memory:
o 50H = 34H (LSB of the first number)
o 51H = 12H (MSB of the first number)
The second 16-bit number is stored in memory:
o 52H = 28H (LSB of the second number)
o 53H = 05H (MSB of the second number)
Step 2: Addition of LSBs
MOV A, 50H → Load 34H (LSB of the first number) into the accumulator.
ADD A, 52H → Add 28H (LSB of the second number) to the accumulator.
o 34H + 28H = 5CH
o No carry generated.
MOV 54H, A → Store the result 5CH in 54H (LSB result).
Step 3: Addition of MSBs with Carry
MOV A, 51H → Load 12H (MSB of the first number) into the accumulator.
ADDC A, 53H → Add 05H (MSB of the second number) and the carry (if any) to the
accumulator.
o 12H + 05H = 17H
o No carry generated.
MOV 55H, A → Store the result 17H in 55H (MSB result).
LSB (54H): 5CH
MSB (55H): 17H
The final result is 175CH.
3. Subtraction of Two 16-bit Numbers
Loads two 16-bit numbers.
Subtracts the lower bytes and stores the result.
Subtracts the higher bytes with borrow and stores the result.
ORG 0000H
MOV 60H, #58H ; First 16-bit number LSB
MOV 61H, #19H ; First 16-bit number MSB
MOV 62H, #34H ; Second 16-bit number LSB
MOV 63H, #07H ; Second 16-bit number MSB
MOV A, 60H ; Load first LSB
SUBB A, 62H ; Subtract second LSB
MOV 64H, A ; Store result LSB
MOV A, 61H ; Load first MSB
SUBB A, 63H ; Subtract second MSB with borrow
MOV 65H, A ; Store result MSB
END
CALCULATION
Given Inputs:
First 16-bit number: 1958H (MSB = 19H, LSB = 58H)
Second 16-bit number: 0734H (MSB = 07H, LSB = 34H)
Subtraction Step by Step:
1. LSB Calculation:
58H−34H=24H
No borrow occurs.
2. MSB Calculation:
19H−07H=12H
No borrow occurs.
Final Result:
64H = 24H (LSB)
65H = 12H (MSB)
Final Output:
1224H
4. Finding the largest number from the given set of numbers
ORG 0000H ; Start of the program
MOV 30H, #10H ; First number (10H)
MOV 31H, #20H ; Second number (20H)
MOV 32H, #15H ; Third number (15H)
MOV 33H, #30H ; Fourth number (30H)
MOV A, 30H ; Load the first number into the accumulator
MOV R0, A ; Store the first number in R0 (Largest number so far)
MOV A, 31H ; Load the second number
INC R0 ; Compare with current largest
CJNE A, R0, Next_Compare1 ; If not equal, jump to Next_Compare1
MOV A, 32H ; Load the third number
INC R0 ; Compare with the current largest
CJNE A, R0, Next_Compare2 ; If not equal, jump to Next_Compare2
MOV A, 33H ; Load the fourth number
MOV R0, A ; Final update if needed
Next_Compare1
Next_Compare2
END ; End of the program
Step-by-Step Explanation:
1. The numbers 10H, 20H, 15H, 30H are stored at memory locations 30H, 31H, 32H, 33H.
2. The first number (10H) is loaded into the accumulator A, and stored in register R0 as the
current largest number.
3. The program then compares the second number (31H = 20H) with the current largest number
(R0).
4. If the second number is larger, it updates R0 with the new largest number.
5. This process is repeated for the third and fourth numbers.
6. The largest number after all comparisons is stored in R0
After comparing all the numbers, the largest number will be stored in R0.
Given the numbers 10H, 20H, 15H, 30H, the largest number is 30H.
[Link] a set of numbers in ascending order
This program sorts a given set of numbers in ascending order by comparing adjacent numbers
and swapping them if they are in the wrong order. The process is repeated until the entire set
of numbers is sorted.
ORG 0000H ; Start of the program
; Initialize the data set of numbers
MOV 30H, #10H ; First number (10H)
MOV 31H, #20H ; Second number (20H)
MOV 32H, #15H ; Third number (15H)
MOV 33H, #30H ; Fourth number (30H)
; Outer loop (for number of passes)
MOV R0, #03H ; R0 = 3 (for 3 comparisons)
OuterLoop:
; Inner loop (for comparing adjacent numbers)
MOV R1, #00H ; Start with the first number
MOV R2, #03H ; Total numbers to compare (3 comparisons)
InnerLoop:
MOV A, 30H ; Load first number into A
MOV B, 31H ; Load second number into B
CJNE A, B, SkipSwap ; If A != B, skip swapping
; Swap numbers if needed
MOV 31H, A ; store smaller
MOV 30H, B ; store larger
SkipSwap :
; Proceed to next number
MOV B,
END
Explanation of the Code:
1. Data Initialization:The program starts by initializing four numbers in memory: 30H, 31H,
32H, and 33H.
2. Outer Loop:The outer loop ensures that the sorting process is repeated for the required
number of passes. In the case of a set of four numbers, we need three passes (R0 = 3).
3. Inner Loop:The inner loop compares adjacent numbers and swaps them if the first number is
greater than the second number.
4. Swapping:If the numbers are out of order, they are swapped using the MOV instruction.
5. Repeat until Sorted:The program continues until all numbers are sorted in ascending order.
Final Output:
Assuming the input numbers are:
30H = 10H (16)
31H = 20H (32)
32H = 15H (21)
33H = 30H (48)
After sorting, the numbers will be arranged in ascending order:
30H = 10H (16)
31H = 15H (21)
32H = 20H (32)
33H = 30H (48)
This means the sorted order will be: 10H, 15H, 20H, 30H.
[Link] the average of N numbers
Sum up the N numbers stored in memory.
Divide the sum by N to get the average.
PROGRAM
ORG 0000H ; Start of the program
; Initialize the numbers
MOV 30H, #10H ; First number (10H)
MOV 31H, #20H ; Second number (20H)
MOV 32H, #30H ; Third number (30H)
MOV 33H, #40H ; Fourth number (40H)
MOV R0, #04H ; Number of numbers (N = 4)
MOV R1, #00H ; Clear R1 to store sum of numbers
; Start summing the numbers
MOV A, 30H ; Load the first number into A
ADD A, 31H ; Add the second number
ADD A, 32H ; Add the third number
ADD A, 33H ; Add the fourth number
MOV R1, A ; Store the sum of numbers in R1
; Divide the sum by N (R0 = 4)
MOV A, R1 ; Load the sum into A
MOV B, R0 ; Load N (4) into B
DIV AB ; Divide A by B, quotient is in A, remainder in B
MOV 40H, A ; Store the quotient (average) in memory location 40H
; End of the program
END
Explanation of the Program:
1. Data Initialization: Initialize four numbers in memory locations 30H to 33H.
2. Summing the Numbers: Load each number into the accumulator A and add the subsequent
numbers using the ADD instruction. After adding all the numbers, we store the sum in register
R1.
3. Division (Finding the Average):The sum is divided by N (which is 4 in this example) using
the DIV AB instruction. The quotient (the average) is stored in A and the remainder is stored
in B.
4. Store the Average:Finally, the average (quotient) is stored in memory location 40H
Example:
Given numbers:
30H = 10H (16)
31H = 20H (32)
32H = 30H (48)
33H = 40H (64)
Sum of numbers:
Sum = 16 + 32 + 48 + 64 = 160
Average:
Average = Sum / N = 160 / 4 = 40
Final Output:
The program stores the average in memory location 40H, which will contain 40H (decimal value 64).
[Link] to Find the Number of Positive and Negative Numbers
Check each number from a given set of numbers.
Count how many numbers are positive and how many are negative.
ORG 0000H ; Start of the program
; Initialize the numbers
MOV 30H, #10H ; First number (positive 10)
MOV 31H, #-20H ; Second number (negative -20)
MOV 32H, #30H ; Third number (positive 30)
MOV 33H, #-40H ; Fourth number (negative -40)
; Initialize counters for positive and negative numbers
MOV R0, #00H ; R0 = positive count
MOV R1, #00H ; R1 = negative count
MOV R2, #04H ; R2 = Number of numbers to check (4 in this case)
; Start checking each number
CheckNumbers:
MOV A, 30H ; Load the current number into A
INC 30H ; Increment the address (move to next number)
JZ Done ; If we are done, jump to Done
; Check if the number is positive or negative
JC Positive ; Jump to Positive if the number is positive
Negative:
INC R1 ; Increment negative count (R1)
SJMP Continue
Positive:
INC R0 ; Increment positive count (R0)
Continue:
DEC R2 ; Decrement the number of remaining numbers
JNZ CheckNumbers ; Repeat if there are numbers left
Done:
; Store the results
MOV 40H, R0 ; Store the count of positive numbers in memory location 40H
MOV 41H, R1 ; Store the count of negative numbers in memory location 41H
END ; End of the program
Explanation of the Program:
1. Data Initialization:Initialize four numbers in memory locations 30H to 33H, where 30H and
32H are positive, and 31H and 33H are negative.
2. Counters for Positive and Negative Numbers:Initialize R0 to count positive numbers and
R1 to count negative numbers.
3. Loop to Check Each Number:Loop through each number and check whether it's positive or
negative by inspecting the most significant bit (MSB). The JC instruction checks if the
number is positive (no carry), otherwise it is negative.
4. Counting Positive and Negative Numbers:If the number is positive, we increment R0, and
if negative, we increment R1.
5. Storing the Results:Once all numbers have been checked, we store the count of positive
numbers in 40H and negative numbers in 41H.
Example:
Given numbers:
30H = 10H (positive 16)
31H = -20H (negative -32)
32H = 30H (positive 48)
33H = -40H (negative -64)
Output:
Positive numbers: 10H (16) and 30H (48)
Negative numbers: -20H (-32) and -40H (-64)
Thus, the program will output:
Count of positive numbers = 2
Count of negative numbers = 2
These results will be stored in memory locations:
40H = 02H (2 positive numbers)
41H = 02H (2 negative numbers)
Final Output in Memory:
Memory location 40H will store 02H (2 positive numbers).
Memory location 41H will store 02H (2 negative numbers).