Where is all the knowledge we lost with information? T. S. Eliot

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
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.
10/6: Lecture Topics Procedure call Calling conventions The stack
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.
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)
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
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)
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
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.
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.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
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.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
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.
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
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Instructions - Type and Format
MIPS Procedures.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Program and memory layout
Computer Architecture
Program and memory layout
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Where is all the knowledge we lost with information? T. S. Eliot Week 7 Where is all the knowledge we lost with information? T. S. Eliot

Procedures 1 - MAL Procedure Call & Return Mechanisms 2 - Dynamic Storage Allocation 3 - Activation Records 4 - Parameter Passing 5 - Saving Registers 6 - MIPS Register Usage 7 - Sample MAL Program that uses Procedures

Why Use Procedures? Code is easier to understand. Easier to develop and maintain. Different programmers can write different parts of the program Code becomes modular

Why Procedures Cont. Allows reduction in size of program in exchange for somewhat reduced performance (overhead of Call, Return, and certain associated overhead)

generate assembly language instructions to implement HLL Compilers .. generate assembly language instructions to implement calling the procedure returning from the procedure passing parameters to the procedure returning parameters from the procedure

MAL Procedure Call & Return Mechanism 1 - Save the return address 2 - Call the procedure 3 - Execute the procedure 4 - Return to calling program

Reality -Procedure Execution Steps 1. Save parameters. Where? In registers (fast) or in memory (slow) 2. Save return address. Where? 3. Call procedure. How? new jump instruction

Procedure Execution Steps Cont. 4. Allocate space to store procedure’s variables (local variables). Why? Variables are used temporarily, space can be reclaimed upon return. For recursive procedure call, must have multiple copies of local variables, one for each recursion. How? using a special stack, the “system stack”

Procedure Execution Steps Cont. 5. Execute procedure 6. Deallocate local variables. How? Pop them off the system stack 7. Save return values, if any. Where? In registers (fast) or in memory (slow) 8. Get return address 9. Return. How? new jump instruction

Procedure Call MAL has a special jump instruction for procedure calls, Jump and Link ( jal ) Instruction’s assembly format: jal proc_label Instruction’s action: program jumps to the address represented by proc_label return address (address of instruction after jal ) is saved in $31, called the “link register” because it links the procedure back to the return location.

Procedure Return A new instruction Jump Register ( jr ) is used for returning from a procedure Instruction’s assembly format: jr $x Instruction’s action: program jumps to the address contained in register x. Together jal and jr allow for simple and fast subroutine calls/returns:

MAL Procedure Call Summary jal proc1 next_instruction: ... ... done proc1: ... jr $31 next_instruction

MIPS Jump Instructions j jump to a label (similar to branch) Absolute address jal jump and link jumps to a procedure saves the return address in $31 jr R jump register jumps to the location pointed to by register R

Procedure Call and Return jal label1 # Jump and Link jumps to the instruction at label1 saves the return address in $31 jr $31 # Jump Register copies value in register $31 to the PC

Nested Procedure Calls We have a problem: when a procedure A calls another procedure B (a “nested procedure call”) the contents of $31 are overwritten and the return address for A is lost:

Nested Procedure Calls Cont. Solution: transfer the return address to another register In A, before call to B: move $16,$31 # save return address in reg 16 To return from A: jr $16 # return address is in reg 16 This has limitations: Limited by number of nested procedures Programmer must keep return addresses straight

Dynamic Storage Allocation return address in register $ra ($31) must save this return address for nested procedure calls recursive procedure calls

Recursive Procedure Calls It is possible for a procedure to conditionally call itself, a “recursive procedure call”. The number or recursive calls can be indefinite Cannot save return address in registers Solution: save return addresses on the “system stack” Want a stack, because addresses are needed in the reverse order they are produced. A register ($29) is designated as the system stack pointer

MIPS System Stack stack pointer $sp ($29) base of stack at high memory stack grows down (toward smaller addresses) $sp points to first empty location at the top of the stack Reserved Test Segment Data Segment Stack Segment 0x400000 0x7fffffff

Push sw $8, 0($sp) addi $sp, $sp, -4 or sw $8, 4($sp)

Stacking the Return Address The system stack grows from high-numbered memory toward low-numbered memory Proc1: addi $sp, $sp, -4 # make room on stack for return addr. sw $ra, 4($sp) # save return address to the stack ... beq $8, $9, exit # test some condition jal Proc1 # recursive call exit: lw $10, 4($sp) # restore return address from the stack addi $sp, $sp, 4 # deallocate the stack space jr $10 # return to the calling routine

Pop addi $sp, $sp, 4 lw $8, 0($sp) or lw $8, 4($sp)

Activation Records - Concept When a procedure is invoked old environment must be saved. new environment is created When the procedure terminates this new environment disappears and the old environment must be restored. An activation record consists of all the information that corresponds to the state of a procedure.

Saving and Restoring Activation Record (or Stack Frame) is pushed onto the stack just like a word. Can consist of multiple words Varies in size. Stack pointer ($29) must be adjusted by this size After pushing After popping.

Pushing one at a time sw $8, 0($sp) add $sp, $sp, -4 sw $12, 0($sp) jal proc ... proc: sw $31, 0($sp)

Push Activation Record then Increment sp sw $8, 0($sp) sw $12, -4($sp) sw $6, -8($sp) jal proc ... proc: sw $31, -12($sp) add $sp, $sp, -16 add $sp, $sp, 16

Increment sp then Push Activation Record Most Common Method add $sp, $sp, -16 sw $8, 16($sp) sw $12, 12($sp) sw $6, 8($sp) jal proc add $sp, $sp, 16 ... proc: sw $31, 4($sp)

Accessing Parameters in the Procedure lw $4, 4($fp) # parameter 1 lw $5, 8($fp) # parameter 2 lw $6, 12($fp) # parameter 3 This assumes that the parameters are not passed via register.

Caller-Saved/Callee-Saved Registers To avoid unnecessary register saves/restores, software convention divides the registers into two sets: “callee saved registers” (also called “saved registers”): preserved across a call caller does not have to worry about saving them. The callee must save them if it wants to use them. callee saved registers are $16-$23, $30 ($fp)

Caller-Saved/Callee-Saved Registers Cont. “caller saved registers” (also called “temporary registers”): may be destroyed by the callee without saving them. The caller must save these registers if it wants to preserve the values caller saved registers (except those reserved for system use): $2-$15, $24-25, $31 ($ra)

Passing Parameters A fast method of passing parameters to a procedure is using registers (they are fast) MIPS uses the software convention that the first 4 parameters are passed in registers $4-$7 Handles most procedure calls, because usually few parameters are passed. Additional parameters are passed on the system stack.

Passing Parameters Cont. move $4, param1 move $5, param2 move $6, param3 move $7, param4 addi $29, $29, -12 # make room on stack for two params # and return address sw $31, 4($29) # save return address in top stack # location sw $14, 8($29) # store 6th param in next stack location sw $15, 12($29) # store 5th param in next stack location jal Proc1 # call procedure ...

Passing Parameters Cont. Proc1: # params 1-4 are in registers $4-$7, # can use them directly from those regs. lw $8, 12($29) # put 5th parameter in a register lw $9, 8($29) # put 6th parameter in a register ... lw $10, 4($29) # put return address in a register addi $29, $29, 12 # deallocate stack space jr $10 # return to caller

Return Values Procedure return values are handled similarly: First 2 return values are returned in registers, $2 and $3. Additional return values are passed back on the stack.

Dynamic Storage Allocation A procedure’s local variables can be allocated on the system stack, on procedure entry Allows instances of variable for recursive calls Provides proper variable scope Allows easy deallocation E.g., allocating 2 local index variables. Rather than write: .data i: .word j: .word You write: .text myproc: addi $29, $29, -8 #allocate space for 2 local variables

Dynamic Storage Allocation (cont.) The first index variable is access as: lw/sw $x, 4($29) # its location is on the top of the stack The second index variable is access as: lw/sw $x, 8($29) # its location is second from top of the stack Local variables are pushed on top of return address and any parameters passed on stack: Top Of Stack The only local variables allocated space on the stack are those that cannot stay in a register their entire life.

SumN Example

SumN Example.

SumN, Full AR creation

SumN, Full AR creation. Same code as before

Fibonacci Sequence Defined as a recursive function F(n) = 0 ; n=0 1 ; n=1 F(n-1) + F(n-2) ; otherwise Generates the numbers

Fibonacci Sequence Java code public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); }

Fibonacci Sequence Must create a dummy AR for initial procedure call Don’t have to save old a0 first time in N is loaded into a0 the parameter Save return value somewhere safe

Fibonacci Sequence ra is stored in AR, we decide to use s1 and s2, so they must be saved onto the AR There are 2 base cases, each must be identified Create a new AR for Fib(n-1), saving a0 onto stack. After Fib(n-1) return we must undo the AR created above

Fibonacci Sequence Same as previous call, except on return we save the result in a different register. Branch to code to add S1 and S2, the results of the 2 Fib calls

Fibonacci Sequence Load return value of each base case as appropriate into return register Branch to code to add S1 and S2, the results of the 2 Fib calls Restore the saved S1 and S2 from AR, load the stored ra from the AR, then return from the procedure

Main () as a Procedure Main () is called by the operating system, somewhat like a procedure, except: Return address is not saved, so called using jump rather than JAL OS does not necessarily want to execute the next instruction after jump to user code Return is via a special instruction SYSCALL, which jumps to a special return location in the OS. User is blocked from jumping to other locations in the OS, for security and reliability SYSCALL exit.

Global Data Non-local data that can be accessed in all procedures (called Global Data) is kept in a special region of memory Global data space is accessed via global pointer ($gp) Created by usual .word, .float, .byte directives This is the .data region of memory Normally used when multiple programs wish to access common resources.

End