Search This Blog

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   ;

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

9 comments:

  1. I'm really a beginner at this thing and correct me if I am wrong but in method 1 when you load the second input (4321) H=43 l=21 but you have wrote the opposite why?

    ReplyDelete
    Replies
    1. Well sleep deprivation make people do crazy things.... I probably should thank god that In my case it was just this typo in the comment.
      On a serious note i don't actually remember, seems to me while testing on the emulator i might have changed the value or something....
      Apologies cannot help you with this.

      Delete