Skip to content
Related Articles

Related Articles

Addition of Two 8 Bit Numbers in 8051 Microcontroller Using Ports

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Jun, 2022
Improve Article
Save Article

8051 microcontroller is a microcontroller designed by Intel in 1981. It is an 8-bit microcontroller with 40 pins DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2 16-bit timers. It consists of four parallel 8-bit ports, which are programmable as well as addressable as per the requirement.

Problem: To write an assembly language program to add two 8 bit numbers in 8051 microcontroller using ports.


Block diagram:


  • Initialize Ports P0 and P1 as input ports.
  • Initialize Ports P2 and P3 as output ports.
  • Initialize the R1 register.
  • Move the contents from Port 0 to B register.
  • Move the contents from Port 1 to A register.
  • Add contents in A and B.
  • If carry is present increment R1.
  • Move contents in R1 to Port 2.
  • Move the sum in step 6 to Port 3.


ORG 00H                   // Indicates starting address

MOV P0,#0FFH              // Initializes P0 as input port
MOV P1,#0FFH              // Initializes P1 as input port
MOV P2,#00H               // Initializes P2 as output port
MOV P3,#00H               // Initializes P3 as output port

L1:MOV R1, #00H          // Initializes Register R1
MOV B,P0                 // Moves content of P0 to B
MOV A,P1                 // Moves content of P1 to A
CLR C                    // Clears carry flag
ADD A,B                  // Add the content of A and B and store result in A
JNC L2                   // If carry is not set, jump to label L2
INC R1                   // Increment Register R1 if carry present

L2: MOV P2, R1            // Moves the content from Register R1 to Port2
MOV P3,A                 // Moves the content from A to Port3
SJMP L1                  // Jumps to label L1


  • ORG 00H is the starting address of the program.
  • Giving the values as #0FFH and #00H initializes the ports as input and output ports respectively.
  • R1 register is initialized to 0 so as to store any carry produced during the sum.
  • MOV B, P0 moves the value present in P0 to the B register.
  • MOV A, P1 moves the value present in P1 to Accumulator.
  • ADD AB adds the values present in Accumulator and B register and stores the result in Accumulator.
  • JNC L2 refers to jump to label L2 if no carry is present by automatically checking whether the carry bit is set or not.
  • If the carry bit is set to increment register R1.
  • MOV P2, R1, and MOV P3, A refers to moving the carry bit to P2 and result in Accumulator to P3.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!