07-08 - CO - B2Ch2 - MIPS Instruction Set
07-08 - CO - B2Ch2 - MIPS Instruction Set
07-08 - CO - B2Ch2 - MIPS Instruction Set
Computer Organization
Fall 2021
1
Refer to Book2:
Patterson & Hennessy,
“Computer Organization &Design”
Slide Sources:
Dr. Sumanta Guha (Asian Institute of technology)
who adapt and supplement slides Patterson &
Hennessy COD book website (copyright Morgan
Kaufmann)
2
2.1 Introduction
◼ In this book, we will study
The organization of a real computer (MIPS architecture) and its
instruction set.
◼ Here, we start to learn how to design a computer
organization, considering design goals:
maximize performance, minimize cost and energy, and reduce
design time
◼ Our starting point is the instruction set which specifies a
processor’s functionality
what operations it supports
what storage mechanisms it has & how they are accessed
how the programmer/compiler communicates programs to
processor
3
Instruction Set Similarity
◼ By studying, the instruction set of MIPS technology, you
will notice that its similarity with Mano’s computer.
◼ Also, this similarity will exist by comparing MIPS
instruction set with 3 other popular instructions sets:
ARMv7 (32 bits) and ARMv8 (64 bits), the most popular
instruction set in the world.
Intel x86, which powers both the PC and cloud of PostPC Era.
◼ The similarity of instruction sets occurs because:
There are a few basic operations that all computers must provide
(ALU operation, memory access, control flow,… ).
All computers are constructed based on similar principles.
4
MIPS architecture
◼ MIPS technologies
Inspired most architectures developed since the 80's
Its name is not related to millions of instructions per second
It stands for Microcomputer without Interlocked Pipeline Stages
8
Design Principle 2
Design Principle 2: smaller is faster.
which means choose small number of registers for fast operations.
◼ Why?
The number of bits it would take in instruction format
(5 bits in MIPS)
Electronic signals have to travel further on a
physically larger chip increasing clock cycle time.
Smaller is also cheaper!
MIPS Load/Store Architecture
◼ MIPS is a Load/Store Architecture
Only load/store type instructions can access memory
◼ Instructions that transfer data between memory and
registers are called data transfer instructions.
◼ MIPS data transfer instructions
lw (load word), copies data from memory to a register.
sw (store word), copies data from a register to memory.
10
Load/Store Instructions
◼ Load instruction Constant Base address
Destination register (offset) (start address of data)
Note:
- Load word has destination first, store has destination last
11
MIPS byte addressing
◼ MIPS actually uses byte addressing
◼ Most data items are contained in words, a word is 32 bits
or 4 bytes. Registers hold 32 bits of data.
◼ In MIPS, words must start at addresses that are
multiples of 4. This is called an alignment restriction.
Assume PC=0,
want to read next word
of data (4 bytes)
Big-endian Little-endian
Memory Memory
Word 0 Byte 0 Byte 1 Byte 2 Byte 3 Byte 3 Byte 2 Byte 1 Byte 0 Word 0
Word 1 Byte 4 Byte 5 Byte 6 Byte 7 Byte 7 Byte 6 Byte 5 Byte 4 Word 1
15
Constant or Immediate Operands
◼ Small constants are used quite frequently (50% of operands)
e.g., A = A + 5;
B = B + 1;
C = C - 18;
◼ One Solution:
Reserve a register for constants
put program constants in memory and load them as required
◼ Alternative solution:
Offer versions of arithmetic instructions in which one operand is a constant.
◼ MIPS Instructions:
addi $s3, $s3, 4 # $s3=$s3 + 4
The operations in the alternative solution are much faster and use less energy,
Than if constants were loaded from memory.
17
So far we’ve learned:
◼ MIPS
loading words but byte addressing with big-endian
arithmetic on registers only (Load/store instruction
◼ Instruction Meaning
31 26 25 21 20 16 15 11 10 65 0
000000 10001 10010 01000 00000 100000
op rs rt rd shamt funct
Design Principle 4
Design Principle 4: Good design demands a compromise
31 26 25 21 20 16 15 0
100011 10010 01000 0000001111101010
op rs rt 16 bits offset
31 26 25 21 20 16 15 0
001000 11101 11101 0000000000000100
op rs rt 16 bits offset
Distinguish formats
◼ To reduce the complexity of hardware with multiple
instruction formats, keep the formats similar.
For example, the first three fields of the R-type and I-type
formats are the same size and have the same names; the length
of the fourth field in I-type is equal to the sum of the lengths of
the last three fields of R-type.
◼ So, each format is assigned a distinct set of values in the
first field (op) so that the hardware which format to deal
with.
23
“n.a.” (not applicable) means this field does not appear in this format.
Translating MIPS Assembly Language
into Machine Language
◼ If $t1 has the base of the array A and
$s2 corresponds to h, the statement
A[300] = h + A[300];
is compiled into:
lw $t0,1200($t1) # $t0 gets A[300]
add $t0,$s2,$t0 # $t0 gets h+A[300]
sw $t0,1200($t1) # A[300]gets h+A[300]
◼ let’s first represent the machine language instructions
using decimal numbers.
24
Translating MIPS Assembly Language
into Machine Language (Cont.)
◼ The machine language instructions using decimal
numbers
25
Other examples
26
2.6 Logical operations
◼ C and Java logical operators and their corresponding
MIPS instructions.
◼ Note:
MIPS implement NOT using a NOR with one operand being zero.
A NOR 0 = NOT (A)
The two MIPS shift instructions are called shift left logical (sll)
and shift right logical (srl).
27
MIPS Shift operation
◼ Assuming that the original value was in register $s0 and
the result should go in register $t2:
sll $t2,$s0,4 #$t2 = $s0 << 4 bits
◼ The machine language of the instruction above
◼ Note:
The encoding of sll is 0 in both the op and funct fields
The rs field is unused and thus is set to 0.
◼ Example: if (i==j) h = i + j;
bne $s0, $s1, Label
add $s3, $s0, $s1
Label: ....
Control: Conditional Branch
◼ The instructions bne and beq are I-type format
bne $t0, $t1, Label I-type instructions
beq $t0, $t1, Label
35
Loop assembled instructions
37
Signed versus Unsigned Comparison
38
Case/Switch instructions
◼ The simplest way to implement switch:
Turning the switch statement into a chain of if-then-else
statements.
◼ More efficiently alternatives: jump address table or jump table
It is a table of addresses that correspond to labels in the code,
The program loads the appropriate entry from the jump table into a
register. It then needs to jump using the address in the register.
◼ To support such situations, computers like MIPS include:
a jump register instruction (jr), meaning an unconditional jump to
the address specified in a register. Then it jumps to the proper
address using this instruction.
39
2.8 Procedures in computer hardware
◼ In MIPS, to execution of a procedure (or callee), the
program must :
1. Put parameters in a place where the procedure can
access them.
2. Transfer control to the procedure.
3. Acquire the storage resources needed for the procedure.
4. Perform the desired task.
5. Put return values in a place where the calling program
can access it.
6. Return control to the point of origin, since a procedure
can be called from several points in a program.
40
2.8 Procedures in computer hardware
◼ MIPS software follows the following convention for
procedure calling in allocating its 32 registers:
$a0–$a3: four argument registers in which to pass parameters
$v0–$v1: two value registers in which to return values
$ra: one return address register to return to the point of origin
41
The big picture
caller, Callee,
* puts the parameter values in $a0–$a3 *performs the calculations,
* uses jal X to jump to procedure X *places the results in $v0 and $v1,
*returns control to the caller using
◼ Need “jump” and “return”: jr $ra.
45
Procedure example $a0 $a1 $a2 $a3
leaf_example:
Restore
back the
content of
$s0
Return to
46
caller
Preserved and Not preserved registers
Preserved registers: Not preserved registers:
◼ Callee assume that the ◼ Callee can use them
caller may used them. directly without saving
◼ So, callee responsible to: their contents first.
save their values before it
uses them (in stack) , and
Restore their values before
return to caller
47
Main function
◼ The main function that call leaf_example callee
Should puts the parameter values in $a0–$a3 before calling
. . .
main:
.. .
add $a0, $s0, $0
add $a1, $s1, $0 Pass parameters values exist
add $a2, $s2, $0 in ($s0 -- $s3) to arguments
add $a3, $s3, $0 registers ( $a0 -- $a3)
48
Procedure frame
◼ Each procedure is associated with a procedure
frame or activation record
◼ Each frame has a frame pointer: $fp ($30)
main {
…
proc1
…}
proc1 {
…
proc2
…}
proc2 {
…
proc3
…}
The frame pointer does not change during procedure execution, unlike the stack
pointer – although can use $sp with a little extra math 49
Procedures
◼ Example C code:
int add10(int i)
{ return (i + 10);}
Procedures
◼ Translated MIPS assembly
◼ Note more efficient use of registers possible! save register
.text in stack
.globl main add10:
addi $sp, $sp, -4
main: sw $s0, 0($sp)
addi $s0, $0, 5
add $a0, $s0, $0 addi $s0, $a0, 10
argument add $v0, $s0, $0
result
to callee jal add10 to caller
control returns here
jump and link restore lw $s0, 0($sp)
add $s1, $v0, $0 values addi $sp, $sp, 4
add $s0, $s1, $0
return jr $ra
li $v0, 10 system code
MEMORY High address
syscall & call to
exit $sp
Content of $s0
Low address
◼ See more examples Sec. 2.13 Sort example
52
Global pointer
◼ A C variable is generally a location in storage, and its
interpretation depends both on its type and storage
class.
Type: integers, characters,...
Storage classes: automatic and static.
◼ Automatic variables:
are local to a procedure and are discarded when the procedure
exits.
◼ Static variables:
exist across exits from and entries to procedures.
variables declared outside all procedures
◼ To simplify access to static data, MIPS software
reserves another register, called the global pointer, or
$gp. 53
MIPS memory allocation
Reserved for OS
54
MIPS register conventions
56
Pseudo-instructions
◼ The assembler can also treat common variations of
machine language instructions as if they were
instructions in their own right.
◼ The hardware need not implement these instructions;
however, their appearance in assembly language
simplifies translation and programming. Such
instructions are called pseudoinstructions.
◼ Thus the MIPS assembler accepts this instruction even
though it is not found in the MIPS architecture:
move $t0,$t1 # register $t0 gets register $t1
◼ The assembler converts this assembly language
instruction into the machine language equivalent of the
following instruction:
add $t0,$zero,$t1 # register $t0 gets 0 + register $t1 57
◼ The pseudoinstructions give MIPS a richer set of
assembly language instructions than those implemented
by the hardware
◼ The MIPS pseudoinstructions:
blt (branch on less than) into the two instructions slt and bne
bgt, bge, and ble. It also converts branches to faraway
locations into a branch and jump.
◼ If you are going to write assembly programs, use
pseudoinstructions to simplify your task.
◼ To understand the MIPS architecture and be sure to get
best performance, however, study the real MIPS
instructions found in Figures 2.1 and 2.19.
58
59
MIPS instructions (in decimal numbers)
60
MIPS assembly language
61
MIPS assembly language (Cont.)
62