Lecture 7: Procedures and Program Execution Preview

Similar documents
Lecture 7: Procedures

CS61C : Machine Structures

CS61C : Machine Structures

UCB CS61C : Machine Structures

CS61C : Machine Structures

Lecture 10: Program Development versus Execution Environment

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

ECE 15B Computer Organization Spring 2010

MIPS Functions and Instruction Formats

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

CS61C : Machine Structures

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

ú There are CONVENTIONS when calling procedures! Stack main ()! { a(0);! }! void a (int m)! frame" Garcia, Spring 2013 UCB printf() (y==?

UCB CS61C : Machine Structures

Bonus slides. Garcia, Spring 2014 UCB. CS61C L11 Introduction to MIPS : Procedures II & Logical Ops (18)

CS 110 Computer Architecture MIPS Instruction Formats

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CS61C : Machine Structures

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

And in Review. Register Conventions (2/4) saved. Register Conventions (1/4) Register Conventions (4/4) Register Conventions (3/4) volatile

CS61C : Machine Structures

CS61C : Machine Structures

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

Review! Lecture 5 C Memory Management !

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

CSE Lecture In Class Example Handout

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

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

Functions in MIPS. Functions in MIPS 1

Agenda. Strings: C vs. Java. Loading, Storing bytes. Support for Characters and Strings 6/29/2011

Today. Putting it all together

Implementing Procedure Calls

ECE260: Fundamentals of Computer Engineering

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

Anne Bracy CS 3410 Computer Science Cornell University

CSE Lecture In Class Example Handout

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

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

Lectures 13 & 14. memory management

ECE232: Hardware Organization and Design

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

Course Administration

Anne Bracy CS 3410 Computer Science Cornell University

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

In Java we have the keyword null, which is the value of an uninitialized reference type

MIPS Functions and the Runtime Stack

CS 61c: Great Ideas in Computer Architecture

Lecture 5: Procedure Calls

CS61CL : Machine Structures

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

Lecture 5: Procedure Calls

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

Stacks and Procedures

Final 9am instead of 9:30! Your grade will be ed by autograder! Jeremy Huddleston sb $t1 0($s2)!

MIPS Procedure Calls. Lecture 6 CS301

Language of the Machine Recursive functions

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

CS 61C: Great Ideas in Computer Architecture. Lecture 6: More MIPS, MIPS Func.ons. Instructor: Sagar Karandikar

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

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

Control Instructions

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

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

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.

SPIM Procedure Calls

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

Procedure Call and Return Procedure call

Compilers and computer architecture: A realistic compiler to MIPS

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

Instruction Set Architectures (4)

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

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Stacks and Procedures

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

Run-time Environment

More C functions and Big Picture [MIPSc Notes]

ECE331: Hardware Organization and Design

Anne Bracy CS 3410 Computer Science Cornell University

CS153: Compilers Lecture 8: Compiling Calls

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

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

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

MIPS Assembly (Functions)

EE 109 Unit 15 Subroutines and Stacks

CA Compiler Construction

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

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

Hakim Weatherspoon CS 3410 Computer Science Cornell University

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

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

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

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

Functions and Procedures

Transcription:

Lecture 7: Procedures and Program Execution Preview CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego

Outline Review Procedure Call Return Calling Nested Programs Memory Management at Compile and Runtime Reference: P+H 2.8

Review: Function Call Bookkeeping Registers play a major role in keeping track of information for function calls Register conventions: Return address $ra Arguments $a0, $a1, $a2, $a3 Return value $v0, $v1 Local variables $s0, $s1,, $s7 The stack is also used for register spilling.

Instruction Support for Functions Syntax for jal (jump and link) is same as for j (jump): jal label jal should really be called laj for link and jump : Step 1 (link): Save address of next instruction into $ra (Why next instruction? Why not current one?) Step 2 (jump): Jump to the given label

Instruction Support for Functions Syntax for jr (jump register): jr register Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to Only useful if we know exact address to jump Very useful for function calls: jal stores return address in register ($ra) jr $ra jumps back to that address

Nested Procedures int sumsquare(int x, int y) { return mult(x,x)+ y; } Something called sumsquare, now sumsquare is calling mult. So there s a value in $ra that sumsquare wants to jump back to, but this will be overwritten by the call to mult. Need to save sumsquare return address before call to mult.

Nested Procedures In general, may need to save some other info in addition to $ra. When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes. E.g., C globals Heap: Variables declared dynamically Stack: Space to be used by procedure during execution; this is where we can save register values

C Memory Allocation Address $sp stack pointer 0 Stack Heap Static Code Space for saved procedure information Explicitly created space, e.g., malloc(); C pointers Variables declared once per program Program

Using the Stack So we have a register $sp which always points to the last used space in the stack. To use stack, we decrement this pointer by the amount of space we need and then fill it with info. So, how do we compile this? int sumsquare(int x, int y) { return mult(x,x)+ y; }

Using the Stack Hand-compile sumsquare: push addi $sp,$sp,-8 sw $ra, 4($sp) sw $a1, 0($sp) int sumsquare(int x, int y) { return mult(x,x)+ y; } # space on stack # save ret addr # save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) add $v0,$v0,$a1 lw $ra, 4($sp) addi $sp,$sp,8 pop jr $ra mult:... # restore y # mult()+y # get ret addr # restore stack

Steps for Making a Procedure Call 1) Save necessary values onto stack 2) Assign argument(s), if any 3) jal call 4) Restore values from stack

Rules for Procedures Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 Return value is always in $v0 (and if necessary in $v1) Must follow register conventions (even in functions that only you will call)! So what are they?

MIPS Registers The constant 0 $0 $zero Reserved for Assembler $1 $at Return Values $2-$3 $v0-$v1 Arguments $4-$7 $a0-$a3 Temporary $8-$15 $t0-$t7 Saved $16-$23 $s0-$s7 More Temporary $24-$25 $t8-$t9 Used by Kernel $26-27 $k0-$k1 Global Pointer $28 $gp Stack Pointer $29 $sp Frame Pointer $30 $fp Return Address $31 $ra Use names for registers -- code is clearer!

Other Registers $at: may be used by the assembler at any time; unsafe to use $k0-$k1: may be used by the OS at any time; unsafe to use $gp, $fp: don t worry about them Note: Feel free to read up on $gp and $fp in Appendix A, but you can write perfectly good MIPS code without them.

Saved Register Conventions $0: No Change. Always 0. $s0-$s7: Restore if you change. Very important, that s why they re called saved registers. If the callee changes these in any way, it must restore the original values before returning. $sp: Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller won t be able to restore values from the stack. HINT -- All saved registers start with S!

Volatile Register Conventions $ra: Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call. $v0-$v1: Can Change. These will contain the new returned values. $a0-$a3: Can change. These are volatile argument registers. Caller needs to save if they ll need them after the call. $t0-$t9: Can change. That s why they re called temporary: any procedure may change them at any time. Caller needs to save if they ll need them afterwards.

Basic Structure of a Function Prologue entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) #save $ra save other regs if need be ra Body... (call other functions ) Epilogue memory restore other regs if need be lw $ra, framesize-4($sp) #restore $ra addi $sp,$sp, framesize jr $ra

Example main() { int i,j,k,m; /* i-m:$s0-$s3 */... i = mult(j,k);... m = mult(i,i);... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;}

Example start: add $a0,$s1,$0 add $a1,$s2,$0 jal mult add $s0,$v0,$0... add $a0,$s0,$0 add $a1,$s0,$0 jal mult add $s3,$v0,$0... done main() { int i,j,k,m; ; /* i-m:$s0i m:$s0-$s3 $s3 */... i = mult(j,k);... m = mult(i,i);... } # arg0 = j # arg1 = k # call mult # i = mult() # arg0 = i # arg1 = i # call mult # m = mult()

mult:add Example int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;} $t0,$0,$0 # prod=0 Loop:slt $t1,$0,$a1# mlr > 0? beq $t1,$0,fin # no=>fin add $t0,$t0,$a0 # prod+=mc addi $a1,$a1,-1# mlr-=1 j Loop # goto Loop Fin:add $v0,$t0,$0 # $v0=prod jr $ra # return

Example Notes: No jal calls are made from mult and we don t use any saved registers, so we don t need to save anything onto stack Temp registers are used for intermediate calculations (could have used s registers, but would have to save the caller s on the stack.) $a1 is modified directly (instead of copying into a temp register) since we are free to change it Result is put into $v0 before returning

C Memory Management C has 3 pools of memory Static storage: global variable storage, basically permanent, entire program run The Stack: local variable storage, parameters, return address ("stack frame" in C) The Heap (dynamic storage): data lives until deallocated by programmer C requires knowing where objects are in memory, otherwise don't work as expect

Normal C Memory Management A program s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via malloc() ; resizes dynamically, grows upward static data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change ~ FFFF FFFF hex stack heap static data code ~ 0 hex For now, OS somehow prevents accesses between stack and heap (gray hash lines). Wait for virtual memory

Intel 80x86 C Memory Management A C program s 80x86 address space : heap: space requested for pointers via malloc(); resizes dynamically, grows upward static data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change stack: local variables, grows downward ~ 08000000 hex heap static data code stack

Memory Management How do we manage memory? Code, Static storage are easy: they never grow or shrink Stack space is also easy: stack frames are created and destroyed in last-in, first-out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any time

Where allocated? int myglobal; main() { int temp; } Structure declaration does not allocate memory Variable declaration does allocate memory If declare outside a procedure, allocated in static storage If declare inside procedure, allocated on the stack and freed when procedure returns

The Stack Stack frame includes: Return address Parameters Space for other local variables SP Stack frames contiguous blocks of memory; stack pointer tells where top stack frame is When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames frame frame frame frame

Stack Last In, First Out (LIFO) memory usage main () { a(0); } void a (int m) { b(1); } void b (int n) { c(2); } void c (int o) { d(3); } void d (int p) { } Stack Pointer Stack Pointer Stack Pointer Stack Pointer Stack Pointer stack

Who cares about stack management? Pointers in C allow access to deallocated memory, leading to hard-to-find bugs! int * ptr () { int y; y = 3; return &y; }; main () { SP main ptr() (y==3) SP main SP int *stackaddr,content; stackaddr = ptr(); content = *stackaddr; printf("%d", content); /* 3 */ content = *stackaddr; printf("%d", content); /*13451514 */ }; main printf() (y==?)

The Heap (Dynamic memory) Large pool of memory, not allocated in contiguous order back-to-back requests for heap memory could result blocks very far apart where C++/Java new command allocates memory In C, specify number of bytes of memory explicitly to allocate item int *ptr; ptr = (int *) malloc(4); /* malloc returns type (void *), so need to cast to right type */ malloc(): Allocates raw, uninitialized memory from heap

Dynamic Memory Allocation C has operator sizeof() which gives size in bytes (of type or variable) Assume size of objects can be misleading & is bad style, so use sizeof(type) Many years ago an intwas 16 bits, and programs assumed it was 2 bytes

Dynamic Memory Allocation To allocate room for something new to point to, use malloc() (with the help of a typecast and sizeof): ptr = (int *) malloc (sizeof(int)); Now, ptr points to a space somewhere in memory of size (sizeof(int)) in bytes. (int *) simply tells the compiler what will go into that space (called a typecast). malloc is almost never used for 1 variable ptr = (int *) malloc (n*sizeof(int)); This allocates an array of n integers.

Dynamic Memory Allocation Once malloc() is called, the memory location might contain anything, so don t use it until you ve set its value. After dynamically allocating space, we must dynamically free it: free(ptr); Use this command to clean up.