use of "branch and link" (that is, call to function) in an assembly line code should really avoided at all cost since you prevent gcc to be aware of it and let gcc generate broken code.
But to answer your question :
- if you function returns a 32-bit word, $v0 contains this return value
- if you function returns a 64-bit word, $v1-$v0 contains this return value (lower value is in $v0)
- if your function accepts one or several arguments, $a0 would be the first, $a1 the next one, up to 8 registers in this order : $a0, $a1, $a2, $a3, $t0, $t1, $t2, $t3
int f(int x, int y) should be called this way :
Code: Select all
int ra, res;
asm __volatile__ (
"move $a0,%3" "\n" // unoptimal if %3 == $a0
"move $a1,%4" "\n" // unoptimal if %4 == $a0
"jalr %2" "\n"
"move %0, $ra" "\n"
"move $ra,%0" "\n"
"move %1,$v0" "\n" // unoptimal if %1 == $v0
:"+r"(ra), "=r"(res)
:"r"(pFunction), "r"(x), "r"(y)
:"v0", "v1", // a call can change them so clobber them!
"a0", "a1", "a2", "a3",
"t0", "t1", "t2", "t3", "t5", "t6", "t7", "t8", "t9",
"memory"
);
but still this is very unoptimal this way.
instead of asking how to call a C function in inline asm function, why not just output your inline asm function which calls, so we can give you the best way to do so ?