Hex to BCD Conversion in Assembly Language Code
Write a Program For Hex to BCD Conversion in Assembly language .
We have a 4 digit Hex number whose equivalent binary number is to be found i.e. FFFF H. Initially we compare FFFF H with decimal 10000 ( 2710 H in Hex ). If number is greater than 10,000 we add it to DH register. Also, we subtract decimal 10,000 from FFFF H, each time comparison is made. Then we compare the number obtained in AX by 1000 decimal. Each time we subtract 1000 decimal from AX and add 1000 decimal to BX. Then we compare number obtained in AX by 100 decimals. Each time we subtract 100 decimal from AX and add 100 decimal to BX to obtain BCD equivalent. Then we compare number obtained in AX with 10 decimal. Each time we subtract 10 decimal from AX and we add 10 decimal to BX. Finally we add the result in BX with remainder in AX. The final result is present in register DH with contains the 5th bit if present and register AX.
Algorithm For Hex to BCD Conversion
Step I : Initialize the data segment.
Step II : Initialize BX = 0000 H and DH = 00H.
Step III : Load the number in AX.
Step IV : Compare number with 10000 decimal. If below goto step VII else goto step V.
Step V : Subtract 10,000 decimal from AX and add 1 decimal to DH
Step VI : Jump to step IV.
Step VII : Compare number in AX with 1000, if below goto step X else goto step VIII.
Step VIII : Subtract 1000 decimal from AX and add 1000 decimal to BX.
Step IX : Jump to step VII.
Step X : Compare the number in AX with 100 decimal if below goto step XIII
Step XI : Subtract 100 decimal from AX and add 100 decimal to BX.
Step XII : Jump to step X
Step XIII : Compare number in AX with 10. If below goto step XVI
Step XIV : Subtract 10 decimal from AX and add 10 decimal to BX..
Step XV : Jump to step XIII.
Step XVI : Add remainder in AX with result in BX.
Step XVII : Display the result in DH and BX.
Program For Hex to BCD Conversion Code
1 2 |
.model small <br/> .stack 100 <br/> .code <br/> mov ax, 0ffffh ; hex number to find it's bcd <br/> mov bx, 0000 <br/> mov dh, 0 <br/> l9 : cmp ax, 10000 ; if ax>10000 <br/> jb l2 <br/> sub ax, 10000 ; subtract 10000 <br/> inc dh ; add 1 to dh <br/> jmp l9 <br/> l2 : cmp ax, 1000 ; if ax>1000 <br/> jb l4 <br/> sub ax, 1000 <br/> add bx, 1000h ; add 1000h to result <br/> jmp l2 <br/> l4 : cmp ax, 100 ; if ax>100 <br/> jb l6 <br/> sub ax, 100 <br/> add bx, 100h ; add 100h to result <br/> jmp l4 <br/> l6 : cmp ax, 10 ; if ax>10 <br/> jb l8 <br/> sub ax, 10 <br/> add bx, 10h ; add 10h to result <br/> jmp l6 <br/> l8 : add bx, ax ; add remainder <br/> ; to result <br/> mov ah, 02 <br/> mov cx, 0204h ; Count to display <br/> ; 2 digits <br/> go: rol dh, cl <br/> mov dl, dh <br/> and dl, 0fh <br/> add dl, 30h ; display 2 msb digits <br/> int 21h <br/> dec ch <br/> jnz go <br/> mov ch, 04h ; Count of digits to be <br/> ; displayed <br/> mov cl, 04h ; Count to roll by 4 bits <br/> l12: rol bx, cl ; roll bl so that msb <br/> ; comes to lsb <br/> mov dl, bl ; load dl with data to be <br/> ; displayed <br/> and dl, 0fH ; get only lsb <br/> cmp dl, 09 ; check if digit is 0-9 or letter A-F <br/> jbe l14 <br/> add dl, 07 ; if letter add 37H else only add 30H <br/> l14: add dl, 30H <br/> mov ah, 02 ; Function 2 under INT 21H (Display character) <br/> int 21H <br/> dec ch ; Decrement Count <br/> jnz l12 <br/> mov ah, 4cH ; Terminate Program <br/> int 21H <br/> end |
How to Run this Program
For Running this program you should have installed Tasm on you computer . If you have not installed Tasm yet please install from Here .
C:\programs>tasm hex2bcd.asm
Turbo Assembler Version 3.0 Copyright (c) 1988, 1991 Borland International
Assembling file: hex2bcd.ASM
Error messages: None
Warning messages: None
Passes: 1
Remaining memory: 437k
C:\programs>tlink hex2bcd
Turbo Link Version 3.0 Copyright (c) 1987, 1990 Borland International
C:\programs>hex2bcd
065535
Source projectgeek.com