unsigned long -> ascii conversion

From: alex (me_at_me.com)
Date: 09/18/04


Date: Sat, 18 Sep 2004 05:47:38 +0000 (UTC)

Hi Guys,

I have written a function that will convert a number in the EAX register
into a hex value in ascii, I found this very easy.. by and'ing (and eax,0fh)
each bit and using
it as an offset into a table containing '0123456789abcdef'.. this code seems
to work fine.

but the same trick wont work if i want to convert an unsigned number in EAX
to
its decimal form in ascii, obviously because hex and decimal are differently
based numbering systems..

whats the approach for unsigned number -> ascii?

below is some quick code i whacked together in about 5 mins under DOS
(i know its disorganised), perhaps someone
could see where I need to modify the code to get it to work

this is the hexadecimal -> ascii code

org 0x0100
[bits 16]
start:

 mov edi,padspace
 mov eax,7830h ;ASCII char for '0x'
 stosw
 mov eax,0x200000 ;the actual number we're going to convert
 ror eax,28 ;start from the msb of 32 bit number (will
end up in al)
 mov ecx,0008h ;processing 4 bits at a time, so 8 loops for the
full 32bit

 cvl: push eax
 mov esi,numset
 and eax,000fh
 add esi,eax
 lodsb
 stosb
 pop eax
 rol eax,0004h
 loopnz cvl

 mov esi,padspace
 call writestring
 mov eax,4c00h ;bail
 int 21h

writestring:
mov ax,0e00h
mov bx,0007h
sloop:
lodsb
cmp al,0000h
je finished
int 10h
jmp sloop
finished:
ret

numset db '0123456789ABCDEF000'
padspace times 0x200 db 0



Relevant Pages

  • Re: 8086 ALP TO CONVERT BCD -HEXA DECIMAL
    ... Seems similar to doing ascii to hex (that is, ... and add each digit as we process it. ... How to convert Packed BCD into HEX ??? ... MOV ebx,;or any other source ...
    (comp.lang.asm.x86)
  • Re: Macro2D
    ... õ1, eax", 0 ... db "push striIIi", NL ... db ".2: cmp edi, ebp", NL ... db ".4: mov eax, edi", NL ...
    (alt.lang.asm)
  • One RosAsm Pre-parser
    ... cmp eax 0 | ja L0<< ... mov ecx, D ...
    (alt.lang.asm)
  • Re: Fastcode Library Design
    ... cmp ecx, SMALLMOVESIZE ... lea edx, ... fild qword ptr [eax] ... mov ecx, ...
    (borland.public.delphi.language.basm)
  • Re: Macro2D
    ... mov, eax ... cmp eax, -1 ... push STD_INPUT_HANDLE ...
    (alt.lang.asm)