Procedure Call. Procedure Call CS 217. Involves following actions

Similar documents
Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Lecture XXIV: Review Xuan Guo CSC 3210 Computer Organization and Programming Georgia State University April 23, 2015 Xuan Guo Lecture XXIV: Review

Xuan Guo. CSC 3210 Computer Organization and Programming Georgia State University. March 31, Lecture XX: Subroutines (3) Xuan Guo

Course Administration

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

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

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

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

System Software Assignment 1 Runtime Support for Procedures

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

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

CSE Lecture In Class Example Handout

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

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

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

Control Instructions

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

Functions and Procedures

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

SPARC Architecture. SPARC Registers

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

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

Hakim Weatherspoon CS 3410 Computer Science Cornell University

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

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Run-time Environment

CS213. Machine-Level Programming III: Procedures

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Code Generation. Lecture 12

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.

CSE Lecture In Class Example Handout

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Anne Bracy CS 3410 Computer Science Cornell University

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CA Compiler Construction

MIPS Functions and the Runtime Stack

Anne Bracy CS 3410 Computer Science Cornell University

Assembly Language: Function Calls

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

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

Chapter 10 Memory Model for Program Execution. Problem

CSCE 5610: Computer Architecture

Lecture 5: Procedure Calls

Assembly Language: Function Calls" Goals of this Lecture"

Code Generation. Lecture 19

Storage in Programs. largest. address. address

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

Lecture 5: Procedure Calls

Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Assembly Language: Function Calls" Goals of this Lecture"

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

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

Systems I. Machine-Level Programming V: Procedures

6.001 Recitation 23: Register Machines and Stack Frames

Giving credit where credit is due

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

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

Compiling Code, Procedures and Stacks

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

x86 assembly CS449 Fall 2017

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

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

CS429: Computer Organization and Architecture

G Programming Languages - Fall 2012

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

Anne Bracy CS 3410 Computer Science Cornell University

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

CS153: Compilers Lecture 8: Compiling Calls

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Control Abstraction. Hwansoo Han

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Implementing Procedure Calls

LECTURE 19. Subroutines and Parameter Passing

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

ECE232: Hardware Organization and Design

Compilation 2014 Activation Records (Part 1)

Compiler Design. Homework 1. Due Date: Thursday, January 19, 2006, 2:00

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

MIPS Functions and Instruction Formats

THEORY OF COMPILATION

Topic 7: Activation Records

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

EE 361 University of Hawaii Fall

CS 314 Principles of Programming Languages. Lecture 13

MIPS Procedure Calls. Lecture 6 CS301

Lecture 7: Procedures and Program Execution Preview

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

Transcription:

Procedure Call CS 217 Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return a + b; int main() { int c = add(3, 4); printf( %d\n, c); return 0; 1

Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return a + b; int main() { int c = add(3, 4); printf( %d\n, c); return 0; Jump Instruction jmpl address, reg 10 reg 111000 rs1 0 0 rs2 10 reg 111000 rs1 1 simm13 31 29 24 18 leaves PC in reg 13 12 4 reg = PC; /* return address */ PC = npc; npc = rs1 + op2; 2

Call Instruction call label 01 disp30 31 29 leaves PC (location of call) in %o7 (%r15) %o7 = PC; /* return address */ PC = npc; npc = PC + sign_extend(disp30) << 2; Like: jmpl label, %o7 Procedure Call Example Simplest example: leaf procedure (c=a+b) ld a,%o0 ld a,%o0 ld b,%o1 call _add call _add ld b,%o1 nop st %o0,c st %o0,c 3

Return Instruction jmpl %o7+8,%g0 transfers control from caller to callee synthetic instructions: ret and retl why +8? _add: save %sp,..., %sp add %o0, %o1, %o1 ret restore Calls with Function Pointers jmpl reg,%o7 jumps to the 32-bit address specified in reg leaves PC (return address) in %o7 (%r15) example: c = (*apply)(a,b); ld b,%o0 ld c,%o1 ld apply,%o3 jmpl %o3,%o7; nop st %o0,a 4

Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return a + b; int main() { int c = add(3, 4); printf( %d\n, c); return 0; Procedure Call Requirements Pass a variable number of arguments Pass and return structures Allocate and deallocate space for local variables Save and restore caller s registers Handle nested procedure calls Entry and exit sequences collaborate to implement these requirements 5

Nested/Recursive Calls A calls B, which calls C A: B: C: call B call C return return return Must work when B is A Arguments and Return Values By convention caller places arguments in the out registers callee finds its arguments in the in registers only the first 6 arguments are passed in registers the rest are passed on the stack 6

Arguments and Return Value (cont) Registers at call time caller callee %o7 %i7 return address -8 (%r15) %o6 %i6 stack/frame pointer (%r14) %o5 %i5 sixth argument %o0 %i0 first argument Arguments and Return Value (cont) Registers at return time caller callee %o5 %i5 sixth return value %o4 %i4 fifth return value %o0 %i0 first return value 7

Register Windows Machine has more than 32 registers Each procedure gets 16 new registers All procedures can use globals The window slides at call time caller s out registers become callee s in registers Instructions save slides the window forward restore slides the window backwards decrement/increments CWP register Finite number of windows (usually 8) cwp wim in local out Register Windows Machine has more than 32 registers Each procedure gets 16 new registers All procedures can use globals The window slides at call time caller s out registers become callee s in registers Instructions save slides the window forward restore slides the window backwards decrement/increments CWP register Finite number of windows (usually 8) cwp wim in local out 8

Register Windows Machine has more than 32 registers Each procedure gets 16 new registers All procedures can use globals The window slides at call time caller s out registers become callee s in registers Instructions save slides the window forward restore slides the window backwards decrement/increments CWP register Finite number of windows (usually 8) cwp wim in local out Register Windows Machine has more than 32 registers Each procedure gets 16 new registers All procedures can use globals The window slides at call time caller s out registers become callee s in registers Instructions save slides the window forward restore slides the window backwards decrement/increments CWP register Finite number of windows (usually 8) cwp wim in local out 9

Register Windows Machine has more than 32 registers Each procedure gets 16 new registers All procedures can use globals The window slides at call time caller s out registers become callee s in registers Instructions save slides the window forward restore slides the window backwards decrement/increments CWP register Finite number of windows (usually 8) cwp wim in local out Register Windows (cont) %i7 %i0 %l7 %l0 %o7 %o0 %i7 %i0 %l7 %l0 %o7 %o0 %i7 %i0 %l7 %l0 %o7 %o0 save restore 10

Window Managment Call time (save) save %sp,n,%sp current window becomes previous window decrements CWP and checks for overflow adds N to the stack pointer (allocates N bytes if N<0) if overflow occurs, save registers on the stack (must be enough stack space) Return time (restore) previous window becomes current window increments CWP and checks for underflow Stack Procedure call information stored on stack locals, including compiler temporaries caller s registers, if necessary callee s arguments, if necessary Sparc s stack grows down from high to low address The stack pointer (%sp) points to top word on the stack (must be multiple of 8) 11

Stack Frame %fp (old %sp) %fp - offset %sp + offset %sp + offset %sp previous frame local & temp vars saved FP regs arguments 7,8, argument 6 argument 5 argument 4 argument 3 argument 2 argument 1 ptr to struct rtrn 16 words to hold saved in/out regs Example Stack Frames main() { t(1,2,3,4,5,6,7,8); t(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { int b1 = a1; return s(b1,a8); s(int c1, int c2) { return c1 + c2; 12

Example (cont) _main: save %sp,-104,%sp set 1,%o0 set 2,%o1 set 3,%o2 set 4,%o3 set 5,%o4 set 6,%o5 set 7,%i5 st %i5,[%sp+4*6+68] set 8,%i5 st %i5,[%sp+4*7+68] call _t; nop ret; restore Example (cont) _t: save %sp,-96,%sp st %i0,[%fp-4] ld [%fp-4],%o0 ld [%fp+96],%o1 call _s; nop mov %o0,%i0 ret; restore _s: add %o0,%01,%o0 retl; nop 13

main a8 a7 a6 a5 a4 a3 a2 a1 struct * +96 +92 +88 +64 +88 +84 16 words b1-4 %fp t +64 c2 c1 struct * %sp 16 words 14