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

Similar documents
COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Storage in Programs. largest. address. address

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

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

SPIM Procedure Calls

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

MIPS Procedure Calls. Lecture 6 CS301

CSE Lecture In Class Example Handout

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

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

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

Instruction Set Architectures (4)

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

COMP2611: Computer Organization MIPS function and recursion

ECE232: Hardware Organization and Design

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

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

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

CS 316: Procedure Calls/Pipelining

MIPS function continued

Functions in MIPS. Functions in MIPS 1

Functions and Procedures

EE 361 University of Hawaii Fall

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

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

Q1: /14 Q2: /12 Q3: /8 Q4: /8. Total: /42

Course Administration

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

Control Instructions

ECE260: Fundamentals of Computer Engineering

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

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

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.

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

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

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

Today. Putting it all together

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

MIPS Assembly (Functions)

MIPS Functions and the Runtime Stack

ECE 30 Introduction to Computer Engineering

MIPS Functions and Instruction Formats

Stacks and Procedures

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam Solutions 14 December 2016

Lecture 5: Procedure Calls

Lecture 7: Procedures and Program Execution Preview

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Implementing Procedure Calls

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

MIPS%Assembly% E155%

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

ECE331: Hardware Organization and Design

CSE Lecture In Class Example Handout

Code Generation. Lecture 19

Lectures 3-4: MIPS instructions

Procedure Call and Return Procedure call

ECE 2035 Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 1 May 2013

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lab 4 : MIPS Function Calls

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

CS64 Week 5 Lecture 1. Kyle Dewey

Stacks and Procedures

Lecture 5: Procedure Calls

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.

ECE260: Fundamentals of Computer Engineering

ECE 30 Introduction to Computer Engineering

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

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

Code Generation. Lecture 12

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

MIPS Assembly (FuncDons)

CS153: Compilers Lecture 8: Compiling Calls

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Chapter 2A Instructions: Language of the Computer

The Activation Record (AR)

Code Genera*on for Control Flow Constructs

CENG3420 Lecture 03 Review

Compiling Code, Procedures and Stacks

Computer Architecture

6.004 Tutorial Problems L3 Procedures and Stacks

Lecture 7: Procedures

Stacks and Procedures

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

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

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

Reduced Instruction Set Computer (RISC)

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

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Common Problems on Homework

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

More C functions and Big Picture [MIPSc Notes]

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

Assembler. Lecture 8 CS301

MIPS Assembly (FuncDons)

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

Transcription:

Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C Caller s responsibility Callee s responsibility

Consider the following function: int Foo(int a, int b) { int x; int y = 10; x = ay+b; return x;} This problem writes the MIPS code to implement Foo in parts. The beginning of Foo s activation frame is shown on the stack. This is the state as Foo is called. HI FP -> Caller s return value Caller of Foo s AF LO SP -> Caller s locals RA to caller FP of caller b a Return value ends here Foo s AF starts here Part A) Write MIPS code to complete Foo s activation frame by pushing locals on the stack. Be sure to update the frame pointer properly. (Answers are in bold face.) Foo: add $30, $29, $0 addi $29, $29, -8 addi $1, $0, 10 sw $1, -8($30) # set base of FP # alloc 2 ints: x, y # initialize y # push init val of y Part B) Compute the x = ay+b; statement by accessing a, y, and b from the stack. Be sure to update locals on the stack. lw $3, 4($30) # $3: a lw $2, 8($30) # $2: b lw $1, -8(30) # $1: y (optional, already in $1) mult $3, $1 # ay mflo $3 # $3: ay add $3, $3, $2 # $3: ay+b sw $3, -4($30) # update x = ax+b Part C) Finish the implementation of Foo by implementing its return to its caller. lw $3, -4($30) # $3: x (optional, already in $3) sw $3, 0($30) # store x in return value slot addi $29, $30, $0 # pop off locals

Suppose we have a function Fun which calls another function AppleFun with 2 args: int Fun(int I, J) { int x, y, z, Result; Result = AppleFun(I, J) + I + J; return Result;} Part of the MIPS implementation of Fun is given below. Part A) Fill in the MIPS code necessary before the call to AppleFun. You will need to prepare the stack and preserve registers that are needed after the function call. Part B) Write the MIPS code to restore the values from AppleFun s activation frame and assign the value of Result. Fun: add $6, $1, $2 addi $29, $29, -24 sw $31, 20($29) sw $30, 16($29) sw $6, 12($29) sw $2, 8($29) sw $1, 4($29) jal AppleFun lw $31, 20($29) lw $30, 16($29) lw $6, 12($29) lw $3, 0($29) addi $29, $29, 24 add $6, $3, $6 sw $6, -16($30) # assume Fun s locals are # allocated in its AF # assume Result is # allocated at offset -16 # relative to Fun s FP. # assume $1: I, $2: J # $6: I+J The Answer to PART A goes in these slots. Determine what should be preserved ($31, $30, $6) + allocate I/O params. The Answer to PART B goes in these slots. Restore preserved values and update Result. # deallocate Fun s locals # return

Now suppose Fun calls BerryFun, which is a function of 2 pointers to integers. int BerryFun(int *A, *B) {} int *C; int *D; int Fun(int I,J) { Result = BerryFun(&C, &D); } Suppose C and D are statically defined at addresses 5000 and 5004, respectively. Fill in the MIPS code to implement this function call. Fun: # assume Fun s locals are # allocated in its AF # assume Result is allocated at # offset -16 relative to Fun s FP. # assume $1: I, $2: J addi $29, $29, -20 sw $31, 16($29) The Answer to PART A goes in these sw $30, 12($29) slots. Determine what should be addi $1, $0, 5004 preserved ($31, $30) + allocate sw $1, 8($29) I/O params. Note that the inputs addi $1, $0, 5000 are addresses of C and D. sw $1, 4($29) jal BerryFun lw $31, 16($29) lw $30, 12($29) lw $3, 0($29) sw $3, -16($30) The Answer to PART B goes in these slots. Restore preserved values and update Result. # deallocate Fun s locals # return

Function pointer example int search (int a, int b, int ascending) { E = extreme(a, b, (ascending? maximum : minimum)); } int extreme(int x, int y, int (* compare)(int, int)) { if ((* compare)(x, y) == 1) result = x; else result = y; } Suppose the functions maximum and minimum are defined at locations 4600 and 4700, respectively. The current state of the stack at the beginning of search looks like this: HI LO FP -> SP -> RA of caller FP of caller ascending b a Return value Search s locals

Part A) Write MIPS code to set up Extreme s activation frame just before the jal to Extreme. Search: # Answer to Part A goes below. Set up Extreme s AF (preserve RA, FP, alloc I/O: F. ptr, x, y, rtn val) addi $29, $29, -24 sw $31, 20($29) sw $30, 16($29) lw $1, 12($30) # $1: read ascending input beq $1, $0, Descend addi $2, $0, 4600 # ptr to Maximum J Skip Descend: addi $2, $0, 4700 # ptr to Minimum Skip: sw $2, 12($29) # pass function pointer lw $3, 4($30) # read a lw $4, 8($30) # read b sw $3, 4($29) # pass a as x sw $4, 8($29) # pass b as y jal Extreme Part B) Fill in the missing MIPS code below to prepare the call to the function pointer: Extreme: addi $30, $29, $0 lw $7, 4($30) $7: x (read input) lw $8, 8($30) $8: y (read input) lw $6, 12($30) $6: f. ptr (read input) # Answer to Part B goes below. Prepare to call the function ptr, preserve RA, FP, allocate I/O. addi $29, $29, -20 sw $31, 16($29) sw $30, 12($29) sw $7, 4($29) # pass x sw $8, 8($29) # pass y addi $31, $0, FPtrCall # form RA (FPtrCall+4) addi $31, $31, 4 # (if > 32K, use lui+ori) FPtrCall: jr $6 # jump to addr in $6 lw $31, 16($29) # restore RA lw $30, 12($29) # restore FP lw $1, 0($29) # load rtn val addi $2, $0, 1 # if ((* compare)(x,y) ==1 beq $1, $2, Skip # then goto Skip lw $4, 8($30) # $4: y J End Skip: lw $4, 4($30) # $4: x End: sw $4, 0($30) # store return val