03 - MIPS Intruction Set Architecture
03 - MIPS Intruction Set Architecture
03 - MIPS Intruction Set Architecture
Architecture
ELT 3047
Computer Architecture
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 6
MIPS General-Purpose Registers
32 General Purpose Registers (GPRs)
Assembler uses the dollar notation to name registers
$0 is register 0, $1 is register 1, …, and $31 is register 31
All registers are 32-bit wide in MIPS32 $0 = $zero $16 = $s0
$1 = $at $17 = $s1
Register $0 is always zero $2 = $v0 $18 = $s2
$3 = $v1 $19 = $s3
Any value written to $0 is discarded $4 = $a0 $20 = $s4
$5 = $a1 $21 = $s5
Software conventions $6 = $a2 $22 = $s6
$7 = $a3 $23 = $s7
There are many registers (32) $8 = $t0 $24 = $t8
$9 = $t1 $25 = $t9
Software defines names to all registers $10 = $t2 $26 = $k0
To standardize their use in programs $11 = $t3 $27 = $k1
$12 = $t4 $28 = $gp
Example: $8 - $15 are called $t0 - $t7 $13 = $t5 $29 = $sp
$14 = $t6 $30 = $fp
Used for temporary values $15 = $t7 $31 = $ra
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 7
MIPS Register Conventions
Assembler can refer to registers by name or by number
It is easier for you to remember registers by name
Assembler converts register name to its corresponding number
Name Register Usage
$zero $0 Always 0 (forced by hardware)
$at $1 Reserved for assembler use
$v0 – $v1 $2 – $3 Result values of a function
$a0 – $a3 $4 – $7 Arguments of a function
$t0 – $t7 $8 – $15 Temporary Values
$s0 – $s7 $16 – $23 Saved registers (preserved across call)
$t8 – $t9 $24 – $25 More temporaries
$k0 – $k1 $26 – $27 Reserved for OS kernel
$gp $28 Global pointer (points to global data)
$sp $29 Stack pointer (points to top of stack)
$fp $30 Frame pointer (points to stack frame)
$ra $31 Return address (used by jal for function call)
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 8
Instruction Formats
All instructions are 32-bit wide, Three instruction formats:
Register (R-Type)
Register-to-register instructions
Op: operation code specifies the format of the instruction
Op6 Rs5 Rt5 Rd5 sa5 funct6
Immediate (I-Type)
16-bit immediate constant is part in the instruction
Op6 Rs5 Rt5 immediate16
Jump (J-Type)
Used by jump instructions
Op6 immediate26
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 9
Instruction Categories
Integer Arithmetic
Arithmetic, logical, and shift instructions
Data Transfer
Load and store instructions that access memory
Data movement and conversions
Jump and Branch
Flow-control instructions that alter the sequential sequence
Floating Point Arithmetic
Instructions that operate on floating-point registers
Miscellaneous
Instructions that transfer control to/from exception handlers
Memory management instructions
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 10
Layout of a Program in Memory
0x7FFFFFFF
Stack Segment Stack Grows
Downwards
Memory
Addresses
in Hex
Dynamic Area
Data Segment
Static Area
0x10000000
Text Segment
0x04000000
Reserved
0
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 11
MIPS Assembly Language Program
# Title: Filename:
# Author: Date:
# Description:
# Input:
# Output:
################# Data segment #####################
.data
. . .
################# Code segment #####################
.text
.globl main
main: # main program entry
. . .
li $v0, 10 # Exit program
syscall
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 12
.DATA, .TEXT, & .GLOBL Directives
.DATA directive
Defines the data segment of a program containing data
The program's variables should be defined under this directive
Assembler will allocate and initialize the storage of variables
.TEXT directive
Defines the code segment of a program containing instructions
.GLOBL directive
Declares a symbol as global
Global symbols can be referenced from other files
We use this directive to declare main procedure of a program
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 13
Next . . .
Instruction Set Architecture
Overview of the MIPS Architecture
R-Type Arithmetic, Logical, and Shift Instructions
I-Type Format and Immediate Constants
Jump and Branch Instructions
Translating If Statements and Boolean Expressions
Load and Store Instructions
Translating Loops and Traversing Arrays
Addressing Modes
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 14
R-Type Format
Op6 Rs5 Rt5 Rd5 sa5 funct6
Examples:
Assume $s1 = 0xabcd1234 and $s2 = 0xffff0000
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 19
Shift Operations
Shifting is to move all the bits in a register left or right
Shifts by a constant amount: sll, srl, sra
sll/srl mean shift left/right logical by a constant amount
The 5-bit shift amount field is used by these instructions
sra means shift right arithmetic by a constant amount
The sign-bit (rather than 0) is shifted from the left
srl
shift-in 0 ... shift-out LSB
sra
shift-in sign-bit ... shift-out LSB
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 20
Shift Instructions
Instruction Meaning R-Type Format
sll $s1,$s2,10 $s1 = $s2 << 10 op = 0 rs = 0 rt = $s2 rd = $s1 sa = 10 f=0
srl $s1,$s2,10 $s1 = $s2>>>10 op = 0 rs = 0 rt = $s2 rd = $s1 sa = 10 f=2
sra $s1, $s2, 10 $s1 = $s2 >> 10 op = 0 rs = 0 rt = $s2 rd = $s1 sa = 10 f=3
sllv $s1,$s2,$s3 $s1 = $s2 << $s3 op = 0 rs = $s3 rt = $s2 rd = $s1 sa = 0 f=4
srlv $s1,$s2,$s3 $s1 = $s2>>>$s3 op = 0 rs = $s3 rt = $s2 rd = $s1 sa = 0 f=6
srav $s1,$s2,$s3 $s1 = $s2 >> $s3 op = 0 rs = $s3 rt = $s2 rd = $s1 sa = 0 f=7
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 22
Your Turn . . .
Multiply $s1 by 26, using shift and add instructions
Hint: 26 = 2 + 8 + 16
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 23
Next . . .
Instruction Set Architecture
Overview of the MIPS Architecture
R-Type Arithmetic, Logical, and Shift Instructions
I-Type Format and Immediate Constants
Jump and Branch Instructions
Translating If Statements and Boolean Expressions
Load and Store Instructions
Translating Loops and Traversing Arrays
Addressing Modes
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 24
I-Type Format
Constants are used quite frequently in programs
The R-type shift instructions have a 5-bit shift amount constant
What about other instructions that need a constant?
I-Type: Instructions with Immediate Operands
Op6 Rs5 Rt5 immediate16
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 35
Next . . .
Instruction Set Architecture
Overview of the MIPS Architecture
R-Type Arithmetic, Logical, and Shift Instructions
I-Type Format and Immediate Constants
Jump and Branch Instructions
Translating If Statements and Boolean Expressions
Load and Store Instructions
Translating Loops and Traversing Arrays
Addressing Modes
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 36
Translating an IF Statement
Consider the following IF statement:
if (a == b) c = d + e; else c = d – e;
Assume that a, b, c, d, e are in $s0, …, $s4 respectively
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 38
Better Implementation for AND
if (($s1 > 0) && ($s2 < 0)) {$s3++;}
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 39
Compound Expression with OR
Short-circuit evaluation for logical OR
If first expression is true, second expression is skipped
if (($sl > $s2) || ($s2 > $s3)) {$s4 = 1;}
Store Instruction:
Transfers data from a register to memory
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 43
Load and Store Word
Load Word Instruction (Word = 4 bytes in MIPS)
lw Rt, imm16(Rs) # Rt MEMORY[Rs+imm16]
Store Word Instruction
sw Rt, imm16(Rs) # Rt MEMORY[Rs+imm16]
Base or Displacement addressing is used
Memory Address = Rs (base) + Immediate16 (displacement)
Immediate16 is sign-extended to have a signed displacement
Base or Displacement Addressing
Op6 Rs5 Rt5 immediate16
+ Memory Word
Base address
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 44
Example on Load & Store
Translate A[1] = A[2] + 5 (A is an array of words)
Assume that address of array A is stored in register $s0
lw $s1, 8($s0) # $s1 = A[2]
addiu $s2, $s1, 5 # $s2 = A[2] + 5
sw $s2, 4($s0) # A[1] = $s2
...
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 45
Load and Store Byte and Halfword
The MIPS processor supports the following data formats:
Byte = 8 bits, Halfword = 16 bits, Word = 32 bits
Load & store instructions for bytes and halfwords
lb = load byte, lbu = load byte unsigned, sb = store byte
lh = load half, lhu = load half unsigned, sh = store halfword
Load expands a memory data to fit into a 32-bit register
Store reduces a 32-bit register to fit in memory
32-bit Register
s sign – extend s s b
0 zero – extend 0 bu
s sign – extend s s h
0 zero – extend 0 hu
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 46
Load and Store Instructions
Instruction Meaning I-Type Format
lb rt, imm16(rs) rt = MEM[rs+imm16] 0x20 rs5 rt5 imm16
lh rt, imm16(rs) rt = MEM[rs+imm16] 0x21 rs5 rt5 imm16
lw rt, imm16(rs) rt = MEM[rs+imm16] 0x23 rs5 rt5 imm16
lbu rt, imm16(rs) rt = MEM[rs+imm16] 0x24 rs5 rt5 imm16
lhu rt, imm16(rs) rt = MEM[rs+imm16] 0x25 rs5 rt5 imm16
sb rt, imm16(rs) MEM[rs+imm16] = rt 0x28 rs5 rt5 imm16
sh rt, imm16(rs) MEM[rs+imm16] = rt 0x29 rs5 rt5 imm16
sw rt, imm16(rs) MEM[rs+imm16] = rt 0x2b rs5 rt5 imm16
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 52
Next . . .
Instruction Set Architecture
Overview of the MIPS Architecture
R-Type Arithmetic, Logical, and Shift Instructions
I-Type Format and Immediate Constants
Jump and Branch Instructions
Translating If Statements and Boolean Expressions
Load and Store Instructions
Translating Loops and Traversing Arrays
Addressing Modes
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 53
Addressing Modes
Where are the operands?
How memory addresses are computed?
Immediate Addressing
Op6 Rs5 Rt5 immediate16 Operand is a constant
Register Addressing
Op6 Rs5 Rt5 Rd5 sa5 funct6 Operand is in a register
Register
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 54
Branch / Jump Addressing Modes
Used for branching (beq, bne, …)
PC-Relative Addressing
Op6 Rs5 Rt5 immediate16
+1 Word = Target Instruction
PC30 00
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 55
Jump and Branch Limits
Jump Address Boundary = 226 instructions = 256 MB
Text segment cannot exceed 226 instructions or 256 MB
Upper 4 bits of PC are unchanged
Target Instruction Address PC4 immediate26 00
2. Smaller is faster
Limit the number of registers for faster access (typically 32)
3. Make the common case fast
Include constants inside instructions (faster than loading them)
Design most instructions to be register-to-register
4. Good design demands good compromises
Fixed-size instructions compromise the size of constants
MIPS Instruction Set Architecture ICS 233 – Computer Architecture & Assembly Language – KFUPM © Muhamed Mudawar – slide 58