flat assembler
Message board for the users of flat assembler.

Index > Main > Binary number to decimal ASCII string conversion

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
THEWizardGenius:

It took me a while to understand this algo, too, but this allowed me to rewrite it from scratch, and in the end I noticed I used one less mul instruction than the original, resulting in faster code. I comented the code in my first post the best I could (its not optimized, for clarity), if you follow the code and see what is happening with a hex capable calculator, I think you will eventually understand it.

This algorithm is based on base conversion of fractions, you can search the web for more information.

And for really criptic code, my best optimized version of this algo:

Code:
magic1  equ     0a7c5ac47h 
magic2  equ     068db8badh 

bin2ascii_test:
        mov     eax,magic1
        mul     esi
        shr     esi,3
        add     esi,eax
        adc     edx,0
        shr     esi,20                  ;separate remainder
        mov     ebx,edx
        shl     ebx,12
        and     edx,0FFFF0000h          ;mask quotient
        and     ebx,0FFFFFFFh           ;remove quotient nibble from remainder.
        mov     eax,magic2
        mul     edx
        add     esi,ebx
        mov     eax,edx
        shr     edx,28
        and     eax,0FFFFFFFh
        lea     esi,[4*esi+esi+5]       ;multiply by 5 and round up
        add     dl,'0'
        mov     ebx,esi
        and     esi,07FFFFFFh
        shr     ebx,27
        mov     [edi],dl
        add     bl,'0'
        lea     eax,[4*eax+eax+5]       ;mul by 5 and round up
        mov     [edi+5],bl
        lea     esi,[4*esi+esi]
        mov     edx,eax
        and     eax,07FFFFFFh
        shr     edx,27
        lea     ebx,[esi+0c0000000h]
        shr     ebx,26              
        and     esi,03FFFFFFh       
        add     dl,'0'              
        lea     eax,[4*eax+eax]     
        mov     [edi+1],dl          
        lea     esi,[4*esi+esi]
        mov     [edi+6],bl
        lea     edx,[eax+0c0000000h]         
        shr     edx,26              
        and     eax,03FFFFFFh       
        lea     ebx,[esi+60000000h] 
        and     esi,01FFFFFFh       
        shr     ebx,25              
        lea     eax,[4*eax+eax]
        mov     [edi+2],dl          
        lea     esi,[4*esi+esi]
        mov     [edi+7],bl
        lea     edx,[eax+60000000h]
        shr     edx,25            
        and     eax,01FFFFFFh     
        lea     ebx,[esi+30000000h]
        mov     [edi+3],dl   
        shr     ebx,24       
        and     esi,00FFFFFFh
        mov     [edi+8],bl   
        lea     edx,[4*eax+eax+30000000h]
        shr     edx,24                  
        lea     ebx,[4*esi+esi+18000000h]
        shr     ebx,23                  
        mov     [edi+4],dl
        mov     [edi+9],bl
        ret              
    


Terje:
I think the Fasm community would be very interested in your posts, if you have some time for that.
Post 13 Sep 2005, 21:34
View user's profile Send private message Reply with quote
Terje Mathisen



Joined: 09 Sep 2005
Posts: 7
Terje Mathisen
I wonder one thing:

You have obviously saved MUL operations by avoiding the need for back-multiplication and subtraction, so just to be certain:

Have you done either a mathematical proof or an exhaustive test to verify that your shortcut does work for all possible 32-bit inputs?

Terje
Post 16 Sep 2005, 09:51
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
Yes, I tested for all 32 bit numbers, by converting to ASCII and back, and comparing with the original value.
In fact, I had to add some code to increase the precision, because the test failed the first time.
Post 16 Sep 2005, 18:45
View user's profile Send private message Reply with quote
Terje Mathisen



Joined: 09 Sep 2005
Posts: 7
Terje Mathisen
Quote:

In fact, I had to add some code to increase the precision, because the test failed the first time.


Right, that was why I asked. If the initial reciprocal MUL had been a bit more prescise, then the fractional result would have been directly usable for the lower 5 digits of the result.

Terje
Post 19 Sep 2005, 10:44
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2145
Location: Estonia
Madis731
64-bit! Smile

I discovered 2 things:
1) lea r32/64,[r32/64+0c0000000h] doesn't work because of the encoding limits. This introduces more code and
2) you can fuse two magics into one possibly reducing code

this may result in a third interesting thing, that you can do INT64_2_ASCII conversion...well, lets see
Post 16 Mar 2007, 15:18
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
What kind of program does enough number to ascii conversions to make this worthwhile?
Post 16 Mar 2007, 15:39
View user's profile Send private message Reply with quote
roboman



Joined: 03 Dec 2006
Posts: 122
Location: USA
roboman
Any program that displays at least one number on the screen and you have more of a need to save a few clock cycles then a few bytes. Coming to mind is nearly any thing that displays changing numbers on the screen in real time (or game time) and will be run on a pc with more ram then you are going to use.

Thanks, very cool code, will have to add that to the collection.
Post 17 Mar 2007, 19:38
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2145
Location: Estonia
Madis731
Already used it, but I hope to make it use all the 64-bit features so I can save a few bytes Very Happy
Where can it be used is of course outputting numbers for users. Internally you don't need that, but you really need to show decimals to an average person who reads the outcome. My application was converting statistics to number format: A,5, B,3, etc.
the zeros can be removed by simple compare loop which is penalty-hungry when not taken care of, but can be fast.
Post 22 Mar 2007, 12:59
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.