Search code examples
assemblyx86masm

Convert byte to string in x86 assembly language


In x86 assembly language, is there any efficient way to convert a byte to a string of binary digits (represented as a byte array of 0s and 1s)? As far as I know, there isn't any 'toString' function in x86 assembly, as in most high-level programming languages.

.stack 2048

.data
theString byte 0, 0, 0, 0, 0, 0, 0, 0 ;store eax as a binary string here.
ExitProcess proto, exitcode:dword 

.code
start:
mov eax, 3;
;now I need to convert eax to a binary string somehow (i. e., a byte array of 0s and 1s)
invoke  ExitProcess, 0
end start

Solution

  • Was it that hard?:

    .data
    mystr db 33 dup(0)
    
    .code
    
    EaxToBinaryString:
        mov     ebx, offset mystr
        mov     ecx, 32
    EaxToBinaryString1:
        mov     dl, '0' ; replace '0' with 0 if you don't want an ASCII string
        rol     eax, 1
        adc     dl, 0
        mov     byte ptr [ebx], dl
        inc     ebx
        loop    EaxToBinaryString1
        ret