2 Unit3
2 Unit3
Outline
Instruction Set Architecture
Overview of the MIPS Processor
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 2
Instruction Set Architecture (ISA)
Critical Interface between hardware and software
An ISA includes the following …
Instructions and Instruction Formats
Data Types, Encodings, and Representations
Programmable Storage: Registers and Memory
Addressing Modes: to address Instructions and Data
Handling Exceptional Conditions (like division by zero)
Instruction Set Architecture ICS 233 – Computer Architecture and 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
$11 = $t3 $27 = $k1
To standardize their use in programs
$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
Instruction Set Architecture ICS 233 – Computer Architecture and 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)
Instruction Set Architecture ICS 233 – Computer Architecture and 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
Instruction Set Architecture ICS 233 – Computer Architecture and 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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 10
Next . . .
Instruction Set Architecture
Overview of the MIPS Processor
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 11
R-Type Format
Op6 Rs5 Rt5 Rd5 sa5 funct6
Examples:
srl
shift-in 0 ... shift-out LSB
sra
shift-in sign-bit ... shift-out LSB
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 17
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 19
Your Turn . . .
Multiply $s1 by 26, using shift and add instructions
Hint: 26 = 2 + 8 + 16
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 20
Next . . .
Instruction Set Architecture
Overview of the MIPS Processor
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 21
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 32
Next . . .
Instruction Set Architecture
Overview of the MIPS Processor
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 33
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 35
Better Implementation for AND
if (($s1 > 0) && ($s2 < 0)) {$s3++;}
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 38
Next . . .
Instruction Set Architecture
Overview of the MIPS Processor
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 39
Load and Store Instructions
Instructions that transfer data between memory & registers
Store Instruction:
Transfers data from a register to memory
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 41
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
...
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 42
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 43
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
Register Addressing
Op6 Rs5 Rt5 Rd5 sa5 funct6 Operand is in a register
Register
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 51
Branch / Jump Addressing Modes
Used for branching (beq, bne, …)
PC-Relative Addressing
Op6 Rs5 Rt5 immediate16
+1 Word = Target Instruction
PC30 00
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 52
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
Instruction Set Architecture ICS 233 – Computer Architecture and Assembly Language – KFUPM
© Muhamed Mudawar slide 53
Jump and Branch Limits
During execution, PC contains the address of current instruction
(thus we add 1 to immediate16).
Maximum branch limit is -215 to +215-1 instructions.
If immediate is positive => Forward Jump
If immediate is negative => Backward Jump