Search code examples
c++compiler-constructionassemblyspim

Convert C++ Code to Assembly for SPIM


I'm having a lot of trouble getting my compiled assembly file working on SPIM. Basically I want to write a c++ file, and then generate a .s file that I can open in SPIM without error. This means that the assembly must be in MIPS32 ABI using MIPS I instructions (some MIPS II). How do I do this? Right now I'm using g++ but I'm having major errors when I try ot run the file in SPIM. I'm working on MAC OSx 10.6.3 and I'm compiling remotely on a linux machine. Is there a special compiler I can use that will make this easy for me?


Solution

  • Give the compiler -S option, it will generate the assembly code. Then you will have to edit the code so that SPIM accepts it.

    You'll also want g++ -S -fno-delayed-branch if you enable optimization like -O1 or -Og for more readable code. Usually SPIM is configured to simulate a MIPS without branch-delay slots, but gcc will assume that the instruction after a branch is run even if it's taken. -fno-delayed-branch gets gcc to fill any branch-delay slots with nop.

    Another useful option is -fverbose-asm to have gcc add comments with the C variable name of each operand.

    You'll want to avoid using C++ libraries like std::vector that compile to a lot of extra code vs. local arrays, especially without optimization, unless you really need those features.