I’d like to call assembly (specifically MIPS) code from my C program and call the C back from the assembly.
I’ve decided on the GNU GCC as my compiler, (I am also guessing I need an emulator?)
I’m on a x86 Win 7 machine.
There are some things that are very unclear to me how this can/should work out.
- If MIPS will be using a load-store archi with 32 regs and the C will continue to use a register memory archi because I’m on x86?
- Now that I want to call mips assembly instead of x86 assembly, can/do I still use asm() ?
- If MIPS uses more registers than C, will I be able to access those registers from my C code?
Can anyone help me out with this, perhaps by pointing out where I could learn this bit of sorcery?
Thanks
Disclaimer: I am working on a verification of self modifying code project for credit in school, and this code is going to be used as an example, but I am not getting any credit for this code.
The most common MIPS calling convention is described here. The easiest way to write a C-callable assembly routine is to write a skeleton for the routine in C, and then copy the assembly code output from the compiler into your assembly source (use gcc’s
-Soption). Say you want to call an assembler function defined inCasint foo(int a, int b). You would write a simple version of that function inC. For example, put the following into foo.c:Then you would compile that function using a MIPS cross compiler (see below) using the
-Sand the-O0option to gcc which will produce a text output filefoo.Sgiving you MIPS assembler source code to access the arguments for functionfooand showing you where to put the return value. Simply copy that source file into your own assembler source, and add the assembler calculations you need to computefoo.Calling C from assembly is straightforward once you have calling in the other direction figured out.
You can download a free MIPS gcc cross compiler tool chain from Mentor Graphics (formerly Codesourcery).
You can download a free, fully functional (it boots and runs Linux) MIPS simulator from here. Don’t use SPIM or MARS, since they do not completely model the MIPS architecture.