Function calls within inline asm?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Function calls within inline asm?

Post by Kojima »

Hey, I was just wondering if it's possible to make function calls (defined in C++) in line asm?

something like,

Code: Select all


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?
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

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!
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Thanks I'll give it a try. I take it there's no way to call delegates from asm though?
SSpeare
Posts: 63
Joined: Tue May 23, 2006 11:45 pm
Contact:

Post by SSpeare »

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.
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.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

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 :)
Post Reply