Solved

How to remove an Assembler message (warning) in inlined ASM using GCC

Posted on 2003-11-10
7
1,114 Views
Last Modified: 2007-12-19
Hello,

I am writing some codes which interface to x86 ASM subroutines from withing C++ source files. The interface in the C++ source files is in C++. The inline sequence has the purpose of calling a function based on a function-pointer technique used in the C++ language. THe program runs well and I am satisfied with the code-technical aspects of the solution. However, there are some unwanted assembler warnings issued by the compiler.

The following message text is issued:
  Assembler Messages: Warning: indirect call without '*'

This warning pops up at each usage of the inline sequence.

What does this warning mean? Is it really harmless, as I assume? How can I find any ways to eliminate this warning?

Thank you very much.

Sincerely, Chris.

0
Comment
Question by:dude_1967
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 9714938

How about showing us the lines in question?
0
 
LVL 5

Accepted Solution

by:
mtmike earned 500 total points
ID: 9715229
Your inline assembly probably looks something like:

asm ("call %0" : : "r" (func));

This should be:

asm ("call *%0" : : "r" (func));

This is just the way gas expects indirect function calls to be written. Both forms are accepted though and produce identical code. So, it is harmless.
0
 
LVL 1

Author Comment

by:dude_1967
ID: 9715525
Thanks for the replies. Mike, the use of the star removes the warnings.

For the sake of completeness: The lines of code in question are shown, in the modified form below. There is support for GCC as well as Microsoft Visual C++. I am weak on the syntax for inline assembler in GCC. If there are any additional suggestions for improvement, then I would greatly appreciate them.

Thanks again. This issue is in my opinion closed and the points have been awarded.

Sincerely, Chris.

extern "C"
{
  typedef void (*t_pfn_void_pc)(char*);

  static void call_asm(t_pfn_void_pc pfn, char* p)
  {
    #if defined(USE_INTEL_x86_SYSTEM)
     
      #if defined(_MSC_VER)
   
        _asm
        {
          mov eax, dword ptr [p]   // Input parameter p for subroutine pfn(char* p).
          push eax                 // Stack the input parameter.
          call dword ptr [pfn]     // Call pfn(p).
          add esp, 04h             // Restore esp.
          xor ebx, ebx             // Tell compiler that ebx was clobbered.
          xor ecx, ecx             // Tell compiler that ecx was clobbered.
          xor edx, edx             // Tell compiler that edx was clobbered.
          xor esi, esi             // Tell compiler that esi was clobbered.
          xor edi, edi             // Tell compiler that edi was clobbered.
        }
   
      #elif defined(__GNUC__)
   
        asm("movl %0, %%eax" : : "g" (p));
        asm("pushl %eax");
        asm("calll *%0" : : "g" (pfn));
        asm("popl %%eax" : : : "eax", "ebx", "ecx", "edx", "edi", "esi");
   
      #endif
   
    #endif
  }
}

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:mtmike
ID: 9718111
The GCC inline assembly syntax is a bit tricky to use and it is quite easy to make mistakes. Some code that executes correctly without optimization will go awry when optimization is turned on.

Some friendly tips...

GCC (with optimization enabled) will happily assemble the given code into something like:

movl ...(esp,1),eax   <- load p
movl ...(esp,1),eax   <- load pfn
push eax
call *eax
pop eax

This is of course wrong.

The problem is that GCC treats asm statements as independent. So, the code could be written as:

asm("movl %0,%%eax\n"
    "pushl %%eax\n"
    "calll *%1\n"
    "popl %%eax"
    : : "g" (p), "g" (pfn) : "eax", "ebx", "ecx", "edx", "edi", "esi");

But this is also wrong, since GCC may replace %1 (pfn) with an esp relative reference (and it will do so if you compile with -O2 -fomit-frame-pointer). This can be avoided by making sure that the function pointer is loaded into a register (use "r" instead of "g").

Note that GCC refuses to use any registers specified into the clobber list as inputs and/or outputs. Registers that may be used as inputs, but will be clobbered after the asm statement should be given as dummy outputs, eg

int b, c, d, D, S;

asm volatile("push %%eax\n"
             "call *%6\n"
             "pop %%eax\n"
             : "=b" (b), "=c" (c), "=d" (d), "=D" (D), "=S" (S)
             : "a" (p), "r" (pfn));

The "volatile" is needed to prevent GCC from optimizing away the whole asm statement. Add "cc" to the clobber list in case the called function clobbers the condition codes (eflags).

Regards,
mtmike
0
 
LVL 1

Author Comment

by:dude_1967
ID: 9721701
OK mike,

Thanks again. It's really getting there. So I have implemented the following subroutine based on your solution (See posting below) and I have two additional questions:

1) It is to me unclear why the compiler knows to place the input parameter p in eax. Is there something explicit going on?
2) How does the compiler know that eax has been clobbered or corrupted?

Thanks for the additional clarification.

Sincerely, Chris.

extern "C"
{
  typedef void (*t_pfn_void_pc)(char*);

  static void call_asm(t_pfn_void_pc pfn, char* p)
  {
    #if defined(USE_INTEL_x86_SYSTEM)
     
      #if defined(_MSC_VER)
   
        _asm
        {
          mov eax, dword ptr [p]   // Input parameter p for subroutine pfn(char* p).
          push eax                 // Stack the input parameter.
          call dword ptr [pfn]     // Call pfn(p).
          add esp, 04h             // Restore esp.
          xor ebx, ebx             // Tell compiler that ebx was clobbered.
          xor ecx, ecx             // Tell compiler that ecx was clobbered.
          xor edx, edx             // Tell compiler that edx was clobbered.
          xor esi, esi             // Tell compiler that esi was clobbered.
          xor edi, edi             // Tell compiler that edi was clobbered.
        }
   
      #elif defined(__GNUC__)

        // Dummys for inline assembler.
        ef_UINT32 b, c, d, D, S;

        asm volatile("pushl %%eax\n"
                     "calll *%6\n"
                     "popl %%eax"
                     : "=b" (b), "=c" (c), "=d" (d), "=D" (D), "=S" (S)
                     : "a" (p), "r" (pfn));
   
      #endif
   
    #endif
  }
}
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9722425
The '"a" (p)' in the input list tells the compiler to load the value of p into eax. The compiler will assume that eax still contains the value of p afterwards. Since eax is pushed and popped, this should be true.

GCC treats asm statements as input/output functions with certain constraints on the inputs and outputs. GCC assumes that all inputs will have been consumed before outputs are produced. These are the four usage patterns:

- Input operands (in input list). These are read-only (unless saved/restored).
- Output operands (in output list). These are write-only and may be used to hold unrelated input operands.
- Input/output operands (in both input and output list). These are read/write.
- Temporaries (in clobber list). These will never be used for inputs or outputs and are assumed to be clobbered.

GCC inline assembly seems overly complex, but is quite powerful. For example, you can learn GCC the "add" instruction:

static inline int add(int a, int b)
{
      int res;

      asm("add %2,%0" : "=r" (res) : "0" (a), "rm" (b) : "cc");
      return res;
}

The "add" instruction produces a single output that must be a register ("=r" (res)). It takes two input parameters, one must be the same register as its output ("0" (a)) and the other can be a register or memory reference ("rm" (b)). This instruction clobbers the condition codes ("cc").
0
 
LVL 1

Author Comment

by:dude_1967
ID: 9722740
Thanks for the clarification.

Yeah, I like the mixture of high-level language idioms combined with assembler instructions including consequential register management---very powerful indeed.

This was a nice learning exercise.

Sincerely, Chris.

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows 7, Delphi Code, Drawcolumn ERROR 13 404
push(rx) in Beta 21 377
Automating interactive programs 5 415
port mapped I/o query 10 139
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now