Skip to content

szLen from MASM32 Library CopyToAsm Example

mrfearless edited this page Mar 4, 2018 · 3 revisions

Taking the following as an example, if we compile this simple program that makes use of the szLen function of the MASM32 library:

; Compile and link settings:
; \masm32\bin\ml /c /coff szlen.asm
; \masm32\bin\link /SUBSYSTEM:CONSOLE szlen.obj

include     \masm32\include\masm32rt.inc

.data
str1        db 'This is a string',0

.code

start:

    Invoke szLen, Addr str1
    print str$(eax),13,10
    invoke  ExitProcess,0
    ret

END start

Debug this with x64dbg and follow the call to szLen:

Using the CopyToAsm plugin we can copy the selected disassembly to the clipboard and paste the contents into a text file, which will give us this result:

You will notice that the jump instructions have been adjusted for us, and labels inserted at the correct placement for these jumps. Additionally the hex values have been modified to masm compatible (with an 'h' appended and a 0 prefixing any hex values that begin with A-F)

With this code that has been converted for us by the CopyToAsm plugin we can now use this directly in our assembly source if we so desire, for example something like this:

; Compile and link settings:
; \masm32\bin\ml /c /coff szlenctacode.asm
; \masm32\bin\link /SUBSYSTEM:CONSOLE szlenctacode.obj

include     \masm32\include\masm32rt.inc

.data
str1        db 'This is a string',0

.code

start:

    lea eax, str1
    push eax
    call szLengthOfString
    print str$(eax),13,10
    invoke  ExitProcess,0
    ret

szLengthOfString:
    mov eax,dword ptr [esp+4h]
    sub eax,4h

    LABEL_0x00B41147:
    add eax,4h
    cmp byte ptr [eax],0h
    je LABEL_0x00B4117F
    cmp byte ptr [eax+1h],0h
    je LABEL_0x00B41175
    cmp byte ptr [eax+2h],0h
    je LABEL_0x00B4116B
    cmp byte ptr [eax+3h],0h
    jne LABEL_0x00B41147
    sub eax,dword ptr [esp+4h]
    add eax,3h
    ret 4h

    LABEL_0x00B4116B:
    sub eax,dword ptr [esp+4h]
    add eax,2h
    ret 4h

    LABEL_0x00B41175:
    sub eax,dword ptr [esp+4h]
    add eax,1h
    ret 4h

    LABEL_0x00B4117F:
    sub eax,dword ptr [esp+4h]
    ret 4h

END start

In the above code listing we have added our own label szLengthOfString. We then push the offset (address) of the string str1 and then call our version of the szLen function that has been compiled into our program.

Of course there are many other uses for the CopyToAsm plugin, but hopefully it will make it a bit easier to extract any required disassembly to usable masm (or inline asm) code.

Clone this wiki locally