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

Similar documents
MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CSE Lecture In Class Example Handout

Course Administration

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Lecture 5: Procedure Calls

ECE232: Hardware Organization and Design

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Lecture 5: Procedure Calls

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Instruction Set Architectures (4)

ECE331: Hardware Organization and Design

ECE260: Fundamentals of Computer Engineering

MIPS function continued

MIPS Functions and the Runtime Stack

COMP2611: Computer Organization MIPS function and recursion

CSE Lecture In Class Example Handout

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

ECE 30 Introduction to Computer Engineering

MIPS Functions and Instruction Formats

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Computer Architecture

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Functions in MIPS. Functions in MIPS 1

EE 361 University of Hawaii Fall

Chapter 2: Instructions:

MIPS%Assembly% E155%

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Review of Activation Frames. FP of caller Y X Return value A B C

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Lecture 6: Assembly Programs

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Lecture 4: MIPS Instruction Set

Lecture 5. Announcements: Today: Finish up functions in MIPS

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Chapter 2A Instructions: Language of the Computer

CENG3420 Lecture 03 Review

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

CA Compiler Construction

CS 316: Procedure Calls/Pipelining

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Homework 4 - Solutions (Floating point representation, Performance, Recursion and Stacks) Maximum points: 80 points

MIPS Procedure Calls. Lecture 6 CS301

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Computer Organization MIPS ISA

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Common Problems on Homework

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CS 61c: Great Ideas in Computer Architecture

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

6.004 Tutorial Problems L3 Procedures and Stacks

Lab 4 : MIPS Function Calls

MIPS Assembly (Functions)

Procedure Call and Return Procedure call

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

Compiling Code, Procedures and Stacks

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

CDA3100 Midterm Exam, Summer 2013

SPIM Procedure Calls

Instructions: Assembly Language

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Today. Putting it all together

Patterson PII. Solutions

We will study the MIPS assembly language as an exemplar of the concept.

Course Administration

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

The Operating System (OS) MicroComputer Engineering OperatingSystem slide 1!

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CSC 2400: Computer Systems. Using the Stack for Function Calls

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

ECE260: Fundamentals of Computer Engineering

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

MIPS Assembly Language Guide

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

COE608: Computer Organization and Architecture

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

Transcription:

Problem: bell help 1 2 Bal help Bal bell 3 Jr $31 here? Jr $31 What happend What will happen here? MicroComputer Engineering ActivationConcept page 1

The activation concept Main 1. Executing program, wants bell 2. Creates son, Tells him bell, Falls asleep MicroComputer Engineering ActivationConcept page 2

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 MicroComputer Engineering ActivationConcept page 3

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 MicroComputer Engineering ActivationConcept page 4

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

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

The activation concept Son Main 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 1. Executing program, wants bell 2. Creates son, Tells him bell, Falls asleep 3. Asleep MicroComputer Engineering ActivationConcept page 7

The activation concept Main 1. Executing program, wants bell 2. Creates son, Tells him bell, Falls asleep 3. Asleep 4. Executing MicroComputer Engineering ActivationConcept page 8

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 MicroComputer Engineering ActivationConcept page 9

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). MicroComputer Engineering ActivationConcept page 10

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

The activations responsibility to himself Need space for local variables? Create space on stack. Use $tx registers as scratchpads. Want to create a son? Protect the $tx and $ax-registers if needed. Protect the return address ($31). by saving them on the stack MicroComputer Engineering ActivationConcept page 12

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 MicroComputer Engineering ActivationConcept page 13

Stack the activation records: Grandson used activation record grandson Son (asleep) saved activation record son The concept gives unique context. Main (asleep) saved activation record main MicroComputer Engineering ActivationConcept page 14

The user stack $sp points to top- of- stack $sp User stack (part of the data memory) MicroComputer Engineering ActivationConcept page 15

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 MicroComputer Engineering ActivationConcept page 16

Wrong 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. MicroComputer Engineering ActivationConcept page 17

Framepointer Low address" -8($fp) -4($fp) 0($fp) 4($fp) Var2 Var1 Old fp Return addr <= $sp points here <= $fp points here Caller s stack High address" MicroComputer Engineering ActivationConcept page 18

Example N Compute Σ i i=1 Define as recursive definition: sum(n) = 1 if N = 1 N + sum(n-1) otherwise MicroComputer Engineering ActivationConcept page 19

Pseudocode Procedure sum(n:integer):integer; if (N=1) then end; return (1) else T := N + sum(n-1); return (T) MicroComputer Engineering ActivationConcept page 20

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? MicroComputer Engineering ActivationConcept page 21

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 MicroComputer Engineering ActivationConcept page 22

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