Week4 - Instruction Set - Part 2
Week4 - Instruction Set - Part 2
Week 4
INSTRUCTION SET – Part 2
Reference book:
Computer Organization and Design: The Hardware/Software Interface,
Patterson, D. A., and J. L. Hennessy, Morgan Kaufman, Revised Fourth
Edition, 2011.
Copyrights 2017 CE-UIT. All Rights Reserved.
Week 4 – Instruction Set – Part 2
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Instruction fields
op: operation code (opcode)
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
op address
6 bits 26 bits
Jump instructions
op: operation code (opcode)
address: jump to target address
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
0 17 18 8 0 32
00000010001100100100000000100000 2 = 0232402016
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
8hex 17 8 7
001000100010100000000000000001112 = 2228000716
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
23hex 17 8 8
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
2bhex 17 8 8
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, … in $s0, $s1, …
Compiled MIPS code:
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit: …
Assembler calculates addresses
C code:
while (save[i] == k) i += 1;
i in $s3, k in $s5, address of save in $s6
Compiled MIPS code:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit: …
Instruction Formats
Arithmetic Instructions
Load/Store Instructions
Logical Instructions
Conditional Branch Instructions
Unconditional Branch Instructions
op address
6 bits 26 bits
2hex 2500
3hex 2500
C code
while (save[i] == k) # Find k in array save
i += 1;
Variables i, k are stored in registers $s3 và $s5; based address of array save is stored in
register $s6.
MIPS code
Loop: sll $t1,$s3,2 # Temp reg $t1 = 4 * i
add $t1,$t1,$s6 # $t1 = address of save[i]
lw $t0,0($t1) # Temp reg $t0 = save[i]
bne $t0,$s5, Exit # go to Exit if save[i] != k
addi $s3,$s3,1 #i=i+1
j Loop # go to Loop
Exit:
Reference
Section: 2.5, 2.6, 2.7
Book: Computer Organization and Design: The
Hardware/Software Interface, Patterson, D. A., and J. L.
Hennessy, Morgan Kaufman, Revised Fourth Edition, 2011.
Exercise: attached file