This program implements a half adder using the 8051 microcontroller, demonstrating basic digital logic operations.

Description

A half adder adds two single bits and produces a sum and carry output:

  • Inputs: Port 1 (A) and Port 0 (B)
  • Outputs:
    • Sum on P2.0
    • Carry on P3.0

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
ORG 0000H
	
	MOV P1, #0FFH;
	MOV P0, #0FFH;
	
	HERE:	MOV A , P1
		MOV R1 , P0
		XRL A,R1
		RRC A
		MOV P2.0, C
	
		MOV A, P1
		MOV R1 , P0
		ANL A , R1
		RRC A
		MOV P3.0, C
		SJMP HERE
	
END

Explanation

Sum Calculation (A XOR B)

  1. MOV A, P1: Load input A
  2. MOV R1, P0: Load input B into R1
  3. XRL A, R1: XOR operation (A ⊕ B) gives the sum
  4. RRC A: Rotate right through carry to get LSB
  5. MOV P2.0, C: Output sum bit to P2.0

Carry Calculation (A AND B)

  1. MOV A, P1: Load input A again
  2. MOV R1, P0: Load input B into R1
  3. ANL A, R1: AND operation (A • B) gives the carry
  4. RRC A: Rotate right through carry to get LSB
  5. MOV P3.0, C: Output carry bit to P3.0

The program continuously reads inputs and updates outputs in an infinite loop.