NO

Author Topic: Convert in Binary  (Read 437 times)

Offline HellOfMice

  • Member
  • *
  • Posts: 362
  • Never be pleased, always improve
Convert in Binary
« on: January 01, 2025, 06:03:55 PM »

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

Offline Vortex

  • Member
  • *
  • Posts: 976
    • http://www.vortex.masmcode.com
Re: Convert in Binary
« Reply #1 on: January 01, 2025, 07:20:36 PM »
Hi Philippe,

Could you check if you need PARMAREA=4*QWORD since your function does not call any other function(s)?
Code it... That's all...

Offline HellOfMice

  • Member
  • *
  • Posts: 362
  • Never be pleased, always improve
Re: Convert in Binary
« Reply #2 on: January 01, 2025, 07:28:53 PM »
You are right I should remove the PARMAREA

How do you understand this line:

cmp [rsi],0

for me it is "COMPARE THE QWORD POINTED TO BY RSI,0"
With POASM it is FALSE it compares to a DWORD
I added CMP QWORD PTR [RSI],0
and it worked

It was used to find the end of pointers on strings

Thank You Vortex for the remarks and Happy New year to the people with whom I exchange since a long time: John, Vortex & Timo
--------------------------------
Kenavo

Offline Vortex

  • Member
  • *
  • Posts: 976
    • http://www.vortex.masmcode.com
Re: Convert in Binary
« Reply #3 on: January 01, 2025, 07:50:15 PM »
Hi Philippe,

You are welcome. This line should be OK :

Code: [Select]
CMP QWORD PTR [RSI],0
Code it... That's all...