CSE 410, Spring 2008 Computer Systems

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

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.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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
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)
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
Procedure call frame: Hold values passed to a procedure as arguments
 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)
Intro to Computer Architecture
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
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.
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:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
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.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
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.
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.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Instructions - Type and Format
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Prof. Hakim Weatherspoon
MIPS Instructions.
ECE232: Hardware Organization and Design
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
Computer Instructions
Program and memory layout
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CSE 410, Spring 2008 Computer Systems Procedures CSE 410, Spring 2008 Computer Systems http://www.cs.washington.edu/410 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Readings and References Section 2.7, Supporting Procedures in Computer Hardware Section A.5, Memory Usage Section A.6, Procedure Call Convention (Monday) Section 3.2, Signed and Unsigned Numbers, P&H another presentation of binary, hex, and decimal ignore signed numbers for now, we will cover them next week Other References MIPSpro Assembly Language Programmer’s Guide, document number 007-2418-006, Silicon Graphics, 2003 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Instructions and Data flow main memory instructions and data program counter increments by 4 instructions and data registers 32 bits wide 32 in number functional units implement instructions 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Layout of program memory 7FFF FFFF reserved (4KB) 7FFF EFFF stack (grows down) ~1792 MB < ~1.5GB Not to Scale! heap (grows up) 1001 0000 1000 FFFF global data (64 KB) 1000 0000 0FFF FFFF program (text) (252 MB) 0040 0000 003F FFFF reserved (4 MB) 0000 0000 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Why use procedures? So far, our program is just one long run of instructions Procedures are a tool and an abstraction We can do a lot this way, but the program rapidly gets too large to handle easily Procedures allow the programmer to organize the code into logical units 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

What does a procedure do for us? A procedure provides a well defined and reusable interface to a particular capability entry, exit, parameters clearly identified Reduces the level of detail the programmer needs to know to accomplish a task The internals of a function can be ignored messy details can be hidden from innocent eyes internals can change without affecting caller All the usual good stuff – not specific to asm, but maybe even more crucial here 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

How do you make a procedure call? Setup – (caller steps) Make input parameters available Jump and link to target function Execution & Teardown– (callee steps) Accquire needed resources on the stack Run algorithm Make output results available Release resources Restore saved registers teardown stack Jump back to point caller + 4 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

How do you use a procedure? 1. set up parameters 2. transfer to procedure 3. acquire storage resources 4. do the desired function 5. make result available to caller 6. return storage resources 7. return to point of call 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Calling conventions The details of how you implement the steps for using a procedure are governed by the calling conventions being used There is much variation in conventions which causes much programmer pain A convention is just that! Not enforced… Understand the calling conventions of the system you are writing for Moto88000, MIPS-1, 8086, , P&H, cse410, ... 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

1. Set up parameters The registers are one obvious place to put parameters for a procedure to read very fast, easily referenced, useful direct ops Many procedures have 4 or less arguments $a0, $a1, $a2, $a3 are used for arguments … but some procedures have more we don’t want to use up all the registers so we use memory to store the rest (where?) 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

The Stack Stack pointer ($sp) points to the “top” value on the stack (ie, the lowest address in use) There are no “push” or “pop” instructions we adjust the stack pointer directly stack grows downward towards zero subu $sp, $sp, xx : make room for more data addu $sp, $sp, xx : release space on the stack note that both subu and addu become addiu Think about the xx value – what might be common? 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Dynamic storage on the stack ... jal main main: subu $sp,$sp,8 ... 12($sp) 0x7fffee04 20($sp) 8($sp) 0x7fffee00 16($sp) 4($sp) 0x7fffedfc 12($sp) 0($sp) 0x7fffedf8 8($sp) 0x7fffedf4 4($sp) 0x7fffedf0 0($sp) 0x7fffedec 0x7fffede8 0x7fffede4 towards 0 $sp 0x7fffedf8 $sp 0x7fffedf0 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Layout of stack frame >4 argument area (if needed) $sp (on entry) saved registers (if needed) procA: subu $sp,$sp,xx ... local variables (if needed) stack frame >4 argument area (if needed) $sp (after subu) towards 0 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Argument Build Area (“>4 Argument Area”) Some calling conventions require that caller reserve stack space for all arguments 16 bytes (4 words) left empty to mirror $a0-$a3 Other calling conventions require that caller reserve stack space only for arguments that do not fit in $a0 - $a3 so “>4 argument area” is only present if your input arguments didn’t fit in 4 registers for MIPS-32 i.e., Passing 6 items We will assume this convention of allocating space for $a0-$a3 for a few of following examples 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Agreement A procedure and all of the programs that call it must agree on the calling convention This is one reason why changing the calling convention for system libraries is a big deal A common architectural agreement is… caller reserves stack space for all arguments 16 bytes (4 words) left empty to mirror $a0-$a3 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

2. Transfer to procedure ... jal main main: subu $sp,$sp,8 ... 12($sp) 0x7fffee04 20($sp) 8($sp) 0x7fffee00 16($sp) 4($sp) 0x7fffedfc 12($sp) 0($sp) 0x7fffedf8 8($sp) 0x7fffedf4 4($sp) 0x7fffedf0 0($sp) 0x7fffedec 0x7fffede8 0x7fffede4 towards 0 $sp 0x7fffedf8 $sp 0x7fffedf0 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Jump and link Jump can take you anywhere within the currently active 256 MB segment ~ 67 million words (a lot or a little?) Link store return address in $ra note: this overwrites current value of $ra Really! Note it! This will haunt you otherwise. 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

3. Acquire storage resources argument build area (if needed) $sp (on entry) saved registers (if needed) procA: subu $sp,$sp,40 sw $ra,32($sp) sw $s0,28($sp) local variables (if needed) stack frame argument build area (if needed) $sp (after subu) towards 0 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

3a. Saved registers There is only one set of registers If called procedure unexpectedly overwrites them, caller will be surprised and distressed Another agreement called procedure can change $a0-$a3, $v0-$v1, $t0-$t9 without restoring original values called procedure must save and restore value of any other register it wants to use Saved registers $ra – what if the procedure invokes another procedure? 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Register numbers and names usage zero always returns 0 1 at reserved for use as assembler temporary 2-3 v0, v1 values returned by procedures 4-7 a0-a3 first few procedure arguments 8-15, 24, 25 t0-t9 temps - can use without saving 16-23 s0-s7 temps - must save before using 26,27 k0, k1 reserved for kernel use - may change at any time 28 gp global pointer 29 sp stack pointer 30 fp or s8 frame pointer 31 ra return address from procedure 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

3b. Local variables If the called procedure needs to store values in memory while it is working, space must be reserved on the stack for them Debugging note compiler can often optimize so that all variables fit in registers and are never stored in memory so a memory dump may not contain all values See x86 example later in class use switches to turn off optimization (but …) 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

3c. Argument area Main in LeafFunction.s uses this convention: caller reserves stack space for all arguments 16 bytes (4 words) left empty to mirror $a0-$a3 So, we’ll allocate but leave empty words on our stack Contrast to cubeNonLeaf function. If your procedure does more than one call to other procedures, then ... the argument area must be large enough for the largest set of arguments So, for the arg area contribution to your subu amount Finitely bounded by the procedure with the most args 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Using the stack pointer Adjust it once on entry, once on exit Initial adjustment should include all the space you will need in this procedure Remember that a word is 4 bytes so expect to see references like 8($sp), 20($sp) Keep stack pointer double word aligned adjust by multiples of 8 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

4. Do the desired function You have saved the values of the registers that must be preserved across the call (prologue) The arguments are in $a0 - $a3 or on the stack The stack pointer points to the end of your stack frame Go, function, go! signal processing (FTs in asm?), image filter (see coords example), encryption, … 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

5. Make result available to caller Registers $v0 and $v1 are available for this Most procedures put a 32-bit value in $v0 Returning the address of a variable? be very careful! your portion of the stack is invalid as soon as you return Which does *not* preclude its access or use; misuse of the stack here is the source of problems such as dangling pointers. the object must be allocated in caller’s part of stack (or somewhere further back) or globally allocated 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

6. Return storage resources argument build area (if needed) $sp (after addu) saved registers (if needed) lw $ra,32($sp) lw $s0,28($sp) addu $sp,$sp,40 jr $ra local variables (if needed) stack frame argument build area (if needed) $sp (while executing) towards 0 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

7. Return to point of call Jump through register The address of the instruction following the jump and link was put in $ra when we were called (the “link” in jump and link) We have carefully preserved $ra while the procedure was executing So, “jr $ra” takes us right back to caller If we did things right, return to PC + 4 the word following the jal or jalr 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

CSE 410 Calling Conventions Called procedure adjusts stack pointer once on entry, once on exit, in units of 8 bytes ($sp is DW aligned) Register usage in functions not required to save and restore $t0-$t9, $a0-$a3 must save and restore $s0-$s8, $ra if changed function results returned in $v0, $v1 Argument build area caller reserves stack space for all arguments 16 bytes (4 words) left empty to mirror $a0-$a3 For turnin, standard gcc/MIPS convention is fine Try this at home, though. 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington

Other Calling Conventions For a lib written in C# to be called from VB In .Net, extra layer of compilation from: The HLL to the IL to the (JITed) native instructions IL: stack-based V.S. our load-store architecture in MIPS Calling conventions must be in place here, or code compiled for one style of architecture will be unavailable to code built with other conventions in mind (non-reusable) CIL/Framework compiler handles this for you This is a different layer of abstraction than the underlying native instruction set For Itanium 128b instructions : 3 ops 2/22/2019 cse410-06-procedures-a © 2006-07 Perkins, DW Johnson and University of Washington