CS274: Computer Architecture - The MIPS Assembly Language and Instructions
Activity Goals
The goals of this activity are:
- To write a simple program using the instructions of the MIPS Assembly Language
- To identify the three instruction formats of the MIPS Assembly Language
- To relate the relatively few instructions and formats to the regularlity of the language design
- To explain the function of a system call
- To differentiate between registers and memory
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.
Model 1: A First Program
Questions
- Using your best guess, write comments for each line of code. What do you think they do?
- Save and import this program into the MIPS Simulator and run it. What happens?
Model 2: Instructions of the MIPS Instruction Set
Instruction |
Note |
Type |
Example |
Effect |
add |
Add two registers |
R |
add $t1, $t2, $t3 |
t1 = t2 + t3 |
subtract |
Subtract two registers |
R |
sub $t1, $t2, $t3 |
t1 = t2 - t3 |
add immediate (constant) |
Add a register and a 16-bit value |
I |
addi $t1, $t2, 10 |
t1 = t2 + 10 |
|
|
|
|
|
load word |
Load from memory at address register + immediate into a register |
I |
lw $s0, 4($sp) |
s0 = *(sp + 4) |
store word |
Store a register to memory at address register + immediate |
I |
sw $s0, 4($sp) |
*(sp + 4) = s0 |
load immediate |
Store a 16-bit value in the lower half of a 32-bit register |
I |
li $t0, 100 |
t0 = 100 |
load upper immediate |
Store a 16-bit value in the upper half of a 32-bit register |
I |
lui $t0, 100 |
t0 = (100 << 16) |
|
|
|
|
|
and |
Bitwise and two registers |
R |
and $t1, $t2, $t3 |
t1 = t2 & t3 |
or |
Bitwise or two registers |
R |
or $t1, $t2, $t3 |
t1 = t2 | t3 |
nor |
Bitwise nor two registers |
R |
nor $t1, $t2, $t3 |
t1 = ~(t2 | t3) |
and immediate |
Bitwise and a register and a value |
I |
andi $t1, $t2, 4 |
t1 = the value at the third least significant bit of t2 |
or immediate |
Bitwise or a register and a value |
I |
ori $t1, $t2, 7 |
t1 = the value of t2 but the least three significant bits are set to 1 |
shift left logical |
Shift the bits of a register left |
R |
sll $t1, $t2, 1 |
t1 = t2 << 1 = t2 * 2 |
shift right logical |
Shift the bits of a register right |
R |
srl $t1, $t2, 1 |
t1 = t2 >> 1 = t2 / 2 |
not |
??? |
??? |
??? |
??? |
|
|
|
|
|
branch if equal |
Move the program counter to its current location (the next instruction to execute) plus the immediate (can be specified as a label) if the registers are equal |
I |
beq $t1, $t2, 8 |
t1 == t2? PC = PC + 4 + 8 : PC = PC + 4 |
branch if not equal |
Move the program counter to its current location (the next instruction to execute) plus the immediate (can be specified as a label) if the registers are NOT equal |
I |
bne $t1, $t2, 8 |
t1 != t2? PC = PC + 4 + 8 : PC = PC + 4 |
set if less than |
Set the result to 1 if the source register is less than the target register, 0 otherwise |
R |
slt $t1, $t2, $t3 |
t2 < t3 ? t1 = 1: t1 = 0 |
set if less than immediate |
Set the result to 1 if the source register is less than the immediate, 0 otherwise |
I |
slti $t1, $t2, 1 |
t2 < 1 ? t1 = 1: t1 = 0 |
branch if less than |
??? |
??? |
??? |
??? |
|
|
|
|
|
jump |
Set the program counter to the jump target (augmented by the first 4 bits of the current program counter, and assuming the last two bits are 0 for word alignment) |
J |
j loop |
Set PC to the address of loop |
jump and link (function call) |
Same as jump, but set the jr register to the current program counter (the next instruction to execute) to enable a return |
J |
jal factorial |
Set PC to the address of factorial, set $ra to PC+4 |
jump register (return) |
Set the program counter to the value in the register |
R |
jr $ra |
Set the PC to the value of $ra |
Questions
- Why do you think there are three different types of instructions? Why couldn't they all use the same format?
- The
not
instruction does not exist in the language set. What other bitwise instruction could you use to re-create a not
behavior?
- Similarly, the "branch if less than" instruction
blt
is a pseudoinstruction, and does not exist as a machine level language. Instead, it translates to other existing MIPS instructions. How could you implement this using two MIPS instructions?
- Suppose you have two values in memory, and you wish to add them together. How do you do it?
Model 3: Registers
Register |
Number |
Type |
$zero |
0 |
0 |
$at |
1 |
Assembler Temporary |
$v0 - $v1 |
2 - 3 |
Return Values |
$a0 - $a3 |
4 - 7 |
Procedure Arguments |
$t0 - $t7 |
8 - 15 |
Temporary Registers |
$s0 - $s7 |
16 - 23 |
Saved Registers (procedures must restore these values before returning) |
$t8 - $t9 |
24 - 25 |
Temporary Registers |
$k0 - $k1 |
26 - 27 |
Kernel Reigsters |
$gp |
28 |
Global Pointer (procedures must restore) |
$sp |
29 |
Stack Pointer (procedures must restore) |
$fp |
30 |
Frame Pointer (procedures must restore) |
$ra |
31 |
Return Address |
Questions
- What do you think is the difference between the
t
registers and the s
registers?
- When might you use a
t
register as opposed to an s
register in your program?
Model 4: System Calls
Questions
- What do you think system call number 4 does, and what do you think system call number 10 does?
- Save and import this program into the MIPS Simulator and run it. What happens?
Submission
I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.