 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.

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

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
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.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
10/6: Lecture Topics Procedure call Calling conventions The stack
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)
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
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.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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
Procedure call frame: Hold values passed to a procedure as arguments
CS61C L11 Linker © UC Regents 1 CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CENG 311 Starting a Program. Review (1/2) °IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with +/- infinity, Not-a-Number (Nan),
CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) 1 Instructors: Nicholas Weaver & Vladimir Stojanovic
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
MIPS Functions and the Runtime Stack
Lecture 3 Translation.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Lecture 5: Procedure Calls
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Lecture 6: Assembly Programs
Program and memory layout
Computer Architecture
Program and memory layout
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused  Procedures allow the programmer to concentrate on one portion of the code at a time › parameters act as barriers between the procedure and the rest of the program and data, allowing the procedure to be passed values (arguments) and to return values (results)

 Main routine (caller) places parameters in a place where the procedure (callee) can access them › $a0 - $a3: four argument registers  Caller transfers control to the callee  Callee acquires the storage resources needed  Callee performs the desired task  Callee places the result value in a place where the caller can access it › $v0 - $v1: two value registers for result values  Callee returns control to the caller › $ra: one return address register to return to the point of origin

 $0: No Change. Always 0.  $s0-$s7: Restore if you change. Very important, that’s why they’re called saved registers. If the callee changes these in any way, it must restore the original values before returning.  $sp: Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller won’t be able to restore values from the stack.  HINT -- All saved registers start with S!

 $ra: Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call.  $v0-$v1: Can Change. These will contain the new returned values.  $a0-$a3: Can change. These are volatile argument registers. Caller needs to save if they’ll need them after the call.  $t0-$t9: Can change. That’s why they’re called temporary: any procedure may change them at any time. Caller needs to save if they’ll need them afterwards.

 Suppose we want a procedure ‘sprod’ that returns x1*y1 + x2*y2.  Abstractly we can think of this as: sprod(x1,y1,x2,y2){ a = prod(x1,y1);#x1*y1 b = prod(x2,y2);#x2*y2 return add2(a,b);#a + b }  Procedure ‘sprod’ calls ‘prod’ and ‘add2’.

 jal procAddress › causes $31 to contain the next address  So each time that a procedure is called (eg, when ‘prod’ is called inside ‘sprod’) › $31 gets overwritten (‘clobbered’)  so that the return address for the outer procedure is no longer available!  Stacks can get us out of this problem.

 When a C program is run, there are 3 important memory areas allocated: › Static: Variables declared once per program, cease to exist only after execution completes. E.g., C globals › Heap: Variables declared dynamically › Stack: Space to be used by procedure during execution; this is where we can save register values

 A stack is used to store the sequence of return addresses when there are nested calls  The calling (outer) procedure › pushes address in $31 onto stack › calls the procedure ($31 contains return address) › pops stack to restore $31 (after procedure returns)  The called procedure... before returning › if it has called another procedure, it overwrites $31 with a pop from the stack › jr $31

 Step-1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack (in reverse order arg5 is at the top of the stack).  Step-2: Save caller-saved registers Save registers $t0-$t9 if they contain live values at the call site.  Step-3: Execute a jal instruction.

 Step-1: Establish stack frame. Subtract the frame size from the stack pointer. subi $sp, $sp, Typically, minimum frame size is 32 bytes (8 words).  Step-2: Save callee saved registers in the frame. Register $fp is always saved. Register $ra is saved if routine makes a call. Registers $s0-$s7 are saved if they are used.  Step-3: Establish Frame pointer Add the stack - 4 to the address in $sp  addi $fp, $sp, - 4

 Step-1: Put returned values in registers $v0, [$v1]. (if values are returned)  Step-2: Restore callee-saved registers. Restore $fp and other saved registers. [$ra, $s0 - $s7]  Step-3: Pop the stack Add the frame size to $sp. addi $sp, $sp,  Step-4: Return Jump to the address in $ra. jr $ra

f 0 = 1; f -1 = -1 f n = f n-1 + f n-2 f = 1, 1, 2, 3, 5, 8, 13, …

#include int main (int argc, char *argv[]) { int i; int sum = 0; for (i = 0; i <= 100; i = i + 1) sum = sum + i * i; printf ("The sum from is %d\n", sum); }

 The steps › Software design is translated into source code › Source code is entered using an editor, making source files › Source files are assembled or compiled producing object files › Object files are linked into an executable file › Executable files are loaded into memory and then run Executable Object files Assembler source Program Loaded Into memory Result Of Careful Software design

Types of assembly lines:  Machine instruction  sw $ra,20($sp)  Pseudoinstruction › Data-definition .asciiz “The sum from” › Assembler directive .text .globl main .align 0  Label main: loop:.text.align2.globlmain main: subu$sp, $sp, 32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6, $t6 lw$t8, 24($sp) addu$t9, $t8, $t7 sw$t9, 24($sp) addu$t0, $t6, 1 sw$t0, 28($sp) ble$t0, 100,loop la$a0, str lw$a1, 24($sp) jalprintf move$v0, $0 lw$ra, 20($sp) addu$sp, $sp, 32 jr$ra.data.align0 str:.asciiz"The sum from is %d\n" This Is what You write

addiu$29, $29, -32 sw$31, 20($29) sw$4, 32($29) sw$5, 36($29) sw$0, 24($29) sw$0, 28($29) lw$14, 28($29) lw$24, 24($29) multu$14, $14 addiu$8, $14, 1 slti$1, $8, 101 sw$8, 28($29) mflo$15 addu$25, $24, $15 bne$1, $0, -9 sw$25, 24($29) lui$4, 4096 lw$5, 24($29) jal addiu$4, $4, 1072 lw$31, 20($29) addiu$29, $29, 32 jr$31 move$2, $0 You sometimes read This in the debugger, Especially when you Are learning

If you can read this You are a computer Not for human consumption

 The object file header describes the size and position of the other pieces of the file.  The text segment › contains the machine language code for routines in the source file. These routines may be unexecutable because of unresolved references.  The data segment › contains a binary representation of the data in the source file. The data also may be incomplete because of unresolved references to labels in other files.  The relocation information › identifies instructions and data words that depend on absolute addresses. These references must change if portions of the program are moved in memory  The symbol table › associates addresses with external labels in the source file and lists unresolved references.  The debugging information › contains a concise description of the way in which the program was compiled, so a debugger can find which instruction addresses correspond to lines in a source file and print the data structures in readable form.