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
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

bin2ascii_test:
mov     eax,magic1
mul     esi
shr     esi,3
shr     esi,20                  ;separate remainder
mov     ebx,edx
shl     ebx,12
and     ebx,0FFFFFFFh           ;remove quotient nibble from remainder.
mov     eax,magic2
mul     edx
mov     eax,edx
shr     edx,28
and     eax,0FFFFFFFh
lea     esi,[4*esi+esi+5]       ;multiply by 5 and round up
mov     ebx,esi
and     esi,07FFFFFFh
shr     ebx,27
mov     [edi],dl
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
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.
13 Sep 2005, 21:34
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
16 Sep 2005, 09:51
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.
16 Sep 2005, 18:45
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
19 Sep 2005, 10:44

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

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
16 Mar 2007, 15:18
Goplat

Joined: 15 Sep 2006
Posts: 181
Goplat
What kind of program does enough number to ascii conversions to make this worthwhile?
16 Mar 2007, 15:39
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.
17 Mar 2007, 19:38

Joined: 25 Sep 2003
Posts: 2145
Location: Estonia
Already used it, but I hope to make it use all the 64-bit features so I can save a few bytes
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.
22 Mar 2007, 12:59
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum