hii
i'm looking for away to determine how many chars do i need to print a dword

e.g 64h = 100 dec 3 chars
64h*Ah = 1000 4 chars
and so on ..
so
``````

mov edx,thedword
mov ecx,1
xor eax,eax
@@:
;lea ecx,[ecx*4+ecx]
imul ecx,0Ah
inc eax
cmp edx,ecx
jge @B
``````
eax gives the needed len
there must be better ideas i'm extermly tired right now and my brain starting to cut the power :)

bye
thanks
eko
Posted on 2004-11-19 13:06:54 by eko
hii
i'm looking for away to determine how many chars do i need to print a dword

e.g 64h = 100 dec 3 chars
64h*Ah = 1000 4 chars
and so on ..
so
``````

mov edx,thedword
mov ecx,1
xor eax,eax
@@:
;lea ecx,[ecx*4+ecx]
imul ecx,0Ah
inc eax
cmp edx,ecx
jge @B
``````
eax gives the needed len
there must be better ideas i'm extermly tired right now and my brain starting to cut the power :)

bye
thanks
eko

The max value you can have for a dword is 0FFFFFFFFh = 4,294,967,295. Counting digits in the base 10 version gives 10 digits.
Posted on 2004-11-19 13:58:59 by mark_larson
max dword value is 4294967295, so your max chars is 10.
Posted on 2004-11-19 14:53:36 by Qages
As a few have already answered, you should reserve at least 10 bytes. However, if you consider the DWORD as a signed dword, you could then need as many as 11. Furthermore, you may also want to end it with a "0" byte to have a "null-terminated" string representation. My recommendation would thus be a 12-byte buffer.

Raymond
Posted on 2004-11-19 20:53:10 by Raymond
hi all
of course the max len is 10(withoutthenull)
but i need cases when the len is not 10
e.g
eax=1
so i only need two bytes(with thenull)
hope this makes it more clear

any better ideas than my loop above?

bye

thanks eko
Posted on 2004-11-20 14:30:34 by eko
of course the max len is 10(withoutthenull)
but i need cases when the len is not 10
Why???
It couldn't be for allocating movable memory which is certainly not allocated in chunks smaller than 16 bytes.
It couldn't be for allocating the size of a LOCAL variable in a procedure; the maximum possible is the only option.
It couldn't be for declaring the size of a buffer in the .data section; you can't resize that buffer on the run.

The size of the string can easily be determined after its conversion into a large enough buffer if such size is required for other reasons.

Raymond
Posted on 2004-11-20 18:12:42 by Raymond
Well, he could be calculating column sizes for a table of numbers to be displayed on screen, or something.

I think the multiply loop is the quickest way. You could try this, but it would probably be slower:
``````
mov ecx,[thedword]
jecxz zero
fldlg2
sar ecx,31
fild dword ptr [thedword]
fabs
neg ecx
fyl2x
fistp dword ptr [length]