The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on May 14, 2010, 09:20:18 PM

Title: Routine question
Post by: ragdog on May 14, 2010, 09:20:18 PM
Hi

I have a question to this routine

I found a source with a calcution and now wonder me waht make this


pushad
mov edi, [_Dest]
mov eax, [_IN]
mov ebx, 0Ah
xor ecx, ecx
     @@:
xor edx, edx
div ebx
add dl,30h
   push edx
inc ecx
   test  eax,eax
jnz @B
     @@:
pop edx
mov [edi], dl
inc edi
dec ecx
jnz @B
mov byte ptr [edi],0
popad
Title: Re: Routine question
Post by: clive on May 14, 2010, 09:24:27 PM
Converts a 32-bit value into ASCII decimal
Title: Re: Routine question
Post by: ragdog on May 14, 2010, 09:31:09 PM
Ahh ok

A simply way is us wsprintf %d for it
Title: Re: Routine question
Post by: clive on May 14, 2010, 09:52:09 PM
Yes, sprintf(__Dest, "%u", __IN);

I would code it something like this, there are faster ways

pushad ; overkill

mov edi, [_Dest]; Output C string buffer
mov eax, [_IN] ; Input number 32-bit
mov ebx, 10 ; decimal, base 10
push 0 ; push NUL, end of stack/number marked
@@:
xor edx, edx ; Clear upper 32-bits for 64-bit div 32-bit
div ebx ; eax = edx:eax / ebx, edx = edx:eax % ebx
add edx,30h ; edx += '0'; edx is remainder from div 10
push edx ; push char
test eax,eax ; number consumed
jnz @B

@@: ; Unstack the number
pop edx ; pop char
mov [edi], dl ; *p++ = char
inc edi
test edx,edx ; NUL found
jnz @B

popad
Title: Re: Routine question
Post by: qWord on May 14, 2010, 09:59:29 PM
The div can be replaced by an multiplication with the reciprocal - searching the forum you will find several threads about i2a,dw2a...
Title: Re: Routine question
Post by: dedndave on May 14, 2010, 10:38:56 PM
QuoteThe div can be replaced by an multiplication with the reciprocal - searching the forum you will find several threads about i2a,dw2a...

oh yah - and don't forget qWord's magic number program !!!   :bg

http://www.masm32.com/board/index.php?PHPSESSID=45aa5fcf6fed2c10a8876d7ef5cf362b&topic=9937.msg72815#msg72815
Title: Re: Routine question
Post by: qWord on May 14, 2010, 10:52:15 PM
 :bg
for the sake of completeness: macro for division by a constant (magic division) (http://www.masm32.com/board/index.php?topic=12421.0)
Title: Re: Routine question
Post by: dedndave on May 15, 2010, 01:51:35 AM
you can divide by 100,000,000 to extract 8 digits
save the quotient
then, divide that the remainder 10,000 to give you a 4-digit quotient and a 4-digit remainder
convert that remainder to 4 ASCII decimal digits
then convert that quotient to 4 ASCII decimal digits
then, recall the saved quotient and repeat
that is 16 digits - for a 64-bit value, you will only have 4 left

if you search the forum a bit, Drizz, JJ, Paul Dixon, and Lingo all have versions based on that