CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic3: Instructions, The Language of the Machine José Nelson Amaral.

Slides:



Advertisements
Similar presentations
MIPS Assembly Tutorial
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
Chapter 2 Instructions: Language of the Computer
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
Instructions Set Bo Cheng Instruction Set Design An Instruction Set provides a functional description of a processor. It is the visible.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
RISC Concepts, MIPS ISA and the Mini–MIPS project
The Processor 2 Andreas Klappenecker CPSC321 Computer Architecture.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 4436ECE 5367 ISA I. ECE 4436ECE 5367 CPU = Seconds= Instructions x Cycles x Seconds Time Program Program Instruction Cycle CPU = Seconds= Instructions.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
MIPS Instruction Set Advantages
ISA-2 CSCE430/830 MIPS: Case Study of Instruction Set Architecture CSCE430/830 Computer Architecture Instructor: Hong Jiang Courtesy of Prof. Yifeng Zhu.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
Computer Architecture CSE 3322 Lecture 2 NO CLASS MON Sept 1 Course WEB SITE crystal.uta.edu/~jpatters.
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
9/29: Lecture Topics Conditional branch instructions
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
MS108 Computer System I Lecture 3 ISA Prof. Xiaoyao Liang 2015/3/13 1.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Computer Organization Rabie A. Ramadan Lecture 3.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
CDA 3101 Spring 2016 Introduction to Computer Organization
Computer Organization Instructions Language of The Computer (MIPS) 2.
Ch2a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
10/1: Lecture Topics Conditional branch instructions –slides from 9/29 set Unconditional jump instructions Instruction encoding.
Lecture 17: 10/31/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS Assembly.
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Computer Architecture & Operations I
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
MIPS coding.
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instruction Encoding
MIPS Instruction Encoding
Lecture 5: Procedure Calls
Computer Architecture
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
MIPS Coding Continued.
MIPS coding.
Presentation transcript:

CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic3: Instructions, The Language of the Machine José Nelson Amaral

CMPUT Computer Organization and Architecture I2 Reading Assignment zChapter 3 of Hennessy and Patterson: ySections 3.1 to 3.5.

CMPUT Computer Organization and Architecture I3 Basics of the MIPS Architecture The MIPS architecture has 32 integer registers numbered from 0 to 31. The register 0 always contain the value 0. By software convention these 32 integer registers are divided into subgroup and most of them are referred by a letter and a number. For instance, compilers usually use registers named $s0, $s1, … to contain variables in a C program. Registers named $t0, $t1, … are typically used to store temporary values generated during the computation, but that have no correspondents in the variables in the C program.

CMPUT Computer Organization and Architecture I4 Compiling a C Assignment Using Registers C assignment: f = (g + h) - (i + j) Assumption: f  $s0 g  $s1 h  $s2 i  $s3 j  $s4 MIPS assembly: add $t0, $s1, $s2# $t0  g + h add $t1, $s3, $s4# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1

CMPUT Computer Organization and Architecture I5 When Operands are in Memory C assignment: A[12] = h + A[8] Assumption: h  $s2 base of array A  $s3 MIPS assembly: lw $t0, 32($s3)# $t0  A[8] add $t0, $s2, $t0# $t0  h + A[8] sw $t0, 48($s3)# A[12]  $t0

CMPUT Computer Organization and Architecture I6 Memory Organization In MIPS an integer is represented by 4 bytes. A memory address references a single byte. Therefore the difference between the addresses of two consecutive integers in memory is 4. For instance, if the numbers and were stored consecutively in memory at address 0x , the memory would contain: (remember that = ) 0x x x x x x x x x00 0x13 0xFF 0x97 AddressValue In this example, the memory address of is 0x , and the memory address of is 4 bytes ahead at 0x

CMPUT Computer Organization and Architecture I7 Endianess 0x x x x x x x x x00 0x13 0xFF 0x97 AddressValue In the previous example, there are two ways to store at the address 0x and at the address 0x : 0x x x x x x x x x13 0x00 0x97 0xFF AddressValue What is the difference?

CMPUT Computer Organization and Architecture I8 Little-End and Big-End = = 0x Little end of Big end of (binary) (hexadecimal) = = 0xFFFF FF97 Little end of Big end of (binary) (hexadecimal)

CMPUT Computer Organization and Architecture I9 Endianess 0x x x x x x x x x00 0x13 0xFF 0x97 AddressValue The question is: which end of the integer do we store first in memory? 0x x x x x x x x x13 0x00 0x97 0xFF AddressValue Big end of Little end of Little Endian Byte OrderBig Endian Byte Order DECstations and Intel 80x86 are little-endians. Sun SPARC and Macintosh are big-endians.

CMPUT Computer Organization and Architecture I10 Addressing Memory MIPS assembly: lw $t0, 32($s3)# $t0  A[8] add $t0, $s2, $t0# $t0  h + A[8] sw $t0, 48($s3)# A[12]  $t0 Now we can understand the lw and sw instructions in the MIPS Assembly: lw stands for “load word”, and sw stands for “store word”. A word is the most used data unit in a processor. In the MIPS a word has 4 bytes, or 32 bits. lw loads a word from an specified memory location into a register in the processor. sw stores a word from a register in the processor into the specified memory location.

CMPUT Computer Organization and Architecture I11 Addressing Memory MIPS assembly: lw $t0, 32($s3)# $t0  A[8] add $t0, $s2, $t0# $t0  h + A[8] sw $t0, 48($s3)# A[12]  $t0 Destination register for lw Source register for sw Source memory location for lw Destination memory location for sw

CMPUT Computer Organization and Architecture I12 Displacement Addressing Mode MIPS assembly: lw $t0, 32($s3)# $t0  A[8] add $t0, $s2, $t0# $t0  h + A[8] sw $t0, 48($s3)# A[12]  $t0 32($s3) tells the processor to read the value stored in register $s3, add 32 (8 words) to it, and use the result as the memory address for the load instruction

CMPUT Computer Organization and Architecture I13 Example of lw execution MIPS assembly: lw $t0, 32($s3)# $t0  A[8] $t0 $t1 $t2 $s0 $s1 $s2 $s Address Bus

CMPUT Computer Organization and Architecture I14 Example of lw execution MIPS assembly: lw $t0, 32($s3)# $t0  A[8] $t0 $t1 $t2 $s0 $s1 $s2 $s Memory Cells Memory Controller Address Bus

CMPUT Computer Organization and Architecture I15 Example of lw execution MIPS assembly: lw $t0, 32($s3)# $t0  A[8] $t0 $t1 $t2 $s0 $s1 $s2 $s Memory Cells Memory Controller Address Bus Data Bus

CMPUT Computer Organization and Architecture I16 Example of sw execution MIPS assembly: sw $t0, 48($s3)# A[12]  $t0 $t0 $t1 $t2 $s0 $s1 $s2 $s Address Bus Data Bus

CMPUT Computer Organization and Architecture I17 Example of sw execution MIPS assembly: sw $t0, 48($s3)# A[12]  $t0 $t0 $t1 $t2 $s0 $s1 $s2 $s Memory Cells Memory Controller Address Bus Data Bus

CMPUT Computer Organization and Architecture I18 Example of sw execution MIPS assembly: sw $t0, 48($s3)# A[12]  $t0 $t0 $t1 $t2 $s0 $s1 $s2 $s Memory Cells Memory Controller Address Bus Data Bus

CMPUT Computer Organization and Architecture I19 Representing Instructions: R-Type Instructions add $t0, $s1, $s2# (R8 = $t0, R17 = $s1, R18 = $s2) oprsrtrdshamtfunct R-Type Instruction Format op: Opcode to specify the type of instruction; rs:first register source operand rt: second register source operand rd:register destination operand shamt: used for shift instructions (we will examine them later) funct: function code, specify the variant of the instruction to be executed

CMPUT Computer Organization and Architecture I20 Representing Instructions: R-Type Instructions add $t0, $s1, $s2# (R8 = $t0, R17 = $s1, R18 = $s2) oprsrtrdshamtfunct R-Type Instruction Format In memory we would see: 0x op: Opcode to specify the type of instruction; rs:first register source operand rt: second register source operand rd:register destination operand shamt: used for shift instructions (we will examine them later) funct: function code, specify the variant of the instruction to be executed

CMPUT Computer Organization and Architecture I21 Representing Instructions: R-Type Instructions add $t0, $s1, $s2# (R8 = $t0, R17 = $s1, R18 = $s2) oprsrtrdshamtfunct R-Type Instruction Format In memory we would see: 0x op: Opcode to specify the type of instruction; rs:first register source operand rt: second register source operand rd:register destination operand shamt: used for shift instructions (we will examine them later) funct: function code, specify the variant of the instruction to be executed

CMPUT Computer Organization and Architecture I22 Representing Instructions: R-Type Instructions add $t0, $s1, $s2# (R8 = $t0, R17 = $s1, R18 = $s2) oprsrtrdshamtfunct R-Type Instruction Format In memory we would see: 0x op: Opcode to specify the type of instruction; rs:first register source operand rt: second register source operand rd:register destination operand shamt: used for shift instructions (we will examine them later) funct: function code, specify the variant of the instruction to be executed

CMPUT Computer Organization and Architecture I23 Representing Instructions: I format Instructions lw $t0, 32($s3)# (R8 = $t0, R19 = $s3) # $t0  Memory[$s3 + 32] OpCodersrtaddress I-Type Instruction Format In memory we would see: 0x

CMPUT Computer Organization and Architecture I24 Representing Instructions: I format Instructions lw $t0, 32($s3)# (R8 = $t0, R19 = $s3) # $t0  Memory[$s3 + 32] OpCodersrtaddress I-Type Instruction Format In memory we would see: 0x8E680020

CMPUT Computer Organization and Architecture I25 Representing Instructions: Memory Instructions sw $t0, 1200($t1) # (R8 = $t0, R9 = $t1) # Memory[$t ]  $t OpCodersrtaddress I-Type Instruction Format In memory we would see: 0xAE2404B8

CMPUT Computer Organization and Architecture I26 Representing Instructions: Memory Instructions sw $t0, 1200($t1) # (R8 = $t0, R9 = $t1) # Memory[$t ]  $t OpCodersrtaddress I-Type Instruction Format In memory we would see: 0xAE2404B8

CMPUT Computer Organization and Architecture I27 Instructions for Making Decisions C code: if (i == j) f = g + h; else f = g - h;... Assumption: f  $s0 g  $s1 h  $s2 i  $s3 j  $s4 MIPS assembly: bne$s3, $s4, Else# if i  j goto Else add $s0, $s1, $s2# f  g + h jExit# goto Exit Else:sub$s0, $s1, $s2# f  g - h Exit:...

CMPUT Computer Organization and Architecture I28 Representing Instructions: Branch Instructions bne $s3, $s4, 8  PC  PC + 4 if($1  $2) PC  PC OpCodersrtaddress I-Type Instruction Format MIPS assembly: 0x bne$s3, $s4, Else# if i  j goto Else 0x add $s0, $s1, $s2# f  g + h 0x jExit# goto Exit 0x C Else:sub$s0, $s1, $s2# f  g - h 0x Exit:... In memory we would see: 0x R19 = $s3, R20=$s4 Internally the processor multiplies this constant by 4 to obtain the distance of 8 bytes required.

CMPUT Computer Organization and Architecture I29 Representing Instructions: Jump Instructions j Exit  PC  concat(PC[31-28],IR[25-0])<< OpCodeaddress I-Type Instruction Format MIPS assembly: 0x bne$s3, $s4, Else# if I j goto Else 0x add $s0, $s1, $s2# f  g + h 0x jExit# goto Exit 0x B Else:sub$s0, $s1, $s2# f  g - h 0x Exit:... In memory we would see: 0x

CMPUT Computer Organization and Architecture I30 Representing Instructions: Jump Instructions j Exit  PC  concat(PC[31-28],IR[25-0])<< OpCodeaddress MIPS assembly: 0x bne$s3, $s4, Else# if I j goto Else 0x add $s0, $s1, $s2# f  g + h 0x jExit# goto Exit 0x B Else:sub$s0, $s1, $s2# f  g - h 0x Exit:... PC  PC  0x

CMPUT Computer Organization and Architecture I31 While Loops C code: while (save[i] == k) i = i + j;... Assumption: i  $s3 j  $s4 k  $s5 base of save[ ]  $s6 MIPS assembly: Loop:add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[i]  k goto Exit add$s3, $s3, $s4# i  i + j jLoop# goto Loop Exit: How can we improve this code?

CMPUT Computer Organization and Architecture I32 While Loops C code: while (save[i] == k) i = i + j;... MIPS assembly: Loop:add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit add$s3, $s3, $s4# i  i + j jLoop# goto Loop Exit: load save[i]

CMPUT Computer Organization and Architecture I33 While Loops C code: while (save[i] == k) i = i + j;... MIPS assembly: Loop:add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit add$s3, $s3, $s4# i  i + j jLoop# goto Loop Exit: load save[i] save[i]  k? Exit yes

CMPUT Computer Organization and Architecture I34 While Loops C code: while (save[i] == k) i = i + j;... MIPS assembly: Loop:add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit add$s3, $s3, $s4# i  i + j jLoop# goto Loop Exit: load save[i] i  i + j no save[i]  k? Exit yes

CMPUT Computer Organization and Architecture I35 While Loops C code: while (save[i] == k) i = i + j;... MIPS assembly: Loop:add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit add$s3, $s3, $s4# i  i + j jLoop# goto Loop Exit: load save[i] goto Loop i  i + j no save[i]  k? Exit yes

CMPUT Computer Organization and Architecture I36 While Loops C code: while (save[i] == k) i = i + j;... load save[i] i  i + j goto Loop no save[i]  k? Exit yes i  i + j no load save[i] save[i]  k? Exit yes load save[i] save[i] = k? yes Exit no Before Improvement After Improvement

CMPUT Computer Organization and Architecture I37 While Loops load save[i] MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[I]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I38 While Loops load save[i] save[i]  k? Exit yes MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[i]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I39 While Loops i  i + j no load save[i] save[i]  k? Exit yes MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[i]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I40 While Loops i  i + j no load save[i] save[i]  k? Exit yes load save[i] MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[i]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I41 While Loops i  i + j no load save[i] save[i]  k? Exit yes load save[i] save[i] = k? yes Exit no MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[i]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I42 While Loops i  i + j no load save[i] save[i]  k? Exit yes load save[i] save[i] = k? yes Exit no MIPS assembly: add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] bne$t0, $s5, Exit# if save[I]  k goto Exit Loop:add$s3, $s3, $s4# i  i + j add$t1, $s3, $s3# $t1  2 * i add $t1, $t1, $t1# $t1  4 * i add$t1, $t1, $s6# $t1  Addr(save[i]) lw$t0, 0($t1)# $t0  MEM[save[i]] beq$t0, $s5, Loop# if save[i]  k goto Exit Exit:

CMPUT Computer Organization and Architecture I43 Set on Less Than Instruction C code: if (a < b) goto Less... Assumption: a  $s0 b  $s1 MIPS assembly: slt $t0, $s0, $s1 # if ($s0 < $s1) $t0  1 else $t0  0 bne $t0, $zero, Less # if $t0  0 goto Less... Less:

CMPUT Computer Organization and Architecture I44 Case/Switch Statement We will use a jump address table to encode the values of the cases and the addresses of the sequence of instructions that should be executed in each case. MIPS has a jump register (jr) instruction to support jump address tables: jr $t1 is an unconditional jump to the address in register $t1

CMPUT Computer Organization and Architecture I45 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly: slt$t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k<0, goto Exit slt$t3, $s5, $t2# Test if k < 4 beq$t3, $zero, Exit# if k  4, goto Exit add$t1, $s5, $s5# $t1  2*k add$t1, $t1, $t1# $t1  4*k add$t1, $t1, $t4# $t1  Addr[JumpTable[k]] lw$t0, 0($t1)# $t0  JumpTable[k] jr$t0# jump to address in $t0

CMPUT Computer Organization and Architecture I46 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly: slt$t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k<0, goto Exit slt$t3, $s5, $t2# Test if k < 4 beq$t3, $zero, Exit# if k  4, goto Exit add$t1, $s5, $s5# $t1  2*k add$t1, $t1, $t1# $t1  4*k add$t1, $t1, $t4# $t1  Addr[JumpTable[k]] lw$t0, 0($t1)# $t0  JumpTable[k] jr$t0# jump to address in $t0 Step 1: Test if 0  k < 4

CMPUT Computer Organization and Architecture I47 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly: slt$t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k<0, goto Exit slt$t3, $s5, $t2# Test if k < 4 beq$t3, $zero, Exit# if k  4, goto Exit add$t1, $s5, $s5# $t1  2*k add$t1, $t1, $t1# $t1  4*k add$t1, $t1, $t4# $t1  Addr[JumpTable[k]] lw$t0, 0($t1)# $t0  JumpTable[k] jr$t0# jump to address in $t0 Step 2: multiply k by 4 to index 32 bit addresses: $t1  4*k

CMPUT Computer Organization and Architecture I48 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly: slt$t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k<0, goto Exit slt$t3, $s5, $t2# Test if k < 4 beq$t3, $zero, Exit# if k  4, goto Exit add$t1, $s5, $s5# $t1  2*k add$t1, $t1, $t1# $t1  4*k add$t1, $t1, $t4# $t1  Addr[JumpTable[k]] lw$t0, 0($t1)# $t0  JumpTable[k] jr$t0# jump to address in $t0 Step 3: Load address of Jump Table into $t1

CMPUT Computer Organization and Architecture I49 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly: slt$t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k<0, goto Exit slt$t3, $s5, $t2# Test if k < 4 beq$t3, $zero, Exit# if k  4, goto Exit add$t1, $s5, $s5# $t1  2*k add$t1, $t1, $t1# $t1  4*k add$t1, $t1, $t4# $t1  Addr[JumpTable[k]] lw$t0, 0($t1)# $t0  JumpTable[k] jr$t0# jump to address in $t0 Step 4: Load address of code for case in $t0

CMPUT Computer Organization and Architecture I50 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly (cont.): L0:add$s0, $s3, $s4# f  i + j jExit L1:add$s0, $s1, $s2# f  g + h jExit L2:sub$s0, $s1, $s2# f  g - h jExit L3:sub$s0, $s3, $s4# i  i - j Exit:...

CMPUT Computer Organization and Architecture I51 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly (cont.): L0:add$s0, $s3, $s4# f  i + j jExit L1:add$s0, $s1, $s2# f  g + h jExit L2:sub$s0, $s1, $s2# f  g - h jExit L3:sub$s0, $s3, $s4# i  i - j Exit:...

CMPUT Computer Organization and Architecture I52 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly (cont.): L0:add$s0, $s3, $s4# f  i + j jExit L1:add$s0, $s1, $s2# f  g + h jExit L2:sub$s0, $s1, $s2# f  g - h jExit L3:sub$s0, $s3, $s4# i  i - j Exit:...

CMPUT Computer Organization and Architecture I53 Case/Switch Statement C code: switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Assumptions: i  $s3f  $s0 j  $s4g  $s1 k  $s5h  $s2 4  $t2 Initial address of Jump Table  $t4 MIPS assembly (cont.): L0:add$s0, $s3, $s4# f  i + j jExit L1:add$s0, $s1, $s2# f  g + h jExit L2:sub$s0, $s1, $s2# f  g - h jExit L3:sub$s0, $s3, $s4# i  i - j Exit:...