Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
SPARC Architecture & Assembly Language
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition,
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
1 CSC 3210 Computer Organization and Programming Chapter 7 SUBROUTINES D.M. Rasanjalee Himali.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Chapter 8 :: Subroutines and Control Abstraction
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Storage Allocation Mechanisms
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
COMPILERS Activation Records
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
by Richard P. Paul, 2nd edition, 2000.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Functions Functions and Parameters

History A function call needs to save the registers in use The called function will use the registers The registers need to be restored when the called function is completed. This requires three things: – Who saves the registers – Who restores the registers – Where are they saved

Who Saves/Restores Who saves – The calling function – The called function Who restores – The calling function – The called function Any combination of the above

Where A location in memory – In registers – Pointed to by a register – In the caller’s frame – In the called frame – On the stack – Register sets Sparc Architecture uses the frame and register sets

Sparc Register File In the Sparc architecture there are a minimum of 128 registers and 8 global registers These are grouped into 8 global registers and 24 mapped programmer registers The save instruction changes the register mapping to a new set The restore instruction restores the old set. The CWP and WIM are two system registers pointing to the current window of registers and the last free register set.

Sparc Register File Sixteen General Registers In Local Out CWP -> WIM ->

Sparc Register File Sixteen General Registers In Local Out In Local Out CWP -> WIM ->

Sparc Register File Sixteen General Registers In Local Out In Local Out In Local Out CWP -> WIM ->

Window Overflow Sixteen General Registers In Local Out In Local Out CWP -> WIM ->

Window Overflow In Local Out In Local Out CWP -> WIM -> In Local CWP -> WIM -> Save to Frame where this %sp points Out In Local Out

Window Overflow In Local CWP -> WIM ->CWP -> WIM -> Save to Frame In Local Out In Local Out

Function Registers Each function gets its own registers The global registers (one copy for all) Its own copy of the other registers – The I registers – The L registers – The O registers

The frame pointer %i6 The stack pointer %o6 R31 R30 R29 R28 R27 R26 R25 R24 R23 R22 R21 R20 R19 R18 R17 R16 R15 R14 R13 R12 R11 R10 R9 R8 I registers L registers O registers

A Function Call A function call creates a new frame A called function gets access to the %g registers A called function’s I registers are the calling function’s O registers The called function gets its own L registers and O registers. These are called a register set.

<- i6 = fp I registers L registers sp =o6 -> O registers I registers L registers sp =o6 -> O registers Register Set Figure 7.1

Save Instruction save %sp, some_value, %sp The old sp, %o6 becomes %i6, %fp in the new register set after the save The save instruction is also an ADD instruction. It adds some_value to the current %sp, putting the result into the new %sp, leaving the old %sp unchanged.

Restore The restore instruction restores the register window set. If underflow occurs (the opposite of overflow), the system restores the registers from the frame where the %sp points

Functions vs Subroutines A subroutine / procedure can be viewed as a function that returns void (C/C++/Java) procedure print_list(Listtype L); (PASCAL) void print_list( Listtype L); A function can be viewed as a procedure that returns a value (ALGOL) integer procedure factorial(integer N);

function calls Put parameters they should be callfunction_name If function returns a value, look for return value where it should be

The call statement The call statement is equivalent to a jump long instruction callfun_name is equivalent setfun_name, %o0 jmpl%o0, %o7 The %pc at the call is saved in %o7 (= %i7 of the called function )

The ret Statement The ret statement is also a jmpl instruction ret restore is equivalent to jmpl%i7 + 8, %g0// why %i7 + 8 restore

functions.data data stuff.text.globalfun_name fun_name:save%sp, value, %sp get parameters // code for function … put answer ret restore

Where, and How, are the Arguments After the call statement On the stack In registers In memory By value By reference (address) By address of values By address of addresses

After the call (1) calladdem! Addr = %o7 nop! Addr = %o7 + 4.word3! Addr = %o7 + 8.word4! Addr = %o ! Return here Addr = %o7 + 16

After the call (2).text.global addem addem:save%sp, -64, %sp ld[%i7 + 8], %i0 ld[%i7 + 12], %i1 add%i0, %i1, %i0 jmpl%i7 + 16, %g0 restore

Remarks Very efficient Non-recursive Cannot compute the parameters Modern computers will not allow a program to store into the code section Cannot have a variable number of parameters

Arguments on Stack This technique is very flexible Allows recursion Number of parameters can vary for the same function But requires memory access to load and store parameters’ values and results

Sparc Solution First six arguments are placed into the O registers. These O registers become the called functions I registers. No memory references are necessary for these parameters.

But … More than six arguments requires the use of the stack Each stack argument occupies one word, even byte arguments Byte arguments must be moved into word quantities before stored in the stack. First argument is at %sp + 68 The frame size must allow for all the parameters Frame size = 16 + locals + parameters + 68

Example /**************************** * File: par_reg.m * main reads two numbers x * and y, and calls the function * f(x,y,&result) to compute *x*y + 2 and return the value * in result. *********************************

Exercise Write a program to read in two integers a and b, compute a*a + b*b, and print out the sum. The program should loop until two integers are not read. Use a separate functions for each part. Equivalent C code is next. Pass parameters using registers.

Exercise (con’t) void print_prompt(); int read(int & a, int & b); void write(int a, int b); while(print_prompt(), read(a, b) != 2) { s = fun(a, b); write(a, b, s); }

Stack Parameters Parameters may be put on the stack Which frame: caller or called? – Many machines it is the called – For the Sparc it is the caller Therefore, the caller refers to them using the stack pointer The called function uses the frame pointer

Example 2 /*************************************** * File: par_st.m * Purpose: To demonstrate * parameter passing using * the system stack. * Computes the same as par_reg.m ******************************************/

Exercise Repeat the previous exercise, but pass the parameters using the stack.