flat assembler
Message board for the users of flat assembler.

Index > Windows > bin to string convertion

Author
Thread Post new topic Reply to topic
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
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
Code:
       ;EAX number to convert
       ;ESI pointer to a 10-bytes-long,zero terminated buffer
bin2str:
        push ecx
        push edi
        mov ecx,1   ;ecx is the counter
        mov edi,esi
        add esi,10
;-------div10
.divide:
        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
;-------------------
.exit:
        cld
        rep movsb
        pop edi
        pop ecx
        ret
;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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.