1
votes

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.

1
Have you chosen a compiler already? Calling conventions are going to be compiler-specific. - Alexey Frunze
How complex is the MIPS code? Will it call system calls? If not, an emulator for it is much easier to write. You'll have to figure out the tough details of passing data back and forth between the emulator and your C code... - nneonneo
I'm going to use the GNU GCC as my compiler The MIPS will only call functions from my c program - no system calls. - C.E.Sally
Is your C code going to be compiled using a MIPS cross-compiler? I'm afraid I may have misinterpreted your question. - nneonneo

1 Answers

2
votes

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 -S option). Say you want to call an assembler function defined in C as int foo(int a, int b). You would write a simple version of that function in C. For example, put the following into foo.c:

int foo(int a, int b) {
    return a+b;  // some simple code to access all arguments and the return value
}

Then you would compile that function using a MIPS cross compiler (see below) using the -S and the -O0 option to gcc which will produce a text output file foo.S giving you MIPS assembler source code to access the arguments for function foo and 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 compute foo.

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.