User forums > Using Code::Blocks

How to call win32 APIs in inlined at&t asm?

<< < (2/2)

ollydbg:
OK, I just notice that the call convension is different in 64bit and 32bit Windows.

So, here is the modified code for 64bit Windows


--- Code: ---#include <iostream>

using namespace std;

char * msg = "Hello, World!\n";
char * wMsg = "Content of the window..";
char * wCaption = "Window title";



int main (){
  std::cout << "before call" << "\n";


   asm(
    "movq $0, %%rcx\n\t"
    "movq %2, %%rdx\n\t"
    "movq %1, %%r8\n\t"
    "movq $0, %%r9\n\t"
    "callq MessageBoxA\n\t"
    :
    : "r" (msg), "r" (wMsg) , "r" (wCaption)
    : "cc");

    std::cout << "after call" << "\n";
}
--- End code ---

The important change is: for 64bit function call, the first 4 arguments are not pushed in the stack, but put in the registers, see some references:

https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention

https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170

https://stackoverflow.com/questions/42488273/call-a-function-with-inline-asm

reverser:

--- Quote from: ollydbg on May 01, 2022, 10:12:31 am ---OK, I just notice that the call convension is different in 64bit and 32bit Windows.

--- End quote ---

thanks a lot. it executed the call but Args are not passed correctly as message box is titled "Error" and is empty.
can you help me with 32bit version too; in x32, it shows the same error i mentioned. i cant make it work. if you cant (as you said here is not the right forum), please PM me the right place of asking this.

regards.

ollydbg:

--- Quote from: reverser on May 07, 2022, 07:48:53 am ---
--- Quote from: ollydbg on May 01, 2022, 10:12:31 am ---OK, I just notice that the call convension is different in 64bit and 32bit Windows.

--- End quote ---

thanks a lot. it executed the call but Args are not passed correctly as message box is titled "Error" and is empty.
can you help me with 32bit version too; in x32, it shows the same error i mentioned. i cant make it work. if you cant (as you said here is not the right forum), please PM me the right place of asking this.

regards.

--- End quote ---

I'm not sure, you can just Google for some asm forum(maybe masm32.com or others). Maybe stackoverflow site is also OK.

reverser:

--- Quote from: reverser on May 07, 2022, 07:48:53 am ---
--- Quote from: ollydbg on May 01, 2022, 10:12:31 am ---OK, I just notice that the call convension is different in 64bit and 32bit Windows.

--- End quote ---

thanks a lot. it executed the call but Args are not passed correctly as message box is titled "Error" and is empty.
can you help me with 32bit version too; in x32, it shows the same error i mentioned. i cant make it work. if you cant (as you said here is not the right forum), please PM me the right place of asking this.

regards.

--- End quote ---
to resolve this problem:
in x86 mode, win32 APIs must be called by their decorated names, preceded by a under score e.g calll _messageBox@16 (16 is the number of bytes that the functions parameters use on stack)

benkenobi01:
You're wasting your time with that, install masm or nasm and do real asm.

Navigation

[0] Message Index

[*] Previous page

Go to full version