BCD to Hex Conversion in Assembly Language
Write a Program For BCD to Hex Conversion in Assembly language .
We are given a five digit BCD number whose HEX equivalent is to be found i.e. 65535 whose HEX equivalent is to be found. First we will find the Hex equivalent of 60,000. We will compare 60,000H with 10,000H. Each time we compare a counter is decremented by 10,000 and we add 10,000 decimal (2710 Hex). Then we will find the equivalent of 5000. Now we will compare 5000H with 1000H. Each time we compare the counter decrements by 1000 and we add 1000 decimal (3E8 H). Then we find the equivalent of 500H by comparing it with 100H. Each time counter decrements by 100 and we add decimal 100 (64 H). Then we find equivalent of 30H by comparing it with 10H. Each time counter decrements by 10 and we add 10 decimal (0A H). Then, equivalent of 5 is 5H.
Finally, all the equivalents obtained are added to get the equivalent of 65535.
Algorithm For BCD to Hex Conversion
Step I : Initialize the data segment.
Step II : Load the MSB of word in register AX.
Step III : Compare it with 0, if zero goto step VII else goto step IV.
Step IV : decrement AX and initialize BX = 0000.
Step V : add 10000 decimal to BX.
Step VI : Jump to step III.
Step VII : Load LSB of word in register AX.
Step VIII : Compare it with 1000, if below go to step XII else go to step IX.
Step IX : subtract 1000 H from AX.
Step X : Add 1000 decimal to BX.
Step XI : Jump to step VIII
Step XII : Compare number in AX now with 100 H, if below go to step XVI, else go to step XIII.
Step XIII : Subtract 100 H from AX.
Step XIV : Add 100 decimal to BX.
Step XV : Jump to step XII.
Step XVI : Compare number in AX with 10H, if below go to step XX, else go to step XVII.
Step XVII : Subtract 10 H from AX
Step XVIII : Add 10 decimal to BX
Step XIX : Jump to step XVI
Step XX : Add contents of AX and BX.
Step XXI : Display the result.
Program For BCD to Hex Conversion Code
1 2 |
.model small <br/> .stack 100 <br/> .data <br/> a dd 00065535h <br/> .code <br/> mov ax, @data ; Intialize data segment <br/> mov ds, ax <br/> mov ax, word ptr a+2 ; checking msb no <br/> mov bx, 0000h ; intialize hex result <br/> l11: cmp ax, 0 ; cmp ax <br/> jz l10 <br/> dec ax ; if ax=1 then it <br/> ; means no>10000 <br/> add bx, 10000 ; so add 10000 to bx <br/> jmp l11 <br/> l10: mov ax, word ptr a ; load lsb part in ax <br/> l2 : cmp ax, 1000h ; if ax>1000h <br/> jb l4 <br/> sub ax, 1000h <br/> add bx, 1000 ; add 1000 to result <br/> jmp l2 <br/> l4 : cmp ax, 100h ; if ax>100h <br/> jb l6 <br/> sub ax, 100h <br/> add bx, 100 ; add 100 to result <br/> jmp l4 <br/> l6 : cmp ax, 10h ; if ax>10h <br/> jb l8 <br/> sub ax, 10h <br/> add bx, 10 ; add 10 to result <br/> jmp l6 <br/> l8 : add bx, ax ; add remainder <br/> ; to result <br/> mov ch, 04h ; Count of digits to be <br/> ; displayed <br/> mov cl, 04h ; Count to roll by 4 bits <br/> mov bx, ax ; Result in reg bx <br/> l2: rol bx, cl ; roll bl so that msb <br/> ; comes to lsb <br/> mov dl, bl ; load dl with data to <br/> ; displayed <br/> and dl, 0fH ; get only lsb <br/> cmp dl, 09 ; check if digit is 0-9 <br/> ; or letter A-F <br/> jbe l4 <br/> add dl, 07 ; if letter add 37H else only add 30H <br/> l4: add dl, 30H <br/> mov ah, 02 ; Function 2 under INT 21H (Display character) <br/> int 21H <br/> dec ch ; Decrement Count <br/> jnz l2 <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 bcd2hex.asm
Turbo Assembler Version 3.0 Copyright (c) 1988, 1991 Borland International
Assembling file: bcd2hex.asm
Error messages: None
Warning messages: None
Passes: 1
Remaining memory: 437k
C:\programs>tlink bcd2hex
Turbo Link Version 3.0 Copyright (c) 1987, 1990 Borland International
C:\programs>bcd2hex
FFFF
Source projectgeek.com