This lists all of the instructions we use in CSC 252, and their approximate equivalent in C code.
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 |
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 |
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.
Uses a different format from the other load/store instructions.
la reg1 LABEL (Stores the address indicated by LABEL into reg1)
General Format name reg1, reg2, LABEL
| Instruction | C approximation | Notes |
|---|---|---|
| beq | if (reg1 == reg2) goto LABEL |
|
| bne | if (reg1 != reg2) goto LABEL |
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. |