5

Say, I want to call a function with the following signature in inline assembly of gcc. How can I do that?

int some_function( void * arg );
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 4
    Do it in bare bones C first then use `gcc -S` to get template code which you can adapt to your needs. – Paul R Nov 02 '11 at 17:00
  • Paul R, nice solution, but I want a more general purpose solution. – MetallicPriest Nov 02 '11 at 17:04
  • 5
    You aren't supposed to call functions from inline assembly. GCC doesn't try to interpret the code, so it can cause errors, such as not setting up a stack frame under the assumption that no function calls are made. – ughoavgfhw Nov 02 '11 at 17:05
  • 1
    @MetallicPriest: how is this not "general purpose" ? It's the quickest/easiest way to deal with pretty much any assembly-related question - much more efficient than working everything out from scratch. – Paul R Nov 02 '11 at 17:07
  • 4
    Why would you want to do that? Inline assembly is made on purpose such that you don't have to call an assembly function directly and such that it smoothly integrates into the surrounding code. Could you please describe more precisely what your final goal is? – Jens Gustedt Nov 02 '11 at 17:10
  • related: https://stackoverflow.com/questions/3467180/direct-call-using-gccs-inline-assembly – Ciro Santilli OurBigBook.com Jul 03 '19 at 08:40

1 Answers1

9

Generally you'll want to do something like

void *x;
asm(".. code that writes to register %0" : "=r"(x) : ...
int r = some_function(x);
asm(".. code that uses the result..." : ... : "r"(r), ...

That is, you don't want to do the function call in the inline asm at all. That way you don't have to worry about details of the calling conventions, or stack frame management.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226