flat assembler
Message board for the users of flat assembler.

Index > Windows > bin to string convertion

Thread Post new topic Reply to topic

Joined: 05 Nov 2005
Posts: 86
daluca 02 Aug 2006, 03:49
Hello: this is not a question, i know the binary to decimal conversion is
a well-known subject but i have made my own version of the function
and i want to show it so others can use it or improve it.
it uses the MagicNumber and a few shifts and sub's:

the buffer is like: bufer db 'xxxxxxxxxx',0
the zero is needed

the buffer is filled with the string (no leading zeros)
the function has a rep movsb that moves the characters to the begining
of the buffer even if the buffer is full so maybe you want to insert a
cmp ecx,11 to skip moving the characters to where they allready are.

i use the following sintax:
X is the number to divide by 10
R is the integer result
r is the residue (or modulo,i don't now the exact word in english)
so for instance if X=4152 then R=415 and r=2
       ;EAX number to convert
       ;ESI pointer to a 10-bytes-long,zero terminated buffer
        push ecx
        push edi
        mov ecx,1   ;ecx is the counter
        mov edi,esi
        add esi,10
        push eax
        mov edx,3435973837 ;MagicNumber
        mul edx
        pop eax        ;eax = X
        and dl,$f8     ;edx = 8R
        sub eax,edx    ;eax = X-8R
        shr edx,2      ;edx = 2R
        sub eax,edx    ;eax=X-8R-2R  =  X-(8R+2R) = X-10R = r
        shr edx,1      ;edx=R
        xchg eax,edx   ;now eax=R and edx=r
;-write char:-------dl=(0-9)
        or dl,$30      ;dl=('0'-'9')
        dec esi
        mov [esi],dl
        inc cl
        cmp eax,0
        je .exit
        jmp .divide
        rep movsb
        pop edi
        pop ecx
;EAX = 0
;ESI = one byte ahead of the zero in the zero-terminated-string
Post 02 Aug 2006, 03:49
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.