1

Hello I'm trying to compile this program written for MASM:

.686
.xmm
.model flat, stdcall
OPTION CaseMap:None

include ../masm32/libs/windows.inc
include ../masm32/libs/kernel32.inc
includelib ../masm32/libs/kernel32.lib
include ../masm32/libs/user32.inc
includelib ../masm32/libs/user32.lib
includelib ../masm32/libs/msvcrt.lib

EXTERN printf:PROC

.data
    string1 db "Prova Prova Prova",0


.code

main PROC
    push ebp
    mov ebp,esp

    mov eax, offset string1
    push eax

    call printf

    pop ebp
    ret

main ENDP
    END

I'm making the object file with: jwasm -coff printf.asm

but when I try to compile with: i686-w64-mingw32-g++ -m32 printf.o

I get the following error:

Warning: .drectve -defaultlib:../masm32/libs/kernel32.lib ' unrecognized Warning: .drectve -defaultlib:../masm32/libs/user32.lib ' unrecognized Warning: corrupt .drectve at end of def file /usr/bin/i686-w64-mingw32-ld: /usr/lib/gcc/i686-w64-mingw32/10-win32/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): in function main': ./build/i686-w64-mingw32-i686-w64-mingw32-crt/./mingw-w64-crt/crt/crt0_c.c:18: undefined reference to WinMain@16' collect2: error: ld returned 1 exit status

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Z3R0
  • 1,011
  • 10
  • 19
  • 3
    You seem to be trying to mix the MASM32 libraries with G++'s own version of the libraries. G++ probably doesn't seem to support the `includelib` entries in the object file. The C library has C calling convention so you will want `.model flat, C` instead of stdcall and you will have to remove the values pushed on the stack after calling `printf`. As well comment out the `includelib` lines (keep the `include` lines as is). – Michael Petch Aug 06 '22 at 02:25
  • 1
    It should be noted that JWASM isn't the problem, it is that the linker LD that GCC/G++ uses is not the Microsoft linker which can properly parse the includelib info that was embedded in the object file. – Michael Petch Aug 06 '22 at 03:43
  • Thank you for your answer, it start now but how can I pass the string to print to "printf" if I need to remove "push eax"? I though "push eax" was needed to pass the string as a parameter to printf. Thank you – Z3R0 Aug 06 '22 at 07:15
  • OK I made it works with the "push eax", I was missing "invoke ExitProcess, 0" at the end. Thank you! – Z3R0 Aug 06 '22 at 07:21
  • 1
    You still push parameters on the stack for the `printf` call, but when you finish you pop them off or you just add the number of bytes pushed back to ESP. So you could do `add esp, 4` after the call to `printf` since you pushed 4 bytes on the stack when you pushed EAX. If you had added `add ESP, 4` the `ret` should have worked without calling ExitProcess. – Michael Petch Aug 06 '22 at 07:25
  • 1
    If you pushed 2 parameters on you would do `add esp, 8` after the call to `printf` to rebalance the stack and get rid of the parameters. – Michael Petch Aug 06 '22 at 07:35

0 Answers0