Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !

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
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
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.
Intro to Computer Architecture
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
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.
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
MicroComputer Engineering ActivationConcept page 1 Problem: !bell!help! !1! 2! !Bal help! !!! Bal bell! 3Jr $31!!! !Jr $31 ! What happend here? What will.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
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
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
Program and memory layout
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Lecture 6: Assembly Programs
Systems Architecture I
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
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
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! ! ! 1 ! 2 ! ! Bal help ! ! ! ! Bal bell ! 3 Jr $31 ! ! ! Jr $31 ! What happend here? What will happen here?

The activation concept Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep

The activation concept Son 1. Wakes up, starts executing “bell”, wants “help” Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep

The activation concept Grandson Son 1. Wakes up, starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep

The activation concept Grandson 1. Wakes up. starts executing “help” Son 1. Wakes up, starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep 3. Asleep Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep

The activation concept Grandson 1. Wakes up. starts executing “help” 2. Finished. Vanishes, Wakes up his parent Son 1. Wakes up, starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep 3. Asleep Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep

The activation concept Son 1. Wakes up, starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep 3. Asleep 4. Finished, Vanishes, wakes up his parent Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep

The activation concept Main 1. Executing program, wants “bell” 2. Creates son, Tells him “bell”, Falls asleep 3. Asleep 4. Executing

The main points: We talk about activations, not the code being executed. Last in - first out implies a stack OK for different activations to share the same instructions

A parents responsibilities to his unborn son: Be prepared for the son to trash $tx-registers. Put input arguments into $a0 - $a3. Give him a return address (by Bal- instr.). Tell him which code to execute (also by Bal).

The son’s responsibility to his sleeping parent: Leave the stack like he found it. Return results (if any) into registers $v0 - $v1. Leave the $ax-registers like he found them. OK to change $tx-registers and not restore them.

The activations responsibility to himself Need space for local variables? Create space on stack. Use $tx registers as scratchpads. Want to change $ax-register? Save the incoming values into local variables. Want to create a son? Protect the $tx-registers. Protect the return address ($31). by saving them on the stack

What does one activation own? Return address to his parent. His incoming parameters. A place to put his results. Some local variables. Which code to execute (his PC). Called his “activation record”

Stack the activation records: Grandson used activation record “grandson” Son (asleep) The concept gives unique context. saved activation record “son” Main (asleep) saved activation record “main”

The user stack $sp points to top- of- stack User stack (part of the data memory) $sp

The user stack Stack grows toward lower addresses. Basic stack operations: Push $t5 onto stack Pop from stack into $a2 Addi $sp $sp -4 Lw $a2 0($sp) Sw $t5 0($sp) Addi $sp $sp 4

Wrong!!!!! reason: interrupts. Push $t5 onto stack Pop from stack into $a2 Sw $t5 -4($sp) Addi $sp $sp 4 Addi $sp $sp -4 Lw $a2 -4($sp) This method writes Never use a negative beyond T.O.S first, displacement with then fixes $sp respect to T.O.S. reason: interrupts.

Framepointer <= $sp points here -8($fp) -4($fp) Low address -8($fp) <= $sp points here Var2 -4($fp) Var1 <= $fp points here 0($fp) Old fp 4($fp) Return addr Caller’s stack High address

Example Compute  i Define as recursive definition: 1 if N = 1 sum(N) = N + sum(N-1) otherwise

Pseudocode Procedure sum(N:integer):integer; if (N=1) then return (1) else T := N + sum(N-1); return (T) end;

Where do we start? Parameter comes in in $a0. Return address in $31. This activation will probably need a son (because of the recursion) so: Local variables. save $31. save $a0. the variable T?

The Code sum: addi $sp $sp -4 #store return address on stack sw $31 0($sp) addi $sp $sp -4 #store incoming parameter on stack sw $a0 0($sp) if: ori $t0 $r0 1 #test: fixed point? bne $a0 $t0 else then: ori $v0 $r0 1 b exit else: addi $a0 $a0 -1 #sum(N-1) in $v0, original $a0 lost bal sum #recursive call to sum lw $t0 0($sp) #retrive the incoming parameter value add $v0 $v0 $t0 #$v0 has N + T exit: lw $31 4($sp) #restore return address addi $sp $sp 8 #Stack cleaned jr $31 #end activation

Procedure Call Place parameters in a place where the procedure can access them ($a0 - $a3) Transfer control to the procedure (bal bell) Acquire the storage resources needed for the procedure. Perform the desired task. Place the result value in a place where the calling program can access it. ($v0 - $v1) Return control to the point of origin.(jr $31)