Click here to Skip to main content
15,394,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

i want to learn asm inlining using codeblocks+GCC(MinGW32) and i came across this problem.
this is my code:
C++
#include <iostream>
#include <Windows.h>
#include <winuser.h>

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

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

   asm(
"movl _msg,%eax\n\t"
"pushl %eax\n\t"
"calll _printf\n\t"
"popl %eax\n\t"
"movl $0, %eax\n\t"
"movl _wCaption, %ebx\n\t"
"movl _wMsg, %ecx\n\t"
"movl $0, %edx\n\t"
"pushl %eax\n\t"
"pushl %ebx\n\t"
"pushl %ecx\n\t"
"pushl %edx\n\t"
"calll MessageBoxA\n\t"
"popl %edx\n\t"
"popl %ecx\n\t"
"popl %ebx\n\t"
"popl %eax\n\t"
      );

    std::cout << "after call" << "\n";
}

and when i want to compile, i get:
HTML
undefined reference to `MessageBoxA'


i think its config related; because when using MinGW64, this below code compiles just fine:
C++
#include <iostream>

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

char * const wCaption222 = "Window title";

int var = 0;


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


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

    std::cout << var << "\n";
    std::cout << "after call" << "\n";
}


i used "msys2-x86_64" to install MinGW32 and 64 and then set the directories in code blocks (creating two compiler profiles); so i don't know if messed up anything or its MinGW32's fault.

What I have tried:

googling, including various headers
Posted
Updated 21-Jun-22 20:40pm
v3
Comments
Richard MacCutchan 1-May-22 3:16am
   
You need to link the correct Windows library into the program.
reverser69 8-May-22 4:49am
   
can you please specify how? I'm using C::B + GCC(MinGW)
I'll also update my question in hope of better explaining myself.
Richard MacCutchan 8-May-22 7:03am
   
I have never used Code::Blocks so have no suggestions. However, if you look at the documentation for messageBoxA at MessageBoxA function (winuser.h) - Win32 apps | Microsoft Docs[^], you will see that is is defined in User32.lib. So you need to tell Code::Blocks or gcc to include that library in its link phase. A far simpler solution would be to install the latest Microsoft Visual Studio Community Edition, which is completely free.

1 solution

hi
after searching for a month or two, I finally found out the solution:
in x86 code, APIs must be prefixed with underscore and called by their decorated name.
e.ge i must call like:
C++
calll _MessageBoxA@16

16 being the number of bytes of the function's parameter.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900