Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function in gcc inline assembly

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 );
like image 526
MetallicPriest Avatar asked Nov 02 '11 16:11

MetallicPriest


People also ask

Does GCC support inline assembly?

There are, in general, two types of inline assembly supported by C/C++ compilers: asm (or __asm__) in GCC. GCC uses a direct extension of the ISO rules: assembly code template is written in strings, with inputs, outputs, and clobbered registers specified after the strings in colons.

How do function calls work in assembly?

In assembly language, the call instruction handles passing the return address for you, and ret handles using that address to return back to where you called the function from. The return value is the main method of transferring data back to the main program.

What is __ asm __ in C?

The __asm keyword invokes the inline assembler and can appear wherever a C or C++ statement is legal. It cannot appear by itself. It must be followed by an assembly instruction, a group of instructions enclosed in braces, or, at the very least, an empty pair of braces.


1 Answers

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.

like image 195
Chris Dodd Avatar answered Sep 25 '22 03:09

Chris Dodd