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

Similar documents
Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

EE 109 Unit 15 Subroutines and Stacks

Implementing Procedure Calls

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

CA Compiler Construction

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

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.

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

Course Administration

Run-time Environment

CS153: Compilers Lecture 8: Compiling Calls

CS 61c: Great Ideas in Computer Architecture

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

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

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

MIPS Functions and the Runtime Stack

Functions in MIPS. Functions in MIPS 1

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

CSE Lecture In Class Example Handout

Instruction Set Architectures (4)

Procedure Call and Return Procedure call

Anne Bracy CS 3410 Computer Science Cornell University

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Storage in Programs. largest. address. address

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

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

Anne Bracy CS 3410 Computer Science Cornell University

Lec 10: Assembler. Announcements

CS 316: Procedure Calls/Pipelining

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

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.

Anne Bracy CS 3410 Computer Science Cornell University

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

ECE260: Fundamentals of Computer Engineering

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

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

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

ECE232: Hardware Organization and Design

Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

Hakim Weatherspoon CS 3410 Computer Science Cornell University

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

CSCE 5610: Computer Architecture

Stacks and Procedures

Stacks and Procedures

Today. Putting it all together

MIPS Assembly Language Guide

Stacks and Procedures

CS61C : Machine Structures

MIPS Functions and Instruction Formats

MIPS Assembly (Functions)

EE 361 University of Hawaii Fall

Computer Architecture. The Language of the Machine

Lecture 5: Procedure Calls

Stacks and Procedures

CS61C : Machine Structures

Chapter 2A Instructions: Language of the Computer

MIPS%Assembly% E155%

Subroutines. we jump to a new location in the code

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

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

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

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

MIPS Instruction Set

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

M2 Instruction Set Architecture

Compiling Code, Procedures and Stacks

MIPS Assembly Language. Today s Lecture

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

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

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

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

Code Generation. Lecture 12

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

SPIM Procedure Calls

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

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

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

ECE260: Fundamentals of Computer Engineering

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Lecture 7: Procedures and Program Execution Preview

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

Code Generation. Lecture 19

ECE 250 / CS 250 Computer Architecture. Procedures

MIPS Assembly Programming

Lectures 3-4: MIPS instructions

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

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

ECE260: Fundamentals of Computer Engineering

Lecture 7: Procedures

Transcription:

1 Arguments and Return Values 2 EE 109 Unit 16 Stack Frames MIPS convention is to use certain registers for this task used to pass up to 4 arguments. If more arguments, use the stack used for return value Only 1 return value but it may be a double-word (64-bits) in which case $v1 will also be used Number Name Purpose $0 $zero Constant 0 $1 $at Assemblertemporary (psuedo-instrucs.) $2-$ $v0-$v1 Return value ($v1 only used for dword) $4-$7 -$a Arguments(first 4 of a subroutine) $8-$15, $24,$25 $t0-$t9 Temporary registers $16-$2 $s0-$s7 Saved registers $26-$27 $k0-$k1 Kernel reserved $28 $gp Global pointer (staticglobal data var s.) $29 Stack pointer $0 Frame pointer $1 Return address Arguments and Return Values Assembly & HLL s 4 int ans,arg1,arg2; ans = avg(arg1, arg2); int avg(int a, int b) { int temp=1; // local var s return a+b >> temp; MAIN: la $s0, arg1 # Get addr. la $s1, arg2 # of arg1/arg2 lw, 0($s0) # Get val. of lw $a1, 0($s1) # arg1/arg2 jal AVG # call function la $s2, ans sw $v0, ($s2) # store ans. AVG: li $t0, 1 # temp=1 add $v0,,$a1 # do a+b srav $v0,$v0,$t0 # do shift Equivalent Assembly When coding in assembly, a programmer can optimize usage of registers and store only what is needed to memory/stack Can pass additional arguments in registers (beyond -$a) Can allocate variables to registers (not use memory) Can handle spilling registers to memory only when necessary When coding in an HLL & using a compiler, certain conventions are followed that may lead to heavier usage of the We have to be careful not to registers that have useful data

Compiler Handling of Subroutines 5 Stack Frame Motivation 6 High level languages (HLL) use the stack: to values including the return address to pass additional to a subroutine for storage of declared in the subroutine Compilers usually put data on the stack in a certain order, which we call a To access this data on the stack a pointer called the ($0=$ ) is often used in addition to the normal stack pointer () Assume the following C code Now assume each function was written by a different programmer on their own (w/o talking to each other) What could go wrong? { caller(x, nums); return 0; return callee(5)+a+z; { return (val+)/2; Stack Frame Motivation The caller needs to ensure the calleeroutine does not overwrite a needed register 1. Caller may have his own in -$a and then need to call a subroutine and use -$a { caller(x, nums); return 0; return callee(5)+a+z; { return (val+)/2; CALLER: lw $s0, 0($a1) li, 5 1 add $v0,$v0, 1 CALLEE: addi $s0,, sra $v0,$s0,1 New Value Loaded into But old value needed here 7 Stack Frame Motivation The caller needs to ensure the calleeroutine does not overwrite a needed register 1. Caller may have his own arguments in -$a and then need to call a subroutine and use -$a 2. Return address () [We've already seen this problem] { caller(x, nums); return 0; return callee(5)+a+z; { return (val+)/2; 2 CALLER: lw $s0, 0($a1) li, 5 2 add $v0,$v0, 2 CALLEE: addi $s0,, sra $v0,$s0,1 8

Stack Frame Motivation The caller needs to ensure the calleeroutine does not overwrite a needed register 1. Caller may have his own arguments in -$a and then need to call a subroutine and use -$a 2. Return address (). Register values calculated the call but used the call (e.g. $s0) { caller(x, nums); return 0; return callee(5)+a+z; { return (val+)/2; CALLER: lw $s0, 0($a1) li, 5 add $v0,$v0, CALLEE: addi $s0,, sra $v0,$s0,1 9 Callee unknowingly overwrites $s0 which will cause CALLER to malfunction Solution If you're not sure whether some other subroutine is using a register (and needs it later) it to the stack before you overwrite it Recall a push: addi,, -4 sw reg_to_save, 0() it from the stack before you return Recall a pop: lw reg_to_restore, 0() addi,, 4 CALLER: addi,,-4 sw, 0() Save lw $s0, 0($a1) addi,,-4 sw,0() li, 5 lw, 0() addi,,4 add $v0,$v0, lw, 0() addi,,4 Save Restore Restore 10 Solution If you're not sure whether some other subroutine is using a register (and needs it later) 0040208c 1000120 0040208c 0040208c 0000 0000 0000 0000 At start of caller() 7fffeff8 1 7fffeff8 2 Before '' instruction Before '' 7fffeff8 CALLER: addi,,-4 1 sw, 0() lw $s0, 0($a1) addi,,-4 sw,0() li, 5 lw, 0() addi,,4 add $v0,$v0, lw, 0() addi,,4 2 11 Local Variables A functions local variables are also allocated on the stack // Allocate integers int ans, arg1=5, arg2=7; ans = avg(arg1, arg2); // vars. deallocated here 7fffefec arg2=7 arg1=5 ans 7fffeff8 7fffeff0 7fffefec MAIN: addi,, -4 sw, 0() # save # Now allocate integers addi,, li $t0, 5 sw $t0, () li $t0, 7 sw $t0, () lw, 4() # Get val. of lw $a1, 8() # arg1/arg2 jal AVG # call function sw $v0, () #store ans. # deallocate local vars addi,, lw, 0() addi,,4 Equivalent Assembly 12

Local Variables Locally declared arrays are also allocated on the stack Be careful: variables and arrays often must start on well-defined address boundaries char mystr[14] = "hello"; double z; mystr[1-12] mystr[11-8] mystr[7-4] mystr[-0] 7fffefe0 l pad l z z e o h 7fffeff8 7fffeff0 7fffefec 7fffefe8 7fffefe4 7fffefe0 MAIN: addi,, -4 sw, 0() # save # Now allocate array addi,, # not -14 # May pad to get to 8-byte # boundary.. # now alloc. z addi,, # deallocate local vars addi,, lw, 0() addi,,4 Equivalent Assembly 1 Stack Frames Frame = Def: All data on stack belonging to a subroutine/function Space for arguments (in addition to -$a) Space for saved registers (, $s0-$s7, ) Space for local variables (those declared in a function) int ans, x, y; ans = avg(x, y); int avg(int a, int b) { int temp=1; // local var s Main Routine s Stack Frame Subroutines Stack Frame Stack Growth (ans, x, y) (,, Sub-subr s arguments Stack Frame Organization 14 Subroutine s arguments (copy of x,y) Accessing Values on the Stack Stack pointer () is usually used to access only the value on the stack To access arguments and local variables, we need to access values in the stack We can simply use an from [ 8() ] Unfortunately other push operations by the function may change the requiring different displacements at different times for the same variable This can work, but can be confusing lw $t0, 16() # access var. x addi,,-4 # changes sw $t0, 20() # access x with # diff. displacement + offset (ans) (,, To access parameters we could try to use some displacement [i.e. d() ] but if changes, must use new d value 15 Func1 frame Func2 frame Frame Pointer Solution: Use another pointer that during execution of a subroutine We call this the Frame Pointer () and it usually points to the base of the current routines frame (i.e. the firstword of the stack frame) [other implementations might have it points at the last word of the frame] will not change during the course of subroutine execution Can use constant offsets from to access parameters or local variables Key 1: doesn t change during subroutine execution Key 2: Number of arguments, local variables, and saved registers is known at compile time so compiler can easily know what offsets to use - offset (ans) + offset (,, 16

Frame Pointer and Subroutines 17 18 Problem is that each executing subroutine needs The called subroutine must the caller s and setup its own Usually performed immediately after allocating frame space and saving The called subroutine must the caller s before it returns caller s FP + offset - offset (ans) (,, Part II STACK FRAMES 19 20 Review Simple Example The stack is used to store Arguments passed by one subroutine to the next Especially if there are more arguments than can fit in registers (i.e. MIPS uses -$a for arguments but what if there are 5 arguments) Saved register values that a subroutine might need later Best example is Local variables The stack will be accessed with but also with Each offset from is a different variable (,, void caller(int a){ callee(1); int callee(int num){ int x = 6; return x + num; Assembly Code CALLER: addi,,-12 sw,8() sw,4() addi,,8 sw,4() li, 1 lw,4() lw,8() lw,4() addi,,12 CALLEE: addi,,-12 sw,4() sw,0() addi,,8 li $t0,6 sw $t0,0() add $v0,$t0, lw,0() lw,4() addi,,12 Stack during execution of CALLEE Space for a Prev. Prev. Space for num x Caller s Callee Caller Prev.

21 22 Example 2 Example 2 int ans; int x = ; ans = avg(1,5); x = x + 1; int avg(int a, int b) { int temp = 1; return a + b >> temp; MAIN: li $s0, li, 1 li $a1, 5 jal AVG sw $v0,0($gp) addi $s0,$s0,1 sw $s0,-4() AVG: addi,,-24 sw,8() sw,4() addi,,20 sw $s0,-20() li $s0,1 sw $s0,-4() add $v0,,$a1 srav $v0,$v0,$s0 lw $s0,-20(fp) lw,4() lw,8() addi,,24 MAIN: li $s0, li, 1 li $a1, 5 jal AVG sw $v0,0($gp) addi $s0,$s0,1 sw $s0,-4() AVG: addi,,-24 sw,8() sw,4() addi,,20 sw $s0,-20() li $s0,1 sw $s0,-4() add $v0,,$a1 srav $v0,$v0,$s0 lw $s0,-20(fp) lw,4() lw,8() addi,,24 Convention: Local variable section must start and end on an 8-byte boundary +6 +2 +28 +24 +20 +16 +12 +8 +4 Empty x (arg ) (arg 2) (arg 1) (arg 0) Empty temp Empty Saved Saved $s0 Stack during execution of AVG +16 +12 +8 +4-4 -8-12 -16-20 2 24 Real Output from gcc Stack Frame Example int avg(int x, int y) { return (x+y)/2;.file 1 "avg.cpp".align 2.globl _Zavgii $LFB2:.ent _Zavgii _Zavgii: addiu,,-8 $LCFI0: sw,0() $LCFI1: move, $LCFI2: sw $4,8() sw $5,12() lw $,8() lw $2,12() addu $,$,$2 sra $2,$,1 srl $2,$2,1 addu $2,$,$2 sra $2,$2,1 move, lw,0() addiu,,8 j $1.end _Zavgii $LFE2: Assembly Code Code generated by mips-gcc Old +16 +12 +8 +4 (arg ) (arg 2) Room for y Room for x Padding (align 8) Saved Caller's frame AVG's frame Stack during execution of AVG Old +16 +12 +8 +4 This MIPS compiler points the at the top of the frame and not the bottom (i.e. it will usually match ) { int x, y, z x = 1; y = 2; z = caller(x, y) int caller(int a, b) { int sum; sum = a + b + callee(6); return sum; int callee(int t) { t = t + 1; return t;

Stack Summary 25 Data associated with a subroutine is a relationship (i.e. many instances may be running at the same time recursion). A stack allows for any number of concurrent instances to all have their own storage. Stack grows towards addresses Stack frames defines of data related to a subroutine A subroutine should leave the stack & in the same condition it found it are dedicated registers to maintaining the system stack Arg. n-1 to 4 (Arg. ) (Arg. 0) Local data m-1 to 0 (also $t0-9) if needed Padding/Empty Saved Saved regs. ($s0-$s7) Arg. n-1 to 4 (Arg. ) (Arg. 0)