Question

We are developing a C to MIPS cross compiler, and for that we need to compare our MIPS code with the code generated by other C to MIPS cross compiler like LLVM, or CodeSourcery, etc. We are using SPIM to run our MIPS code.

Right now we are using CodeSourcery cross compiler to generate the corrresponding MIPS code for a given C program. It generates statements like lui $2,%hi($LC0) , lw $3,%lo($LC0)($2), where LCO is a label, but while loading that file in SPIM. It shows parser error like

(parser) Unknown character on line 30 of file test1.s
  lui   $2,%hi($LC0)

I don't know what is the equivalent MIPS code for this kind of statement, so that it can be parsed by SPIM?

Was it helpful?

Solution 2

In the book "See MIPS Run 2nd Edition" by Morgan Kaufmann there are equivalents MIPS code for the instructions that use "hi" and "lo" registers for e.g.

  1. lw $2, addr => lui at, %hi(addr)
                           lw $2, %lo(addr)(at)

  2. sw $2, addr($3) => lui at, %hi(addr)
                                   addu at, at, $3
                                   sw $2, %lo(addr)(at)

  3. la $2, addr => lui at, %hi(addr)
                           addiu $2, at, %lo(addr)

  4. la $2, addr($3) => lui at, %hi(addr)
                                 addiu $2, at, %lo(addr)
                                 addu $2, $2, $3

We can put the equivalent code which does not contain "hi" and "lo" registers and make SPIM run our program successfully.

OTHER TIPS

The %hi(addr) and %lo(addr) syntax is used by Gnu assembler, and it works as shown in the code above. SPIM does not support this syntax, because it is intended as simple teaching tool (see this question).

If you are developing a real MIPS cross compilation tool, you should test it with a full MIPS simulator like the free OVP MIPS Simulator. OVP is tested to be compatible with real MIPS processors.

You can use the GCC flag -mno-explicit-relocs to avoid assembler relocation operators when dealing with symbolic addresses (i.e. %hi and %lo).

For example:

mips-elf-gcc -S kernel.c -ffreestanding -mips32 -mno-explicit-relocs -Wall

You can check GCC MIPS options in https://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/MIPS-Options.html

That's not a legal syntax for MIPS assembly.

Memory access in MIPS are usually written like

offset(register_pointer)

For example:

lw $t0, 0($sp)

AFAIK offset is an immediate 16-bit signed integer and cannot be anything else (it cannot be a register, a label, etc...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top