Search This Blog

Loading...

Monday, April 4, 2011

Program to Add 2 16 bit number

This is done using 8 bit registers
--------------------------------

; 2000H = 21H
; 2001H = 12H
; 2002H = 21H
; 2003H = 43H
; RESULT = 1221H + 4321H
; = 5542H
; 2004H = 42H LOWER 2 BYTES
; 2005H = 55H HIGHER 2 BYTES
; METHOD 1


LHLD 2000H ;LOAD FIRST 16 BIT INTO HL PAIR H=12H L=21H
XCHG    ; SAVE IT IN DE PAIR
LHLD 2002H ; LOAD SECOND 16 BIT INTO HL PAIR H=21H L=43H
MOV A, E   ; LOAD LOWER BYTE OF FIRST NUMBER IN ACCUMULATOR
ADD L    ;ADD TWO LOWER BYTES
MOV L, A   ; STORE RESULT IN L. L=42H
MOV A, D   ; LOAD HIGHER BYTE INTO ACCUMULATOR
ADC H    ; ADD 2 HIGHER BYTES ALONG WITH CARRY FROM THE LOWER BYTES
MOV H, A   ;
SHLD 2004H ;
HLT    ;
------------------------------

; METHOD 2
LHLD 2000H ; LOAD FIRST 16 BIT INTO HL PAIR H=12H L=21H
XCHG   ; SAVE IT IN DE PAIR
LHLD 2002H ; LOAD SECOND 16 BIT INTO HL PAIR H=21H L=43H
DAD D   ; ADD 2 16 BIT NUMBERS
SHLD 2004H ; STORE 16 BIT RESULT
HLT   ;

-----------------------------------

No comments:

Post a Comment