This lists all of the instructions we use in CSC 252, and their approximate equivalent in C code.

Basic operations

General format: name reg1, reg2, reg3

Instruction name C approximation Notes
add reg1 = reg2 + reg3
addu reg1 = reg2 + reg3 Same as add but won't throw overflow exception
sub reg1 = reg2 - reg3
subu reg1 = reg2 - reg3 Same as sub but won't throw overflow exception
and reg1 = reg2 & reg3
or `reg1 = reg2 reg3`
xor reg1 = reg2 ^ reg3
nor `reg1 = ~(reg2 reg3)`
slt reg1 = reg2 < r

Immediate instructions

General format: name reg1, reg2, imm

Instruction name C approximation Notes
addi reg1 = reg2 + imm
addiu reg1 = reg2 + imm Same as addi but won't throw overflow exception
subi reg1 = reg2 - imm
andi reg1 = reg2 & imm
ori `reg1 = reg2 imm`
xori reg1 = reg2 ^ imm
slti reg1 = reg2 < imm
sll reg1 = reg2 << imm
srl reg1 = reg2 >> imm
sra reg1 = reg2 >> imm The difference is that this one will sign-extend the immediate value

Load/Store instructions

General Format name reg1, imm(reg2)

Instruction C approximation Notes
lw int32 reg1 = *(reg2+imm)
lh int16 reg1 = *(reg2+imm)
lb int8 reg1 = *(reg2+imm)
sw *(reg2+imm) = reg1
sh *(reg2+imm) = reg1
sb *(reg2+imm) = reg1

Note that, in MIPS. Registers do not have “types”. They are all just 32 bit values. An instruction like sb will simply store the first 8 bits from the register into memory. And ignore the rest.

(Pseudo Instruction) la

Uses a different format from the other load/store instructions.

la reg1 LABEL (Stores the address indicated by LABEL into reg1)

Branching

General Format name reg1, reg2, LABEL

Instruction C approximation Notes
beq if (reg1 == reg2) goto LABEL
bne if (reg1 != reg2) goto LABEL

Jumps

General Format name LABEL

Instruction C approximation Notes
j goto LABEL
jal $ra = PC + 4
goto LABEL jump and link (jal) is for calling functions. It stores the PC + 4 (where PC is the program counter) address in $ra, so that the function knows what line of code to jump back to after it completes.