?
Solved

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

Posted on 2003-11-10
7
Medium Priority
?
1,176 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 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