News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

BCD to ASCII

Started by mnsrl, April 08, 2008, 09:51:51 AM

Previous topic - Next topic

mnsrl

The no. 2520529 is stored as packed BCD (2 dits / byte). How to convert into ASCII and display on screen using MASM32 StdOut function.

Jimg

Quote from: mnsrl on April 08, 2008, 09:51:51 AM
The no. 2520529 is stored as packed BCD (2 dits / byte). How to convert into ASCII and display on screen using MASM32 StdOut function.
A lot depends upon the possible number of bcd digits, the termination of a string of bcd digits, etc.

But the general operation is to load two bcd digits into a byte register, convert it into two bytes, convert each byte into ascii, and store-


.data
source db 00100101b,00100000b,01010010b,10010000b
.data?
destination db 20 dup (?)
.code

If you have to do a great number of these and speed is important, a table lookup might be faster.
    mov ecx,7   ; nibble count or bcd digit count
    mov esi,offset source       ; input bcd
    mov edi,offset destination  ; output ascii
    .repeat
        lodsb           ; get two nibbles of the bcd string
        mov ah,al       ; get a copy
        shr ah,4        ; convert upper nibble
        add ah,30h      ; convert to ascii
        mov [edi],ah    ; save first nibble as ascii
        inc edi
        dec ecx
        .break .if zero?
        and al,0fh      ; mask off lower nibble
        add al,30h      ; convert to ascii
        stosb
        dec ecx
    .until zero?
    mov byte ptr [edi],0    ; ascii terminator
    invoke MessageBox,0,addr destination,0,0


kermit

Hi,

if ur number is a dword and u don't mind using masm's hl macros this might be a solution: :8)
include /masm32/include/masm32rt.inc
.data
number dd 12345678h
.code
start:
print xdword$(number)
ret
end start