| View previous topic :: View next topic |
| Author |
Message |
Kojima
Joined: 26 Jun 2006 Posts: 275
|
Posted: Tue Jul 11, 2006 7:48 pm Post subject: Function calls within inline asm? |
|
|
Hey, I was just wondering if it's possible to make function calls (defined in C++) in line asm?
something like,
| Code: |
inline asm("\n"
//Set up registers for parameters,
syscall %f1 //Make call
} "=func"(myFunc)
|
Or do I have to pass a memory pointer of the function addr as a variable using standard methods and invoke it? IF so could you tell me the asm opcode to do that please? |
|
| Back to top |
|
 |
Fanjita
Joined: 28 Sep 2005 Posts: 217
|
Posted: Tue Jul 11, 2006 10:59 pm Post subject: |
|
|
You usually won't be using syscalls except for calling system-defined functions. Even then, you'd usually just jump to the address of the similarly-named function within the pspsdk library stubs.
To call a function, use "JAL <address>" or "JALR <register containing func address>". These both put the return address into $RA.
The calling convention is for function parameters to be in $a0 (=== $4) onwards (i.e. $a0, $a1, $a2, $a3, then whatever $8 + are called). Function return value comes back in $v0 (=== $2).
I would imagine that you can just pass a function address directly as an inline asm parameter (as in your example, but using JAL rather than SYSCALL), though I've never tried it personally. Should be easy enough to check the generated ASM to see whether what it produces looks valid. _________________ Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you! |
|
| Back to top |
|
 |
Kojima
Joined: 26 Jun 2006 Posts: 275
|
Posted: Tue Jul 11, 2006 11:53 pm Post subject: |
|
|
| Thanks I'll give it a try. I take it there's no way to call delegates from asm though? |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Wed Jul 12, 2006 12:31 am Post subject: |
|
|
Correctly calling C++ from assembly is tricky business. You need to know the calling conventions exactly. Which register holds this, etc. If you are calling a virtual function then you have to look the address up in the objects virtual function table. How to do that is highly dependent on your compiler and if you change your C++ class then your asm will probably break. That is, if you add/remove virtual keyword then the index into the function table will change for other virtual functions as well.
If you want to be confident you are doing it right, then you should really compile your C++ into asm (there is a gcc option to do this) and look at the assembly directly. Then you won't have to ask "how do I ..." as often either because you can just code it in C++ and then see what gcc produces.
| Quote: | | I take it there's no way to call delegates from asm though? |
Of course, you can call anything from assembly because all of your C/C++ code is compiled into assembly. That doesn't mean it's easy or a good idea, but it can always be done. |
|
| Back to top |
|
 |
Kojima
Joined: 26 Jun 2006 Posts: 275
|
Posted: Wed Jul 12, 2006 12:46 am Post subject: |
|
|
| Thanks for clearing that up SS. This is my first time ever using inline asm so I'm bound to have a few more annoying newbie questions left yet though, but I will try your asm idea to curb them for the greater good :) |
|
| Back to top |
|
 |
|