Solved

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

Posted on 2003-11-10
7
1,143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Tell If Two Executable Files are the Same That Are Compiled from C++ 14 642
Binary Bomb Phase 2 5 1,510
BufferBomb Phase 0 4 1,366
ia32 assembly to c 3 489
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
This article was initially published on Monitis Blog, you can read it here . When it comes to deciding which approach to website performance monitoring is best for your business, unfortunately, like so many options in life . . . it depends. In t…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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