Assembly level programs for 8051 Microcontroller

Write Assembly level program (ALP) to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH. Place the sum in R7 and R6. Place LSB in R6.

Assembly Code:

`````` ORG 00H
MOV R1, #0E7H //Load the LSB of 1st number in R1
MOV A, #8DH   //Load the LSB of 2nd number in A
MOV R6,A      //Store Result in R5, i.e. result of adding the LSBs
MOV R2, #3CH  //Load MSB of 1st number in R2
MOV A, #3BH   //Load MSB of 2nd number in Acc
MOV R7,A      // Move the result into R6, i.e. result of adding the MSBs
END
``````

Write ALP to find number of one’s in a given byte.

Assembly Code:

``````     ORG 00H
MOV A,#55H // This is the number whose number of ones need to be counted
MOV R2,#00H // Will Store No of Ones
MOV R3,#08H // No of looping of the counting Subroutine
BACK:   RRC A   // Rotate Accumulator Right with Carry
INC R2  // Increment R2
L1:     DJNZ R3,BACK  // Decrement R3, Jump if not zero
END
``````

Write ALP to find the sum of numbers stored in RAM. Assume your own appropriate BCD numbers and RAM locations. Put the result in BCD.

Assembly Code:

`````` ORG 00H
MOV R1,#99H //Store 20H in RAM Location 30H
MOV R2,#99H //Store 22H in RAM location 32H
MOV A,R1     //Load Acc with content from 30H
MOV B,R2    //Load Register B with content from 32H
DA A
MOV R6,A
END
``````

Write an ALP to illustrate Multiplication and Division instructions in 8051.

Assembly Code:

`````` ORG 00H
MOV A,#0A5H // Load Accumulator with a number 05
MOV B,#04H // Load register with a number 04
MUL AB     // Multiply content of A with B
MOV R3,A   // Store LSB of Result in R3
MOV R4,B   // Store MSB of result in R4
MOV A,#20H
MOV B,#09H
DIV AB
MOV R5,A   // Store quotent in R5
MOV R6,B   // Store Remainder in R6
END
``````

Write an ALP to multiply 16-bit numbers (word) in 8051. Take 8C45H and 235AH and place the product in RAM locations 50H to 54H.

Assembly Code:

`````` ORG 00H
MOV A,#45H
MOV B,#5AH
MUL AB
MOV 31H,A
MOV 30H,B

MOV A,#5AH
MOV B,#8CH
MUL AB
MOV 32H,B
MOV 33H,A

MOV A,#23H
MOV B,#45H
MUL AB
MOV 35H,A
MOV 34H,B

MOV A,#23H
MOV B,#8CH
MUL AB
MOV 37H,A
MOV 36H,B

MOV A,31H
MOV 54H,A //LSB of Result (1st Byte)

MOV A,30H
MOV 53H,A //(2nd Byte)

MOV A,32H
MOV 52H,A //(3rd Byte)

MOV A,36H
MOV 51H,A //(4th Byte)

MOV A,#0H
MOV 50H,A //(5th Byte, if any)

END
``````

Write a program to transfer value #ABh serially via 3rd pin or PORT 2. Send the MSB first.

Assembly Code:

`````` ORG 00H
MOV A,#0ABH
MOV R0,#08H
X1: RLC A
MOV P2.2,C
DJNZ R0,X1
END
``````

Write an ALP to convert Packed BCD to two ASCII numbers.

Assembly Code:

`````` ORG 00h
MOV A, #35H
MOV R1, A
ANL A, #0FH
ORL A, #30H
MOV R6, A

MOV A, R1
ANL A, #0F0H
RR A
RR A
RR A
RR A
ORL A, #30H
MOV R7, A
END
``````

Write an ALP to toggle the LED connected to P2.6 every 10ms. The crystal frequency is 11.0592MHz.

Assembly Code:

``````;If we use 11.0592MHz crystal ..
;We know 8051 takes 12 pulses for 1 machine cycle
;then period of one machine cycle would be 11.0592MHz/12 = 921.6KHz
;t = 1/f so time period of one machine cycle = 1/ 921.6KHz = 1.08506944444us
;921.6 machine cycles will produce 1 ms
;no of loops = 921.6/4=230

ORG 000H
MAIN:   SETB P2.6
ACALL DELAY1
CLR P2.6
ACALL DELAY1
SJMP MAIN
DELAY1: MOV R2,#20D
LABEL:  ACALL DELAY
DJNZ R2,LABEL
RET
DELAY:  MOV R5,#248D
MOV R6,#248D
LOOP1:  DJNZ R5,LOOP1
LOOP2:  DJNZ R6,LOOP2
RET
END
``````