;; addition without using the adder
;; computes sum of AH and BH, leaves result in DH
;; version 2: does not use ADD, SUB, or CMP
;; (destroys AH and BH)
;;
MOV CL,0FFH ; use CL to count 8 iterations
; (we shift off one bit on each iteration,
; and continue until CL is 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
SHR AL,1 ; test carry in
JC A0B0CIN1
MOV CH,0 ; if A = 0, B = 0, Cin = 0
MOV AL,0 ; set Sum = 0, Cout = 0
JMP NEXT
A0B0CIN1: MOV CH,80H ; if A = 0, B = 0, Cin = 1
MOV AL,0 ; set Sum = 1, Cout = 0
JMP NEXT
A0B1: SHR AL,1
JC A0B1CIN1
MOV CH,80H ; 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
SHR AL,1
JC A1B0CIN1
MOV CH,80H ; 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: SHR AL,1
JC A1B1CIN1
MOV CH,0 ; if A = 1, B = 1, Cin = 0
MOV AL,1 ; set Sum = 0, Cout = 1
JMP NEXT
A1B1CIN1: MOV CH,80H ; if A = 1, B = 1, Cin = 1
MOV AL,1 ; set Sum = 1, Cout = 1
JMP NEXT
;;
;; sum bit is in CH (high bit); insert into accumulated sum in DH
;;
NEXT: SHR DH,1
OR DH,CH
SHR CL,1
JNZ LOOP
HLT
END