Dman95 / SASM

SASM - simple crossplatform IDE for NASM, MASM, GAS and FASM assembly languages

Home Page:http://dman95.github.io/SASM/

Repository from Github https://github.comDman95/SASMRepository from Github https://github.comDman95/SASM

Can you explain how to call C functions on Windows?

Michael-Milligan opened this issue · comments

Sorry for maybe stupid question, but how am I supposed to call C functions on Windows? I tried all of the following:

  • declaration with underscore and CEXTERN and call: caused error in call, not in CEXTERN
  • declaration with underscore and CEXTERN and call without undrescore: no errors in build, but crash on call (this may be my fault, so pls tell me if this is the right option)
  • declaration and call without underscore: the same as 2 (so maybe again my mistake, but I don't think if underscore wasn't necessary you would put it in help)
    This is the essential sample of code I tried
    CEXTERN _printf
    mov rax, 0
    mov rdi, msg2
    mov rsi, 0
    call printf
    I understand that I can miss smth in options or smth else, so please tell if it's the case

I don't think this question belongs to SASM issue tracker, because it is not really about SASM. But since I've already read it, I can as well answer.

The correct way to use CEXTERN on Windows is without underscores, both declaration and call. The whole purpose of CEXTERN macro is to make the same code work on both Windows and Linux. The macro adds the aforementioned underscore and a %define.

The macro was initially designed for IA-32, and doesn't work nearly that well on AMD64, because Linux and Windows use different calling conventions (i.e. they use different registers to pass arguments to the function). The code snippet you've shown clearly uses System V AMD64 ABI, which is used on Linux, but not on Windows. This is the likely source of you crash on call.

Thanks, I'll try to search for a solution now when I have more specified area of research

Thank you for help, it was indeed problem in my parameters that I was passing to printf