A small rutine to convert a number in binary on 64 bits max
; **********************************************************************************
; ***************************** Bin ************************************************
; **********************************************************************************
ALIGN 16
Bin PROC USES RBX RDI RSI,__qwNumber:QWORD,__lpszResult:LPSTR,__dwOutputLength:DWORD PARMAREA=4*QWORD
test rcx,rcx
jz @BadArg ; __qwNumber == 0 => ERROR
test rdx,rdx ; _lpszResult == NULL => ERROR
jz @BadArg
test r8d,r8d ; __dwOutputLength == 0 => ERROR
jz @BadArg
cmp r8d,64 ; > 64 bits => ERROR
jg @BadArg
mov rsi,rdx
mov rbx,rcx ; Save the number
mov al,'0' ; Fills the resulting string witl all '0'
mov rdi,rsi
mov ecx,r8d
rep stosb
xor al,al ; Set '\0' at the end of the string
stosb
sub r8d,1 ; Position - 1
add rdx,r8 ; End of the string
xor al,al ; 0
; ===========================================================================================
; ===========================================================================================
ALIGN 16
@Loop :
shr rbx,1 ; Shift the leftmost right bit to CARRY
adc BYTE PTR [rdx],al ; We add CARRY to get '1' if it is set
test rbx,rbx
jz @EndLoop ; If the number == 0 => Finished
sub rdx,1
sub r8d,1 ; Counter--
jnz @Loop ; Continue if > 0
mov rax,rsi ; Return the result string
ret
; ===========================================================================================
; ===========================================================================================
ALIGN 16
@EndLoop :
mov rax,rsi ; We did not loop until the end of the counter
ret ; Return the result string
; ===========================================================================================
; ===========================================================================================
ALIGN 16
@BadArg :
xor eax,eax ; The number of positions was > 64
ret ; A pointer was NULL or the input value was == 0
Bin ENDP