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 );
6
  • 4
    Do it in bare bones C first then use gcc -S to get template code which you can adapt to your needs. Commented Nov 2, 2011 at 17:00
  • Paul R, nice solution, but I want a more general purpose solution. Commented Nov 2, 2011 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. Commented Nov 2, 2011 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. Commented Nov 2, 2011 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? Commented Nov 2, 2011 at 17:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What should I do if I want to call a function in inline assembly? Thanks
@cyj: You don't
@cyj: There is an answer on Calling printf in extended inline ASM that should be safe and shows how much it sucks. Don't do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.