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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Functions in MIPS. Functions in MIPS 1

Instruction Set Architectures (4)

MIPS Procedure Calls - Review

Functions and Procedures

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

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

Today. Putting it all together

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

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

MIPS Procedure Calls. Lecture 6 CS301

CA Compiler Construction

Run-time Environment

CS 316: Procedure Calls/Pipelining

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

CS153: Compilers Lecture 8: Compiling Calls

CSE Lecture In Class Example Handout

Course Administration

Implementing Procedure Calls

EE 361 University of Hawaii Fall

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

Compiling Code, Procedures and Stacks

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

Storage in Programs. largest. address. address

ECE260: Fundamentals of Computer Engineering

ECE232: Hardware Organization and Design

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

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

Control Instructions

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.

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.

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

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

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

Stacks and Procedures

MIPS Functions and the Runtime Stack

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

Lab 4 : MIPS Function Calls

MIPS Functions and Instruction Formats

MIPS Assembly (Functions)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Stacks and Procedures

Stacks and Procedures

Procedure Call and Return Procedure call

Lec 10: Assembler. Announcements

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

SPIM Procedure Calls

EE 109 Unit 15 Subroutines and Stacks

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

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

Stacks and Procedures

Anne Bracy CS 3410 Computer Science Cornell University

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

Lecture 7: Procedures and Program Execution Preview

CS64 Week 5 Lecture 1. Kyle Dewey

Topic 3-a. Calling Convention 2/29/2008 1

MIPS Assembly (FuncDons)

Common Problems on Homework

More C functions and Big Picture [MIPSc Notes]

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

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

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

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

Lecture 5: Procedure Calls

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

CS 61c: Great Ideas in Computer Architecture

Lecture 7: Procedures

Instructions: Assembly Language

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 6: Assembly Programs

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

Procedures and Stacks

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Anne Bracy CS 3410 Computer Science Cornell University

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

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

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

Chapter 2A Instructions: Language of the Computer

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

CS61C : Machine Structures

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

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

Lecture 5: Procedure Calls

CSE Lecture In Class Example Handout

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

Lab 4 Prelab: MIPS Function Calls

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

CENG3420 Lecture 03 Review

CS61C : Machine Structures

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

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

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

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

COMP2611: Computer Organization MIPS function and recursion

UCB CS61C : Machine Structures

Shift and Rotate Instructions

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Transcription:

Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space

Memory Usage A software convention stack 0x7fffffff dynamic data static data Code Reserved 0x10000000 0x04000000 0x00000000 stack segment: holds process-local storage managed in LIFO fashion automatically allocated by the operating system stack grows down (toward lower addresses) as data is put onto it stack pointer $sp is automatically loaded to point to the last allocated location on the stack

Using the Stack Stack pointer ($sp or $29) points to last allocated location on the stack Stack operations: each requires 2 instructions one to adjust the stack pointer one to transfer the data onto/off the stack push: subu $sp,$sp,4 # allocate a new word on the stack sw $8,0($sp) # push the contents of $8 pop: lw $8,0($sp) # pop the contents into $8 addu $sp,$sp,4 # adjust the stack pointer the assembler recognizes a stack pointer operation & generates subui and addui more efficient to allocate all the space you ll need at once often allocate & deallocate the same amount

Example Using the Stack saving values on the stack subu $sp, $sp, 12 sw $t1, 8($sp) sw $t0, 4($sp) sw $s0, 0($sp) restoring values from the stack lw $t1, 8($sp) $sp lw $t0, 4($sp) lw $s0, 0($sp) addu $sp, $sp, 12 $t1 $t0 $s0 allocate all the stack space you will need for a group of data the stack locations are above where the stack pointer is pointing, so the displacements from $sp will be positive

Procedure Calling Convention Protocol between the caller & the callee: who saves which registers how parameters are passed: in registers? on the stack? both? where the return address is located Why have a calling convention? caller & callee can interact correctly & efficiently can have subroutines that are written in different languages & compiled with different compilers easier for a standard debugger A contract between the hardware & software hardware: performs simple instructions has dedicated registers software: controls the sequence of instructions for calling & returning determines which register points to the stack the rules of the protocol Each architecture has its own protocol(s)

Why Have Two Classes of Registers? To reduce register spilling (storing registers in memory because you need to use them for something else) Caller knows what registers it needs after the procedure call. Callee knows what registers it is going to use. Therefore divide the responsibilities. Caller-saved registers (t s): not preserved across procedure calls caller saves them if it wants to use them after the procedure call if it doesn t need them later, it doesn t have to save them so a caller uses them for short-lived values callee knows it can use these registers without saving them savings: caller-saved registers only spilled if the caller uses them after the procedure call Callee-saved registers (s s): preserved across procedure calls caller doesn t save them even if they are used after the return a caller uses them for long-lived values if the callee needs to use them, it must save them first savings: callee-saved registers only spilled if the callee uses them

MIPS Procedure Calling Convention Caller saves the a registers on the stack saves the t registers on the stack: $t0 - $t9 passes the arguments in $a0 - $a3 saves additional passed values on the stack executes jal which puts the return PC in $ra Callee determines the size of the stack frame & changes $sp to point to the end of the frame stack frame: a region of the stack that holds all the data for a single procedure (also called procedure call frame, activation record) subu $sp,$sp,framesize puts its return address & frame pointer ($fp) on the stack saves the s registers on the stack: $s0 - $s7 sets $fp to $sp + stack frame size - 4 puts its local variables on the stack if the callee calls a procedure, it becomes a caller Neither modifies the other s portion of the stack

Stack Frame frame pointer ($fp or $30): points to the first word of a stack frame value never changes: stable offset base for locations within the stack frame not always used

Example Call Sequence Assume: arguments are in $t0 & $t1 want to save caller-saved registers $t6 & $t7 move $a0,$t0 # first argument move $a1,$t3 # second argument subu $sp,$sp,8 # adjust the stack pointer sw $t6,4($sp) # save $t6 on the stack sw $t7,0($sp) # save $t7 on the stack jal target The first thing the callee will do (assuming it does not have to save any $s registers) target: subu $sp,$sp,4 # adjust the stack pointer sw $ra,0($sp) # save the return address

MIPS Procedure Return Convention Return convention: put the return values in $v0, $v1 restore the s (caller-saved) registers restores $fp & $ra pop the activation record by adding its size to $sp return to the caller by jr $ra

Example Return Sequence Before returning, the callee will put results in $v0 & $v1 if needed & then: lw $ra,0($sp)# load the return address into $ra addu $sp,$sp,4 # adjust the stack pointer jr $ra # return The caller will restore $t6 & $t7 and adjust the stack lw $t6,4($sp) # restore $t6 lw $t7,0($sp) # restore $t7 addu $sp,$sp,8 # adjust the stack pointer

Use of Registers & the Stack Registers are used for: passing a small number of arguments (up to 4, $a0 - $a3) passing the return address to the callee ($ra) locating the beginning of the stack frame ($fp) keeping track of the top of the stack ($sp) returning function values ($v0, $v1) Stackisusedfor: passing parameters if more than 4 saving the caller s register parameters (a s) (might be used by the callee) saving the caller s registers that are going to be used by the caller after the procedure returns ($t s) local data for the callee the return address of the caller (in case the callee calls a procedure) the frame pointer (same reason) any callee-saved registers the callee is going to use ($s s) stack frame not always used