Converting a C program to MIPS

Currently you’re generating code for x86-64 – you need to select a MIPS compiler from the popup menu above the assembly pane:

After you’ve done that you’ll probably see generated code like this:

$LFB0 = .
my_function(int, int):
$LVL0 = .
        addu    $2,$4,$5
$LVL1 = .
        addiu   $4,$4,-2
$LVL2 = .
        j       $31
        addu    $2,$4,$2

Note that the compiler has optimised away some of the redundant operations in the original C code. If you want to see an unoptimised version then specify -O0 in the compiler options and you’ll see something much less efficient, but closer to the original source:

$LFB0 = .
my_function(int, int):
        addiu   $sp,$sp,-16
        sw      $fp,12($sp)
        move    $fp,$sp
        sw      $4,16($fp)
        sw      $5,20($fp)
        lw      $3,16($fp)
        lw      $2,20($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,16($fp)
        addiu   $2,$2,-2
        sw      $2,4($fp)
        lw      $3,0($fp)
        lw      $2,4($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,0($fp)
        move    $sp,$fp
        lw      $fp,12($sp)
        addiu   $sp,$sp,16
        j       $31
        nop

Leave a Comment