Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need.
Intro to Computer Architecture
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
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.
Run time vs. Compile time
Run-time Environment and Program Organization
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
Compiler Construction
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4.
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 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Fall 2004ICOM 4036 Programming Laguages Lecture 2 1 Low-Level Programming Prof. Bienvenido Velez ICOM 4036 Lecture 2.
Spring 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Fall 2002INEL 4206 Microprocessors Lecture 5 1 Programming Universal Computers Instruction Sets Prof. Bienvenido Velez Lecture 5.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Lecture 5: Procedure Calls
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
Run-time organization
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instructions.
Low-Level Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
COMS 361 Computer Organization
Operating Systems Run-time Organization
Lecture 6: Assembly Programs
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8

Prof. Necula CS 164 Lecture 142 Status We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis Next are the back-end phases –Optimization –Code generation We’ll do code generation first...

Prof. Necula CS 164 Lecture 143 Run-time environments Before discussing code generation, we need to understand what we are trying to generate There are a number of standard techniques for structuring executable code that are widely used

Prof. Necula CS 164 Lecture 144 Outline Management of run-time resources Correspondence between static (compile-time) and dynamic (run-time) structures Storage organization

Prof. Necula CS 164 Lecture 145 Run-time Resources Execution of a program is initially under the control of the operating system When a program is invoked: –The OS allocates space for the program –The code is loaded into part of the space –The OS jumps to the entry point (i.e., “main”)

Prof. Necula CS 164 Lecture 146 Memory Layout Low Address High Address Memory Code Other Space

Prof. Necula CS 164 Lecture 147 Notes Our pictures of machine organization have: –Low address at the top –High address at the bottom –Lines delimiting areas for different kinds of data These pictures are simplifications –E.g., not all memory need be contiguous In some textbooks lower addresses are at bottom

Prof. Necula CS 164 Lecture 148 What is Other Space? Holds all data for the program Other Space = Data Space Compiler is responsible for: –Generating code –Orchestrating use of the data area

Prof. Necula CS 164 Lecture 149 Code Generation Goals Two goals: –Correctness –Speed Most complications in code generation come from trying to be fast as well as correct

Prof. Necula CS 164 Lecture 1410 Assumptions about Execution 1.Execution is sequential; control moves from one point in a program to another in a well- defined order 2.When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold?

Prof. Necula CS 164 Lecture 1411 Activations An invocation of procedure P is an activation of P The lifetime of an activation of P is –All the steps to execute P –Including all the steps in procedures that P calls

Prof. Necula CS 164 Lecture 1412 Lifetimes of Variables The lifetime of a variable x is the portion of execution in which x is defined Note that –Lifetime is a dynamic (run-time) concept –Scope is a static concept

Spring 2006ICOM 4036 Programming Laguages Lecture 4 13 The MIPS Architecture ISA at a Glance Reduced Instruction Set Computer (RISC) 32 general purpose 32-bit registers Load-store architecture: Operands in registers Byte Addressable 32-bit address space

Spring 2006ICOM 4036 Programming Laguages Lecture 4 14 The MIPS Architecture 32 Register Set (32-bit registers) Register #Reg NameFunction r0 Zero constant r4-r7a0-a3Function arguments r1atReserved for Operating Systems r30fpFrame pointer r28gpGlobal memory pointer r26-r27k0-k1Reserved for OS Kernel r31raFunction return address r16-r23s0-s7Callee saved registers r29spStack pointer r8-r15t0-t7Temporary variables r24-r25t8-t9Temporary variables r2-r3v0-v1Function return values

Fall 2008Compilers15 The MIPS Architecture Examples of Native Instruction Set Instruction Group InstructionFunction Arithmetic/ Logic add $s1,$s2,$s3$s1 = $s2 + $s3 addi $s1,$s2,K$s1 = $s2 + K Load/Storelw $s1,K($s2)$s1 = MEM[$s2+K] sw $s1,K($s2)MEM[$s2+K] = $s1 Jumps and Conditional Branches beq $s1,$s2,Kif ($s1=$s2) goto PC K slt $s1,$s2,$s3if ($s2<$s3) $s1=1 else $s1=0 j Kgoto K Proceduresjal K$ra = PC + 4; goto K jr $ragoto $ra

Spring 2006ICOM 4036 Programming Laguages Lecture 4 16 The SPIM Assembler Examples of Pseudo-Instruction Set Instruction Group SyntaxTranslates to: Arithmetic/ Logic neg $s1, $s2sub $s1, $r0, $s2 not $s1, $s2nor $17, $18, $0 Load/Storeli $s1, Kori $s1, $0, K la $s1, Klui $at, 152 ori $s1, $at, move $s1, $s2 Jumps and Conditional Branches bgt $s1, $s2, Kslt $at, $s1, $s2 bne $at, $0, K sge $s1, $s2, $s3 bne $s3, $s2, foo ori $s1, $0, 1 beq $0, $0, bar foo: slt $s1, $s3, $s2 bar: Pseudo Instructions: translated to native instructions by Assembler

Spring 2006ICOM 4036 Programming Laguages Lecture 4 17 The SPIM Assembler Examples of Assembler Directives GroupDirectiveFunction Memory Segmentation.data Data Segment starting at.text Text (program) Segment.stack Stack Segment.ktext Kernel Text Segment.kdata Kernel Data Segment Data Allocationx:.word Allocates 32-bit variable x:.byte Allocates 8-bit variable x:.ascii “hello”Allocates 8-bit cell array Other.globl xx is external symbol Assembler Directives: Provide assembler additional info to generate machine code

Spring 2006ICOM 4036 Programming Laguages Lecture 4 18 The MIPS Architecture Memory Model 32-bit byte addressable address space

Procedure Linkage Approach I Problem –procedure must determine where to return after servicing the call Solution: Architecture Support –Add a jump instruction that saves the return address in some place known to callee MIPS: jal instruction saves return address in register $ra –Add an instruction that can jump to return address MIPS: jr instruction jumps to the address contained in its argument register

int a = 0; int b = 0; int res = 0; main () { a = 12; b = 5; res = 0; div(); printf(“Res = %d”,res); } void div(void) { while (a >= b) { a = a - b; res ++; } Computing Integer Division (Procedure Version) Iterative C++ Version MIPS Assembly Language C++.data x:.word 0 y:.word 0 res:.word 0 pf1:.asciiz "Result = " pf2:.asciiz "Remainder = ".globlmain.text main:# int main() { # // main assumes registers sx unused la$s0, x# x = 12; li$s1, 12 sw$s1, 0($s0) la$s0, y# y = 5; li$s2, 5 sw$s2, 0($s0) la$s0, res# res = 0; li$s3, 0 sw$s3, 0($s0) jald# div(); lw$s3, 0($s0) la$a0, pf1# printf("Result = %d \n"); li$v0, 4# //system call to print_str syscall move$a0, $s3 li$v0, 1# //system call to print_int syscall la$a0, pf2# printf("Remainder = %d \n"); li$v0, 4# //system call to print_str syscall move$a0, $s1 li$v0, 1# //system call to print_int syscall jr$ra# return // TO Operating System Function Call

Computing Integer Division (Procedure Version) Iterative C++ Version int a = 0; int b = 0; int res = 0; main () { a = 12; b = 5; res = 0; div(); printf(“Res = %d”,res); } void div(void) { while (a >= b) { a = a - b; res ++; } MIPS Assembly Language C++ # div function # PROBLEM: Must save args and registers before using them d:# void d(void) { # // Allocate registers for globals la$s0, x# // x in $s1 lw$s1, 0($s0) la$s0, y# // y in $s2 lw$s2, 0($s0) la$s0, res# // res in $s3 lw$s3, 0($s0) while:bgt$s2, $s1, ewhile# while (x <= y) { sub$s1, $s1, $s2# x = x - y addi$s3, $s3, 1# res ++ jwhile# } ewhile:# // Update variables in memory la$s0, x sw$s1, 0($s0) la$s0, y sw$s2, 0($s0) la$s0, res sw$s3, 0($s0) enddiv:jr$ra# return; # }

Prof. Necula CS 164 Lecture 1422 Activation Trees Assumption (2) requires that when P calls Q, then Q returns before P does Lifetimes of procedure activations are properly nested Activation lifetimes can be depicted as a tree

Prof. Necula CS 164 Lecture 1423 Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main f g g

Prof. Necula CS 164 Lecture 1424 Example 2 Class Main { g() : Int { 1 }; f(x:Int): Int { if x = 0 then g() else f(x - 1) fi}; main(): Int {{f(3); }}; } What is the activation tree for this example?

Prof. Necula CS 164 Lecture 1425 Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } MainStack Main

Prof. Necula CS 164 Lecture 1426 Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g Stack Main g

Prof. Necula CS 164 Lecture 1427 Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g f Stack Main f

Prof. Necula CS 164 Lecture 1428 Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main f g g Stack Main f g

Recursion Basics int fact(int n) { if (n == 0) { return 1; else return (fact(n-1) * n); } n = 3 fact(2) fact(3) n = 2 n = 1 fact(1) n = 0 fact(0) 1 1 * 1 = 1 2 * 1 = 2 3 * 2 = 6 n = 3 n = 2 n = 1 n = 0

Pending Problems With Linkage Approach I Registers shared by all procedures –procedures must save/restore registers (use stack) Procedures should be able to call other procedures –save multiple return addresses (use stack) Lack of parameters forces access to globals –pass parameters in registers Recursion requires multiple copies of local data –store multiple procedure activation records (use stack) Need a convention for returning function values –return values in registers

Prof. Necula CS 164 Lecture 1431 Notes The activation tree depends on run-time behavior The activation tree may be different for every program input Since activations are properly nested, a stack can track currently active procedures

Prof. Necula CS 164 Lecture 1432 Revised Memory Layout Low Address High Address Memory Code Stack

Prof. Necula CS 164 Lecture 1433 Activation Records On many machines the stack starts at high- addresses and grows towards lower addresses The information needed to manage one procedure activation is called an activation record (AR) or frame If procedure F calls G, then G’s activation record contains a mix of info about F and G.

Prof. Necula CS 164 Lecture 1434 What is in G’s AR when F calls G? F is “suspended” until G completes, at which point F resumes. G’s AR contains information needed to resume execution of F. G’s AR may also contain: –Actual parameters to G (supplied by F) –G’s return value (needed by F) –Space for G’s local variables

Prof. Necula CS 164 Lecture 1435 The Contents of a Typical AR for G Space for G’s return value Actual parameters Pointer to the previous activation record –The control link points to AR of caller of G Machine status prior to calling G –Contents of registers & program counter –Local variables Other temporary values

Solution: Use Stacks of Procedure Frames Stack frame contains: –Saved arguments –Saved registers –Return address –Local variables main stack frame div stack frame OS stack growth

Prof. Necula CS 164 Lecture 1437 Example 2, Revisited Class Main { g() : Int { 1 }; f(x:Int):Int {if x=0 then g() else f(x - 1)(**)fi}; main(): Int {{f(3); (*) }};} AR for f: return address control link argument result

Prof. Necula CS 164 Lecture 1438 Stack After Two Calls to f main result 3 (*)(*) f 2 (**) f Stack

Prof. Necula CS 164 Lecture 1439 Notes main has no argument or local variables and its result is never used; its AR is uninteresting (*) and (**) are return addresses of the invocations of f –The return address is where execution resumes after a procedure call finishes This is only one of many possible AR designs –Would also work for C, Pascal, FORTRAN, etc.

Prof. Necula CS 164 Lecture 1440 The Main Point The compiler must determine, at compile-time, the layout of activation records and generate code that correctly accesses locations in the activation record Thus, the AR layout and the code generator must be designed together!

Prof. Necula CS 164 Lecture 1441 Discussion The advantage of placing the return value 1st in a frame is that the caller can find it at a fixed offset from its own frame There is nothing magic about this organization –Can rearrange order of frame elements –Can divide caller/callee responsibilities differently –An organization is better if it improves execution speed or simplifies code generation

Prof. Necula CS 164 Lecture 1442 Discussion (Cont.) Real compilers hold as much of the frame as possible in registers –Especially the method result and arguments

Prof. Necula CS 164 Lecture 1443 Globals All references to a global variable point to the same object –Can’t store a global in an activation record Globals are assigned a fixed address once –Variables with fixed address are “statically allocated” Depending on the language, there may be other statically allocated values

Prof. Necula CS 164 Lecture 1444 Memory Layout with Static Data Low Address High Address Memory Code Stack Static Data

Anatomy of a Stack Frame function arguments saved registers return address Contract: Every function must leave the stack the way it found it local variables of static size caller’s stack frame work area frame Pointer $fp in MIPS stack Pointer $sp in MIPS

Example: Function Linkage using Stack Frames int x = 0; int y = 0; int res = 0; main () { x = 12; y = 5; res = div(x,y); printf(“Res = %d”,res); } int div(int a,int b) { int res = 0; if (a >= b) { res = div(a-b,b) + 1; } else { res = 0; } return res; } Add return values Add parameters Add recursion Add local variables

Example: Function Linkage using Stack Frames div:sub$sp, $sp, 28# Alloc space for 28 byte stack frame sw$a0, 24($sp)# Save argument registers sw$a1, 20($sp)# a in $a0 sw$ra, 16($sp)# Save other registers as needed sw$s1, 12($sp)# Save callee saved registers ($sx) sw$s2, 8($sp) sw$s3, 4($sp)# No need to save $s4, since not used li $s3, 0 sw$s3, 0($sp)# int res = 0; # Allocate registers for locals lw$s1, 24($sp) # a in $s1 lw$s2, 20($sp)# b in $s2 lw$s3, 0($sp)# res in $s3 if:bgt$s2, $s1, else# if (a >= b) { sub$a0, $s1, $s2# move$a1, $s2 jaldiv# addi$s3, $v0, 1# res = div(a-b, b) + 1; jendif# } else:li$s3, 0# else { res = 0; } endif: sw$s1, 32($sp) # deallocate a from $s1 sw$s2, 28($sp)# deallocate b from $s2 sw$s3, 0($sp)# deallocate res from $s3 move$v0, $s3# return res lw$a0, 24($sp)# Restore saved registers lw$a1, 20($sp)# a in $a0 lw$ra, 16($sp)# Save other registers as needed lw$s1, 12($sp)# Save callee saved registers ($sx) lw$s2, 8($sp) lw$s3, 4($sp)# No need to save $s4, since not used addu$sp, $sp, 28# pop stack frame enddiv:jr$ra# return; #

MIPS: Procedure Linkage Summary First 4 arguments passed in $a0-$a3 Other arguments passed on the stack Return address passed in $ra Return value(s) returned in $v0-$v1 Sx registers saved by callee Tx registers saved by caller

Prof. Necula CS 164 Lecture 1449 Heap Storage A value that outlives the procedure that creates it cannot be kept in the AR method foo() { new Bar } The Bar value must survive deallocation of foo’s AR Languages with dynamically allocated data use a heap to store dynamic data

Prof. Necula CS 164 Lecture 1450 Notes The code area contains object code –For most languages, fixed size and read only The static area contains data (not code) with fixed addresses (e.g., global data) –Fixed size, may be readable or writable The stack contains an AR for each currently active procedure –Each AR usually fixed size, contains locals Heap contains all other data –In C, heap is managed by malloc and free

Prof. Necula CS 164 Lecture 1451 Notes (Cont.) Both the heap and the stack grow Must take care that they don’t grow into each other Solution: start heap and stack at opposite ends of memory and let the grow towards each other

Prof. Necula CS 164 Lecture 1452 Memory Layout with Heap Low Address High Address Memory Code Heap Static Data Stack

Prof. Necula CS 164 Lecture 1453 Data Layout Low-level details of machine architecture are important in laying out data for correct code and maximum performance Chief among these concerns is alignment

Prof. Necula CS 164 Lecture 1454 Alignment Most modern machines are (still) 32 bit –8 bits in a byte –4 bytes in a word –Machines are either byte or word addressable Data is word aligned if it begins at a word boundary Most machines have some alignment restrictions –Or performance penalties for poor alignment

Prof. Necula CS 164 Lecture 1455 Alignment (Cont.) Example: A string “Hello” Takes 5 characters (without a terminating \0) To word align next datum, add 3 “padding” characters to the string The padding is not part of the string, it’s just unused memory

END Prof. Necula CS 164 Lecture 1456

Spring 2006ICOM 4036 Programming Laguages Lecture 4 57 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } MIPS/SPIM Version MIPS Assembly Language C++.data# Use HLL program as a comment x:.word12# int x = 12; y:.word4# int y = 4; res:.word0# int res = 0;.globlmain.text main:la$s0, x# Allocate registers for globals lw$s1, 0($s0)# x in $s1 lw$s2, 4($s0)# y in $s2 lw$s3, 8($s0)# res in $s3 while:bgt$s2, $s1, endwhile# while (x >= y) { sub$s1, $s1, $s2# x = x - y; addi$s3, $s3, 1# res ++; jwhile# } endwhile: la$s0, x# Update variables in memory sw$s1, 0($s0) sw$s2, 4($s0) sw$s3, 8($s0)

Spring 2006ICOM 4036 Programming Laguages Lecture 4 58 Computing Integer Division Iterative C++ Version int a = 12; int b = 4; int result = 0; main () { while (a >= b) { a = a - b; result ++; } printf("Result = %d \n"); } MIPS/SPIM Version Input/Output in SPIM MIPS Assembly Language C++.data# Use HLL program as a comment x:.word12# int x = 12; y:.word4# int y = 4; res:.word0# int res = 0; pf1:.asciiz "Result = ".globlmain.text main:la$s0, x# Allocate registers for globals lw$s1, 0($s0)# x in $s1 lw$s2, 4($s0)# y in $s2 lw$s3, 8($s0)# res in $s3 while:bgt$s2, $s1, endwhile# while (x >= y) { sub$s1, $s1, $s2# x = x - y; addi$s3, $s3, 1# res ++; jwhile# } endwhile: la$a0, pf1# printf("Result = %d \n"); li$v0, 4# //system call to print_str syscall move$a0, $s3 li$v0, 1# //system call to print_int syscall la$s0, x# Update variables in memory sw$s1, 0($s0) sw$s2, 4($s0) sw$s3, 8($s0)

Spring 2006ICOM 4036 Programming Laguages Lecture 4 59 SPIM Assembler Abstractions Symbolic Labels –Instruction addresses and memory locations Assembler Directives –Memory allocation –Memory segments Pseudo-Instructions –Extend native instruction set without complicating arquitecture Macros

Spring 2006ICOM 4036 Programming Laguages Lecture 4 60 Simple and uniform 32-bit 3-operand instruction formats –R Format: Arithmetic/Logic operations on registers –I Format: Branches, loads and stores –J Format: Jump Instruction The MIPS Architecture Main Instruction Formats opcode 6 bits rs 5 bits rt 5 bits rd 5 bits shamt 5 bits funct 6 bits opcode 6 bits rs 5 bits rt 5 bits Address/Immediate 16 bits opcode 6 bits rs 5 bits rt 5 bits Address/Immediate 16 bits

Spring 2006ICOM 4036 Programming Laguages Lecture 4 61 MIPS Data Paths

Fall 2008Compilers62 Mips Packaging

Spring 2006ICOM 4036 Programming Laguages Lecture 4 63 Handy MIPS ISA References Appendix A: Patterson & Hennessy SPIM ISA Summary on class website Patterson & Hennessy Back Cover