8086 program to Add two 16 bit BCD numbers



ØØ       Explanation :
·            Consider that two words are available in registers AX and BX. We have to add these two words.
·            Using add instruction, add the contents, of the lower two bits i.e. add AL and BL.
·            The result of this addition is stored in the AL register.
·            DAA instruction is then used to convert the result to valid BCD. Now, add the contents of MSB alongwith carry if generated in LSB addition.

·            The result of MSB addition is stored in the AH register. Adjust this result to valid BCD number. The final result is available in the BX register.
·            The DAA instruction operates only on the AL register and so here, we have to add LSB and MSB separately without using ADD AX, BX.
                        eg. :  AX = 3629 BCD
                                  BX = 4738 BCD
Step I :

2
9


+
3
8



6
1
and auxiliary carry = 1
                                      As    AC    =   1, add 6 to make result valid.
                                         61 + 6    =   67
Step II :   36 + 47 + 0 (Carry of LSB) = 7 D.
                      Lower nibble of addition is greater than 9, so add 6.

7
D
+
0
6

8
3
                      Final result : 8367 BCD.
ØØ       Algorithm :
Step I         :   Initialize the data memory.
Step II       :   Load the first number into AX register.
Step III      :   Load the second number into BX register.
Step IV      :   Add two lower digits.
Step V        :   Adjust result to valid BCD number.
Step VI      :   Store the result in BL.
Step VII    :   Add the two upper digits with carry.
Step VIII   :   Adjust result to valid BCD number.
Step IX      :   Store the result in BH.
Step X       :   Display the result.
Step XI      :   Stop.
ØØ       Flowchart : Refer flowchart 


ØØ       Program :
.model small
.data
a dw 3629H
b dw 4738H
.code
       mov     ax, @data      ; Initialize data section
       mov     ds, ax
       mov     ax, a             ; Load number1 in ax
       mov     bx, b            ; Load number2 in bx
       add      al, bl             ; add lower two digits. Result in al
       daa                         ; adjust result to valid bcd
       mov     bl, al             ; store result in bl
       adc      ah, bh           ; add upper two digits. Result in ah
       mov     al, ah            ; al=ah as daa works on al only
       daa                         ; adjust result to valid BCD
       mov     bh, al            ; store result in bh                    
       mov     ch, 04h         ; Count of digits to be displayed
       mov     cl, 04h ; Count to roll by 4 bits
l2:    rol       bx, cl            ; roll bl so that msb comes to lsb
       mov     dl, bl            ; load dl with data to be displayed
       and      dl, 0fH ; get only lsb
       cmp     dl, 09           ; check if digit is 0-9 or letter A-F
       jbe       l4
       add      dl, 07           ; if letter add 37H else only add 30H
l4:    add      dl, 30H
       mov     ah, 02           ; Function 2 under INT 21H (Display character)
       int       21H
       dec      ch                ; Decrement Count
       jnz       l2
       mov     ah, 4cH         ; Terminate Program
       int       21H
       end




ØØ       Result :
C:\programs>tasm addbcd16.asm
Turbo Assembler  Version 3.0  Copyright (c) 1988, 1991 Borland International
Assembling file:   addbcd16.asm
Error messages:    None
Warning messages:  None
Passes:            1
Remaining memory:  438k
C:\programs>tlink addbcd16
Turbo Link  Version 3.0 Copyright (c) 1987, 1990 Borland International
Warning: No stack
C:\programs>addbcd16
8367
C:\programs>