;; addition without using the adder
;; computes sum of AH and BH, leaves result in DH
;; (destroys AH and BH)
;;
MOV CL,0 ; count = 0
MOV AL,0 ; carry in = 0
;;
;; the following code (until NEXT) examines the low bits of AH and BH,
;; computes their sum (together with the carry in), and leaves the
;; sum in the high bit of CH and the carry out in the low bit of AL
;;
LOOP: SHR AH,1 ; shift off and test low bit of AH
JC A1
SHR BH,1 ; shift off and test low bit of BH
JC A0B1
CMP AL,1 ; test carry in
JE A0B0CIN1
MOV CH,0 ; if A = 0, B = 0, Cin = 0
MOV AL,0 ; set Sum = 0, Cout = 0
JMP NEXT
A0B0CIN1: MOV CH,1 ; if A = 0, B = 0, Cin = 1
MOV AL,0 ; set Sum = 1, Cout = 0
JMP NEXT
A0B1: CMP AL,1
JE A0B1CIN1
MOV CH,1 ; if A = 0, B = 1, Cin = 0
MOV AL,0 ; set Sum = 1, Cout = 0
JMP NEXT
A0B1CIN1: MOV CH,0 ; if A = 0, B = 1, Cin = 1
MOV AL,1 ; set Sum = 0, Cout = 1
JMP NEXT
A1: SHR BH,1
JC A1B1
CMP AL,1
JE A1B0CIN1
MOV CH,1 ; if A = 1, B = 0, Cin = 0
MOV AL,0 ; set Sum = 1, Cout = 0
JMP NEXT
A1B0CIN1: MOV CH,0 ; if A = 1, B = 0, Cin = 1
MOV AL,1 ; set Sum = 0, Cout = 1
JMP NEXT
A1B1: CMP AL,1
JE A1B1CIN1
MOV CH,0 ; if A = 1, B = 1, Cin = 0
MOV AL,1 ; set Sum = 0, Cout = 1
JMP NEXT
A1B1CIN1: MOV CH,1 ; if A = 1, B = 1, Cin = 1
MOV AL,1 ; set Sum = 1, Cout = 1
JMP NEXT
;;
;; sum bit is in CH; shift left and insert into accumulated sum in DH
;;
NEXT: SHL CH,CL
OR DH,CH
ADD CL,1
CMP CL,8
JB LOOP
HLT
END