Here is my solution of the classic problem for conversion number to string in any radix.
[size=12]

;******************************************
; Use with stdcall parameter passing. FASM syntax.
; X - dword number to convert
; pStr - pointer to the string buffer
; Radix - between 2 and approx 36
;******************************************

proc NumToStr,X,pStr,Radix

enter
push ecx eax edi

mov ecx,[Radix] ; radix. Must be <= ff
mov eax,[X]
mov edi,[pStr]

test eax,eax
jns .DecLoop

mov byte [edi],'-'
inc edi
neg eax

.DecLoop:
stdcall DoDivCX,eax

pop edi eax ecx
return

proc DoDivCX,Num
enter
push edx

xor edx,edx
div ecx
test eax,eax
jz .L1
stdcall DoDivCX, eax
.L1:
mov al,dl
cmp al,10
sbb al,69h
das

mov [edi],ax
inc edi
.exit:
pop edx
return
[/SIZE]
Posted on 2002-11-22 02:24:29 by JohnFound
Aha, a very little interest :(
Normal! This is not the best solution.
But I optimized it.
This is "v2.0" :)


[size=12]
;************************************
; NumToStr:
; Converts signed integer value to string.
; NumToStrU:
; Converts unsigned integer value to string.
;
; EDI - pointer to string buffer
; EAX - Number to convert
; ECX - radix from 2 to $ff
;
; Note: Don't use 1 as radix. :)
;************************************

NumToStr:
test eax,eax
jns NumToStrU
neg eax
mov byte [edi],'-'
inc edi

NumToStrU:
cmp eax,ecx
jb .less

xor edx,edx
div ecx
push edx
call NumToStrU
pop eax

.less:
cmp al,$0a
sbb al,$69
das

mov [edi],ax
inc edi
ret
[/size]
Posted on 2002-11-22 12:24:06 by JohnFound
Why?
	mov	[edi],ax

inc edi
stosb

or,

mov , al
inc edi

Never mind. :) I see this is NUL terminating.
It is a nice small general algo.
Posted on 2002-11-22 12:51:00 by bitRAKE