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

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
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
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.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
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.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
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.
Procedures 2a MIPS code examples Making Use of a 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
The Stack This is a special data structure: –The first item to be placed into the stack will be the last item taken out. Two basic operations: –Push: Places.
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:
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.
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
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
MIPS function continued
Program and memory layout
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Systems Architecture I
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
MIPS Functions.
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.

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 icoming 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