Reputation: 397
is there a way to call MessageBox, by using inline assembly?
#include<windows.h>
#pragma comment(lib,"user32.lib");
int main()
{
MessageBox(0,"Hello","Title",1);
return 0;
}
Edit: The links you are providing are great but "Sleep" has only one argument. I need a way to pass multiple arguments to the functions i need to call. Like the MessageBox function.
Upvotes: 2
Views: 1178
Reputation: 9203
I think the comments on the question are pretty clear about how the actual call goes, now lets see how the arguments are passed.
This is a direct question about the Microsoft ABI. It is as follows-
The first 4 arguments are passed in the register %rcx, %rdx, %r8, %r9 and the remaining are passed on the stack. So the rest can be pushed on the stack with the 5th on the top and the 6th below it.
Now there is one peculiarity you have to be careful about. You have to also leave space on the stack for the first 4 arguments. Which means that after call the 5th argument will be after 5 (8 byte) slots (1 for return address and 4 parameters). You can achieve that by doing subq $32, %rsp after pushing the 5th argument.
This extra space is left for the callee to swap the registers out in case it needs to use them.
So for your case,
movq $0, %rcx
movq $hello, %rdx
movq $title, %r8
movq $1, %r9
callq MessageBox
PS: This example is written keeping 64 bit architecture and AT&T syntax. Also the ABI is not the same for *NIX environments. Hope this helps.
EDIT: infact it is better to always leave space on the stack before calling a function with the fast call convention because they might swap the registers and you don't want your stack frame to be overwritten.
Upvotes: 1