MIPS Assembly

register

  • $0: always 0
  • $v0, $v1: expression evaluation and function return value
  • $a0 ~ $a3: argument
  • $t0 ~ $t9: temporary variable saved by caller
  • $s0 ~ $s7: temporary variable saved by callee
  • $gp: global area pointer
  • $sp: stack pointer
  • $fp: frame pointer
  • $ra: return address

memory declaration

.data
var0: .word number0, number1, …
var1: .space number_of_bytes
var2: .byte character0, character1, …
var3: .asciiz string
var4: .float floating_point_number

.text
.align 2

.global main
main:       …
function0:  …

line label

label:  instruction0
        …

inner label

start with underscore to distinguish from function

instruction format

opcodeoperands
6 bits26 bits
  • Op: opcode
  • Rs: source register

R-type

nameOpRsRtRdShFunc
bits655556
  • Rt: the other source register
  • Rd: destination register
  • Sh: shift register
  • Func: function code

I-type

nameOpRsRtImmed
bits65516
  • Rt: target register
  • Immed: immediate value
nameOpTarget
bits626

system call syscall

  1. load syscall code into $v0
  2. put argument into $a0, $a1, $f12 (float)
  3. syscall
  4. return value in $v0, $f0 (float)

syscall code

  • 1: print integer
  • 2: print float
  • 3: print double
  • 4: print string
  • 5: read integer
  • 6: read float
  • 7: read double
  • 8: read string
    arguments: buffer, length
  • 9: sbrk (malloc)
  • 10: exit