MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

The University of Adelaide, School of Computer Science
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Subroutines, parameters and the stack Bryan Duggan.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Help! How do procedure calls work?
MIPS Assembly Language Programming
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Logical and Decision Operations
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS Functions.
Presentation transcript:

MAL 3 - Procedures Lecture 13

MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure: save return address procedure call execute the procedure return

la addvar, rtnadd b proc1 rtnadd:... proc1:... b (addvar) jal proc1... proc1:... jr $31 SALMAL Automatically loads the address of the instruction following jal proc1 to $31 (also called $ra ) and branches to proc1 Automatically loads the contents of $31 to PC(program counter)

Dynamic Storage Allocation To handle procedure calls from within a procedure it is no longer adequate to only use a register to store the return addresses. The return addresses can be stored in a dynamically allocated storage implemented as a stack. This stack is called the system stack. The stack pointer can be accessed using $sp or $29. $sp points to the first available space.

$sp bottom small memory addresses large memory addresses 1 word

System Stack Operations add $sp,$sp,-4 sw $8,4($sp) sw $8,0($sp) add $sp,$sp,-4 add $sp,$sp,4 lw $8,0($sp) lw $8,4($sp) add $sp,$sp,4 push pop

Example 13.1 execute: power(2,2) basepower 16 $sp $sp execute: power(2,3)?

Example 13.2 Power(2,3) Power(2,2) Power(2,1) 2*1=2 2*2=4 2*4=8 leaf

Activation Records When a procedure is invoked, a new environment is created. New local variables maybe defined and values of other variables, e.g. parameters, maybe carried over from the previous environment. The previous environment before the call must be saved in a stack and restored when the call is terminated. The new environment will cease to exist when the call is terminated thus memory space where these values are to be saved are allocated dynamically.

The collection of all the information corresponding to a state of a procedure is called an activation record. An activation record is pushed onto the stack like words (as we did in Example 21.2) except it is larger than a word and variable in size. A return address is just one component of an activation record.

Parameter Passing In general, passing parameters in registers alone will not work for programs that contain nested procedure calls. There are only a limited number of registers and potentially many nested calls, thus registers have to be reused. During reuse, values of variables that may belong to a previous environment will get overwritten. A safe way to pass parameters when there are nested calls is to use a stack.

Allocation of space for parameters can be done by a sequence of pushes onto a stack. Each parameter is pushed onto the stack one at a time. The calling program pushes the parameters onto the stack and transfers control to the called procedure. The called procedure then saves the return address by pushing it onto the stack.

Example 13.3 parameter1 parameter2 parameter1 parameter2 return add stack pointer

Example 13.4 add $sp,$sp,-16 sw $8,16($sp) sw $12,12($sp) sw $6,8($sp) jal proc... proc: sw $31,4($sp) Parameters are in $8,$12,$6 respectively. Here the entire activation record is pushed in one group and so the stack pointer is adjusted only once. Parameters may also be pushed one at a time. In this case, the stack pointer is adjusted more than once.

Returning results & parameter types Most programming languages return only a single value from a procedure call. A common practice is to save the return value in a register. In a pass-by-reference, an address is passed to the called procedure. The called procedure can then modify the value of the variable. In a pass-by-value, only the value of the variable is passed as a parameter, thus no modification can be done to the original values.

Saving Registers Registers can be reused by the called procedures provided that the state of the environment before procedure execution is saved. These information are again saved using a stack and restored to original values when the called procedure terminates. When is saving performed? Called procedure does it Calling procedure saves them before the call

The called procedure can push onto the stack the values of the registers it will be using. This clears enough registers to be used by the called procedure. Restoration is performed just before it returns. The calling procedure can also push onto the stack the values of registers it does not want changed by the called procedure. This can be done as part of the setup for a procedure call.

Example 13.5 jal proc... proc: sw $ra,0($sp) add $sp,$sp,-4 add $sp,$sp,-12 sw $t0,12($sp) sw $t1,8($sp) sw $t2,4($sp) …#proc code here lw $t2,4($sp) lw $t1,8($sp) lw $t0,12($sp) add $sp,$sp,12 add $sp,$sp,4 lw $ra,0($sp) jr $ra add $sp,$sp,-12 sw $t0,12($sp) sw $t1,8($sp) sw $t2,4($sp) jal proc lw $t2,4($sp) lw $t1,8($sp) lw $t0,12($sp) add $sp,$sp,12... proc: sw $ra,0($sp) add $sp,$sp,-4 …#proc code here add $sp,$sp,4 lw $ra,0($sp) jr $ra called procedure setup calling procedure setup

The return address can be saved like any other register but it must saved after the procedure call. This is so because only after a procedure call that $31 will contain the return address. In MIPS RISC architecture the first four parameters are passed via $4,$5,$6,$7. Additional parameters are passed via the stack. If parameters are passed via $4 and $5, the procedure must first save them before calling another procedure.

Some conventions $1 - reserved by the assembler $2-$3- expression evaluation $4-$7- first four parameters $8-$15- temporaries $16-$23- saved across procedure calls $26-$27- reserved for operating system $f20-$f30 - saved across procedure calls For a more extensive example, check pp of the text.