Calling C functions from MASM (x64)
Posted: Wed Feb 13, 2019 6:22 am
I'm really confused as to how calling C functions from MASM works (Visual Studio) on x64...
I have a function I've written in C called 'printf'. If I do the following in MASM, my RSP is changed..
I read on the Microsoft website that the caller should allocate space for 4 qwords (shadow space for the 4 param registers). Well then, I assumed this meant I had to do this:
However, this also modifies my RSP. So, I thought I maybe have to also clean up the stack after the call and this worked:
My question would be: is this last code snippet the RIGHT way of calling a C function from assembly in x64??
EDIT: Also, if this is indeed the right way, how would it go for functions with more than 4 parameters? Would I have to allocate the shadow space before pushing the params or after?
I have a function I've written in C called 'printf'. If I do the following in MASM, my RSP is changed..
Code: Select all
move rcx, format_str
move rdx, argument
call printf
Code: Select all
move rcx, format_str
move rdx, argument
sub rsp, 32
call printf
Code: Select all
move rcx, format_str
move rdx, argument
sub rsp, 32
call printf
add rsp, 32
EDIT: Also, if this is indeed the right way, how would it go for functions with more than 4 parameters? Would I have to allocate the shadow space before pushing the params or after?