Lecture 4 section 2.4-?? As opposed to CISC – Complicated Instruction Set Architecture (ala the x86)

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
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
Lecture 5: MIPS Instruction Set
The University of Adelaide, School of Computer Science
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Chapter 2 Instructions: Language of the Computer
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
CS325 Instructions: Language of the Machine MIPS ARCHITECTURE - AN INTRODUCTION TO THE INSTRUCTION SET by N. Guydosh 2/2/04+
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
331 W02.1Spring 05 Announcements  HW1 is due on this Friday  Appendix A (on CD) is very helpful to HW1.
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
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
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
331 W02.1Fall 2003 Announcements  HW1 is due on this Friday (Sept 12 th )  Appendix A is very helpful to HW1. Check out system calls on Page A-48. 
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
MIPS Instruction Set Advantages
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
ISA-2 CSCE430/830 MIPS: Case Study of Instruction Set Architecture CSCE430/830 Computer Architecture Instructor: Hong Jiang Courtesy of Prof. Yifeng Zhu.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Computing Systems Instructions: language of the computer.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
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.
1  1998 Morgan Kaufmann Publishers Machine Instructions: Language of the Machine Lowest level of programming, control directly the hardware Assembly instructions.
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 Architecture (CS 207 D) Instruction Set Architecture ISA.
CMPE 325 Computer Architecture II
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization Rabie A. Ramadan Lecture 3.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
CDA 3101 Spring 2016 Introduction to Computer Organization
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic3: Instructions, The Language of the Machine José Nelson Amaral.
Chapter 2 Instructions: Language of the Computer.
CS35101 Computer Architecture Spring 2006 Week 2.
MIPS Assembly.
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Computer Instructions
COMS 361 Computer Organization
CSC3050 – Computer Architecture
Computer Architecture
Presentation transcript:

Lecture 4 section 2.4-?? As opposed to CISC – Complicated Instruction Set Architecture (ala the x86)

MIPS Reference Data

Class Participation 1. The following is a MIPS ML instruction: 00000010001100100100000000100000 What AL does it represent? (Use the green sheet to decode it). add $t0, $s1, $s2 As opposed to CISC – Complicated Instruction Set Architecture (ala the x86) b. The following is a MIPS AL instruction: add $t0, $s1, $s2 What is the ML MIPS equivalent? 00000010001100100100000000100000

Machine Language - Arithmetic Instruction Instructions, like registers and words of data, are also 32 bits long Example: add $t0, $s1, $s2 registers have numbers $t0=$8, $s1=$17, $s2=$18 Instruction Format: Do you know what the field names stand for? op rs rt rd shamt funct 000000 10001 10010 01000 00000 100000 For class handout

Machine Language - Arithmetic Instruction Instructions, like registers and words of data, are also 32 bits long Example: add $t0, $s1, $s2 registers have numbers $t0=$8, $s1=$17, $s2=$18 Instruction Format: Do you know what the field names stand for? op rs rt rd shamt funct 000000 10001 10010 01000 00000 100000 For lecture

MIPS Instruction Fields op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits = 32 bits op rs rt rd shamt funct for class handout

MIPS Instruction Fields op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits = 32 bits op rs rt rd shamt funct opcode indicating operation to be performed address of the first register source operand address of the second register source operand the register destination address for lecture shift amount (for shift instructions) function code that selects the specific variant of the operation specified in the opcode field

Machine Language - Load Instruction Consider the load-word and store-word instructions, What would the regularity principle have us do? New principle: Good design demands a compromise Introduce a new type of instruction format I-type for data transfer instructions previous format was R-type for register Example: lw $t0, 24($s2) Where's the compromise? op rs rt 16 bit number For class handout 35 18 8 24 100011 10010 01000 0000000000011000

Machine Language - Load Instruction Consider the load-word and store-word instructions, What would the regularity principle have us do? New principle: Good design demands a compromise Introduce a new type of instruction format I-type for data transfer instructions previous format was R-type for register Example: lw $t0, 24($s2) Where's the compromise? op rs rt 16 bit number For lecture destination address no longer in the rd field - now in the rt field offset limited to 16 bits - so can’t get to every location in memory (with a fixed base address) 35 18 8 24 100011 10010 01000 0000000000011000

Memory Address Location Example: lw $t0, 24($s2) Memory 0xf f f f f f f f 2410 + $s2 = 0x00000002 0x12004094 $s2 For class handout 0x0000000c Note that the offset can be positive or negative 0x00000008 0x00000004 0x00000000 data word address (hex)

Memory Address Location Example: lw $t0, 24($s2) Memory 0xf f f f f f f f 2410 + $s2 = 0x00000002 . . . 1001 0100 + . . . 0001 1000 . . . 1010 1100 = 0x120040ac 0x12004094 $s2 For lecture What is the address of the memory location that gets loaded into $t0? …10010100 …00011000 -------------- …10101100= 0x020040ac 0x0000000c Note that the offset can be positive or negative 0x00000008 0x00000004 0x00000000 data word address (hex)

Machine Language - Store Instruction Example: sw $t0, 24($s2) A 16-bit address means access is limited to memory locations within a region of 213 or 8,192 words (215 or 32,768 bytes) of the address in the base register $s2 op rs rt 16 bit number 43 18 8 24 101011 10010 01000 0000000000011000 For class handout

Machine Language - Store Instruction Example: sw $t0, 24($s2) A 16-bit address means access is limited to memory locations within a region of 213 or 8,192 words (215 or 32,768 bytes) of the address in the base register $s2 op rs rt 16 bit number 43 18 8 24 101011 10010 01000 0000000000011000 For lecture why is it 2**13 (2**15) and NOT 2**14 (2**16) like we just saw with memory (here we have to represent signed numbers so the most significant bit is the sign bit)

Assembling Code Remember the assembler code we compiled last lecture for the C statement A[8] = A[2] - b lw $t0, 8($s3) #load A[2] into $t0 sub $t0, $t0, $s2 #subtract b from A[2] sw $t0, 32($s3) #store result in A[8] Assemble the MIPS object code for these three instructions For class handout

Assembling Code Remember the assembler code we compiled for the C statement A[8] = A[2] - b lw $t0, 8($s3) #load A[2] into $t0 sub $t0, $t0, $s2 #subtract b from A[2] sw $t0, 32($s3) #store result in A[8] Assemble the MIPS code for these three instructions 35 lw 19 8 For lecture lw 35 19 8 8 sub 0 8 18 8 0 34 sw 43 19 8 32 sub 8 18 34 43 sw 19 8 32

Review: MIPS Data Types Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits is a word 64 bits is a double-word Character: ASCII 7 bit code Decimal: digits 0-9 encoded as 0000b thru 1001b two decimal digits packed per 8 bit byte Integers: 2's complement Floating Point

Beyond Numbers Most computers use 8-bit bytes to represent characters with the American Std Code for Info Interchange (ASCII) So, we need instructions to move bytes around ASCII Char Null 32 space 48 64 @ 96 ` 112 p 1 33 ! 49 65 A 97 a 113 q 2 34 “ 50 66 B 98 b 114 r 3 35 # 51 67 C 99 c 115 s 4 EOT 36 $ 52 68 D 100 d 116 t 5 37 % 53 69 E 101 e 117 u 6 ACK 38 & 54 70 F 102 f 118 v 7 39 ‘ 55 71 G 103 g 119 w 8 bksp 40 ( 56 72 H 104 h 120 x 9 tab 41 ) 57 73 I 105 i 121 y 10 LF 42 * 58 : 74 J 106 j 122 z 11 43 + 59 ; 75 K 107 k 123 { 12 FF 44 , 60 < 76 L 108 l 124 | 15 47 / 63 ? 79 O 111 o 127 DEL see Figure 3.15 in the book for the complete listing - note that we are only using 7 of the 8 bits (7 bits gives us 127 encodings) - the extra bit can be used as a parity bit to detect single bit errors during transmission Note that upper and lower case letters differ by exactly 32 13 is carriage return 0 is Null (marking the end of string in C)

Loading and Storing Bytes MIPS provides special instructions to move bytes lb $t0, 1($s3) #load byte from memory sb $t0, 6($s3) #store byte to memory What 8 bits get loaded and stored? load byte places the byte from memory in the rightmost 8 bits of the destination register what happens to the other bits in the register? store byte takes the byte from the rightmost 8 bits of a register and writes it to a byte in memory I op rs rt 16 bit number load byte takes the contents of the byte at the memory address specified, zero-extends it, and loads it into the register

Example of Loading and Storing Bytes Given following code sequence and memory state (contents are given in hexadecimal), what is the state of the memory after executing the code? add $s3, $zero, $zero lb $t0, 1($s3) sb $t0, 6($s3) Memory 0 0 0 0 0 0 0 0 24 What value is left in $t0? 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 16 For class handout What if the machine was little Endian? 1 0 0 0 0 0 1 0 12 0 1 0 0 0 4 0 2 8 F F F F F F F F 4 0 0 9 0 1 2 A 0 Data Word Address (Decimal)

MIPS Reference Data lb – load byte – loads a byte from memory, placing it in the rightmost 8 bits of a register. sb – store byte – takes a byte from the rightmost 8 bits of a register and writes it to memory

Example of Loading and Storing Bytes Given following code sequence and memory state (contents are given in hexadecimal), what is the state of the memory after executing the code? add $s3, $zero, $zero lb $t0, 1($s3) #lbu $t0, 1($s0)gives sb $t0, 6($s3) #$t0 = 0x00000090 mem(4) = 0xFFFF90FF Memory 0 0 0 0 0 0 0 0 24 Questions 2 : What value is left in $t0? 0 0 0 0 0 0 0 0 20 $t0 = 0xFFFFFF90 0 0 0 0 0 0 0 0 16 For lecture Big Endian Little Endian 0 1 2 3 3 2 1 0 $t0 gets 90 $t0 gets 12 word 4 gets FFFF90FF word 4 gets FF12FFFF 1 0 0 0 0 0 1 0 12 Questions 3: What if the machine was little Endian? 0 1 0 0 0 4 0 2 8 F F F F F F F F 4 mem(4) = 0xFF12FFFF $t0 = 0x00000012 0 0 9 0 1 2 A 0 Data Word Address (Decimal) * Class Participation

Review: MIPS Instructions, so far Category Instr Op Code Example Meaning Arithmetic (R format) add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3 Data transfer (I format) load word 35 lw $s1, 100($s2) $s1 = Memory($s2+100) store word 43 sw $s1, 100($s2) Memory($s2+100) = $s1 load byte 32 lb $s1, 101($s2) $s1 = Memory($s2+101) store byte 40 sb $s1, 101($s2) Memory($s2+101) = $s1 similarity of the binary representation of related instructions simplifies the hardware design

Review: MIPS R3000 ISA Instruction Categories Load/Store Computational Jump and Branch Floating Point coprocessor Memory Management Special 3 Instruction Formats: all 32 bits wide Registers R0 - R31 PC HI LO 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits R format OP rs rt rd shamt funct I format OP rs rt 16 bit number J format OP 26 bit jump target

Register Instructions Assume: g, h, i, j are stored in registers $s1 - $s4. Result f to be stored in $s0. Compile: f = (g + h) – (i + j) into MIPS instructions: add $t0, $s1, $s2 # register $t0  (g+h) add $t1, $s3, $s4 # $t1  (i + j) sub $s0, $t0, $t1 # $s0  $t0 - $t1 OP rs rt rd shft funct R-type

Immediate format Immediate = small constant stored in the instruction addi $sp, $sp, const # $sp  $sp + const OP const I-type 16 5 6 rt rs Range of constant operand: -215 ≤ const  215-1 Set on less-then, immediate slti $t0, $s2, const # $t0  1 if $s2 < const ; 0 otherwise

Operand in memory Let A[ ] = array whose starting (base) address is in $s3; let variable h be associated with register $s2; Compile: A[5] = h + A[8] into MIPS instructions: lw $t0, 32 ($s3) # $t0  A[8] add $t0, $s2, $t0 # $t0  h+$t0 sw $t0, 20 ($s3) # A[5]  $t0 8*4 = 32 bytes (byte-addressable) 8 7 6 5 4 3 2 1 $s3 5*4 = 20 OP rs rt immediate I-type

Array with variable index A[ ] = array with base address in $s3; variables g, h, i associated with registers $s1, $s2, $s4 Compile: g = h + A[i] into MIPS instructions: add $t1, $s4, $s4 # $t1  i+i = 2i add $t1, $t1, $t1 # $t1  2i+2i = 4i add $t1, $t1, $s3 # $t1  address of A[i] lw $t0, 0 ($t1) # $t0  A[i] add $s1, $s2, $t0 # $s1  h + A[i]

If statements Let variables f, g, h, i, j be associated with $s0 - $s4 Compile: if (i == j) go to L1; f = g + h; L1: f = f – i; into MIPS instructions: Does the following code work? beq $s3, $s4, L1 # if i = j, go to L1 add $s0, $s1, $s2 # f = g + h (skipped if i=j) L1: sub $s0, $s0, $s3 # f = f – i (always executed)

Jump instructions Regular jump uses J-format j Label # jump to Label 26 OP Label = jump target J-type (the above label is not quite this simple) jr (jump on register address) uses R-format jr $t1 # jump to address given in $t1 OP rs funct R-type OP/funct = jr

If then else statements Let variables f, g, h, i, j be associated with $s0 - $s4 Compile: if (i == j) f = g + h; else f = g –h; into MIPS instructions: bne $s3, $s4, Else # if i  j, go to Else add $s0, $s1, $s2 # f = g + h (skipped if i  j) j Exit # go to Exit Else: sub $s0, $s1, $s2 # f = g – h (skipped if i = j) Exit: OP jump target J-type

Pseudo-instructions Let variables a, b be associated with $s0, $s1 Compile: if (a < b) go to L into MIPS instructions: slt $t0, $s0, $s1 # $t0  1 if $s0 < $s1 (a < b) bne $t0, $zero, L # if $t0  0, go to L We can create pseudo-instruction: blt $s0, $s1, L Special registers, useful in beq, bne, slt: $zero is a special register, holds 0 $at is another special register used in the blt pseudo-instruction It generates the above two instructions with $at replacing $t0

Branching further away -215 ≤ small constant offset  215-1 Consider a branch instruction beq $s0, $s1, L1 # branch to L1 if $s0 = $s1 OP rs rt immediate I-type 16 5 6 If the offset does not fit in the above range the compiler (or programmer) changes it to the following sequence to get a larger distance. bne $s0, $s1, L2 # branch to L2 if $s0 $s1 j Large # unconditional jump to “Large” L2: OP Large – jump target J-type 6 26

Loading large numbers Instruction lui: load upper immediate lui $t0, const # $t0[31:16]  const OP const I-type 16 5 6 rt 16 $to: const 31 15 0000 0000 0000 0000

MIPS Addressing Modes/Instruction Formats All instructions are 32-bit wide op rs rt rd register Register (direct) immed op rs rt Immediate Base+index immed op rs rt register + Memory PC-relative immed op rs rt PC Memory + Also there is pseudodirect addressing as is used by jump instructions

Operation Summary Support these simple instructions, since they will dominate the number of instructions executed: load, store, add, subtract, move register-register, and, shift, compare equal, compare not equal, branch, jump, call, return;

Additional MIPS Instructions As opposed to CISC – Complicated Instruction Set Architecture (ala the x86)

MIPS instructions, formats MIPS instructions: data transfers, arithmetic, logical Pseudo-instruction example: loading large constant MIPS register organization Implementing loops Implementing switch/case statement Procedures and subroutines Stacks and pointers Running a program Compiler, Assembler, Linker, Loader c$redential: b$ring a compute$r die photo wafe$r : This can be an hidden slide. I just want to use this to do my own planning. I have $rea$r$ranged Culle$r’s lectu$re slides slightly and add mo$re slides. This cove$rs eve$rything he cove$rs in his fi$rst lectu$re (and mo$re) but may We will save the fun pa$rt, “ Levels of O$rganization,” at the end (so student can stay awake): I will show the inte$rnal st$rictu$re of the SS10/20. Notes to Patte$rson: You may want to edit the slides in you$r section o$r add ext$ra slides to taylo$r you$r needs.

MIPS: Software conventions for registers R0 $zero constant 0 R1 $at reserved for assembler R2 $v0 value registers & R3 $v1 function results R4 $a0 arguments R5 $a1 R6 $a2 R7 $a3 R8 $t0 temporary: caller saves . . . (callee can clobber) R15 $t7 R16 $s0 callee saves . . . (caller can clobber) R23 $s7 R24 $t8 temporary (cont’d) R25 $t9 R26 $k0 reserved for OS kernel R27 $k1 R28 $gp pointer to global area R29 $sp Stack pointer R30 $fp Frame pointer R31 $ra return Address

MIPS data transfer instructions Instruction Comment sw 500($r4), $r3 Store word sh 502($r2), $r3 Store half sb 41($r3), $r2 Store byte lw $r1, 30($r2) Load word lh $r1, 40($r3) Load halfword lhu $r1, 40($r3) Load halfword unsigned lb $r1, 40($r3) Load byte lbu $r1, 40($r3) Load byte unsigned lui $r1, 40 Load Upper Immediate (16 bits shifted left by 16) 0000 … 0000 LUI $r5 $r5

Loading large numbers Pseudo-instruction li $t0, big: load 32-bit constant lui $t0, upper # $t0[31:16]  upper ori $t0, $t0, lower # $t0  ($t0 Or [0ext.lower]) upper 16 31 15 0000 0000 0000 0000 OR lower 0000 0000 0000 0000 $to: upper lower 32-bit constant

Loop with variable array index Compile the following loop, with A[ ] = array with base address in $s5; variables g, h, i, j associated with registers $s1, $s2, $s3, $s4. Loop: g = g + A[i]; i = i + j; if (i  h) go to Loop; MIPS instructions: Loop: add $t1, $s3, $s3 # $t1  i+i = 2i add $t1, $t1, $t1 # $t1  2i+2i = 4i add $t1, $t1, $s5 # $t1  address of A[i] lw $t0, 0 ($t1) # $t0  A[i] add $s1, $s1, $t0 # $s1  g + A[i] add $s3, $s3, $s4 # $s3  i + j bne $s3, $s2, Loop # if (i  h) go to Loop

While loop while (A[i] == k) i = i + j; into MIPS instructions: Base address of A[i] is in $s6; variables i, j, k are in $s3, $s4, $s5. Compile the following while loop while (A[i] == k) i = i + j; into MIPS instructions: Loop: add $t1, $s3, $s3 # $t1  i+i = 2i add $t1, $t1, $t1 # $t1  2i+2i = 4i add $t1, $t1, $s6 # $t1  address of A[i] lw $t0, 0 ($t1) # $t0  A[i] bne $t0, $s5, Exit # if (A[I]  k) go to Exit add $s3, $s3, $s4 # $s3  i + j j Loop # go to Loop Exit:

Switch/case statement Variables f - k are in $s0 - $s5. Register $t2 contains constant 4. Compile the following switch statement into MIPS instructions switch (k) { case 0: f = i + j; break; /* k=0 */ case 1: f = g + h; break; /* k=1 */ case 2: f = g - h; break; /* k=2 */ case 3: f = i - j; break; /* k=3 */ } Use the switch variable k to index the jump address table. First, test for k, if in correct range (0-3). slt $t3, $s5, $zero # test if k , 0 bne $t3, $zero, Exit # if k < 0, go to Exit slt $t3, $s5, $t2 # test if k < 4 beq $t3, $zero, Exit # if k  4, go to Exit

Switch statement, cont’d Access jump table T [ ] with addresses L0,L1,L2,L3: $t4 is address of T add $t1, $s5, $s5 # $t1  2k add $t1, $t1, $t1 # $t1  4k add $t1, $t1, $t4 # $t1  address of T [k] lw $t0, 0 ($t1) # $t0  T [k] (loads one of L0, L1, L2, L3) Use jump register instruction to jump via $t0 to the right address jr $t0 # jump based on register $t0 L0: add $s0, $s3, $s4 # k = 0, so f=$s0  i + j j Exit # go to Exit L1: add $s0, $s1, $s2 # k = 1, so f=$s0  g + h L2: sub $s0, $s1, $s2 # k = 2, so f=$s0  g + h L3: sub $s0, $s3, $s4 # k = 3, so f=$s0  i - j Exit:

Compiling a leaf procedure (not nested) int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } Let parameter variables g, h, i, j, correspond to the argument registers $a0, $a1, $a2, $a3. Will use temp. registers $t0= (g + h) and $t1=(i + j). Function f will be stored in $s0. Steps: Save the old values of registers ($s0, $t0, $t1) on stack (push) Issue a jal sub_address instruction ($ra  ret_addr, j sub_address) Perform the computation for $t0, $t1, $s0 using argument registers Copy the value of f into a return value register $v0 Restore the old values of the saved registers from stack (pop) Finally, jump back to the calling routine, jr $ra (PC  return_address=PC+4)

Compiling a leaf procedure, cont’d Leaf_example: # label of the procedure Save the old values of registers ($s0, $t0, $t1) on stack (push) sub $sp, $sp, 12 # adjust stack to make room for 3 items sw $t1, 8 ($sp) # save reg $t1 on stack ……. # repeat for $t0, $s0 Perform the computation for $t0, $t1, $s0 using argument registers add $t0, $a0, $a1 # $t0  g + h add $t1, $a2, $a3 # $t1  i + j sub $s0, $t0, $t1 # $s0  (g + h) – (i + j) Copy the value of f into a return value register $v0 add $v0, $s0, $zero # returns f ($v0  $s0 + 0) Restore the old values of the saved registers from stack (pop) lw $s0, 0 ($sp) # restore reg. $s0 for the caller ……. # repeat for $t0, $t1 … add $sp, $sp, 12 # adjust the stack to delete 3 items Finally, jump back to the calling routine (PC  return address) jr $ra # PC  $ra