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