CS230

Compiler Design


A Few Tips on MIPS

Some Useful Operations

You don't need many. In fact you don't need all of the below, but they will all certainly prove useful.
"sw R, V" stores value in register R in variable V.
"lw R, V" loads word (variable) V into register R.
"li R, n" loads numerical literal n into register R.
"add R1, R2, R3" puts R2+R3 in R1.
"seq R1, R2, R3" stores the value of "R2==R3" in R1.
"b label" is an unconditional branch to the given label.
"beq R1, R2, label" branches to the given label if R1==R2.
"bnez R1, label" branches to the given label if R1 != 0.
And so it goes. See the example below for more.
All significant operations (e.g., add, multiply, comparisons, etc.) take place in registers, so values of variables must be loaded into the registers before performing these operations. Symbolically, you may use temporary registers for arithmetic operations $t0,$t1,. . .,$t7 (actually 2 of them are enough).
In short, everything you need to know can be gleaned from the above remarks, this overview, and the below example.

How do you run an assembly program? This is easiest to do with a simulator. The standard for MIPS code is "Spim." This can be run at the command line. Alternatively, there is a GUI version called xspim. However, that is no longer supported. The up-to-date GUI software for this is QtSpim, which is available here. See comments in the code below for how to use it.

An Example

The below prints out the first 7 factorials. Note all variables are simply allocated as full words in the .data segment.
# Prints the first 7 factorials.
# Run as "xspim Factorial.a &"
# Then open terminal ("terminal/pop-up console")
# OR (at command line): "spim Factorial.a".
# OR: Start up QtSpim. Select File/Load File. The file *must* have
# the ".a" extension to be recognized (beware hidden extensions!).
# Once loaded, press the play button!
# Algorithm (source code segment):
# n = 8;
# i = 1;
# t = 1;
# while (i != n)
# {
#    t = i*t;
#    printf(t);
#    i = i + 1;
# }

            .data
n:          .word 0
i:          .word 0
t:          .word 0
nline:      .asciiz "\n"

            .text
            .globl main
main:	    li $t0, 8
            sw $t0, n
            li $t0, 1
            sw $t0, i
            sw $t0, t

            # while (i != n)
loop:       lw $t1, i
            lw $t0, n
            seq $t0, $t0, $t1
            li $t1, 1
            beq $t0, $t1, out
            bnez $t0, in

            # t = i*t
in:         lw $t0, i
            lw $t1, t
            mul $t1, $t1, $t0
            sw $t1, t
                        
            # print t
            lw $a0, t
            li $v0, 1
            syscall

            #newline
            la $a0, nline
            li $v0, 4
            syscall

            # i = i + 1
            lw $t0, i
            li $t1, 1
            add $t0, $t0, $t1
            sw $t0, i

            #end while
            b loop      

            la $a0, nline
            li $v0, 4
            syscall

            la $a0, nline
            li $v0, 4
            syscall

out:        li $v0, 10
            syscall


Back to Project Assignment 5