Subroutines, parameters and the stack Bryan Duggan.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
The University of Adelaide, School of Computer Science
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.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
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
 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.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
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
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedures 2a MIPS code examples Making Use of a Stack.
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.
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 (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.
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:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
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.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Prof. Hsien-Hsin Sean Lee
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
MIPS Procedures.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
MIPS Functions.
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
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Functions.
Presentation transcript:

Subroutines, parameters and the stack Bryan Duggan

What is this lecture about? What is a subroutine? Subroutines in assembler Jal, jr instructions Passing parameters, getting return values Nested subroutines The stack Stack overflow

Introduction He once attempted to incorporate the personality subroutines of several great thinkers from Vulcan, Earth, and other planet's histories. As an unexpected result from this modification, he began to experience 'split personas' and almost killed Kes. The problem was remedied when the subroutines were isolated and purged from his system.

Introduction I'm always amused by the references to programming, which inevitably lead to a discussion of subroutines. Poor Data, whose "grammar subroutines" couldn't be changed to let him say contractions... Except he can use the contractions built into French. Maybe he "thinks in English", so that's special...

Why Subroutines? As our programs grow, we have a need to break them into separate logical units. A subroutine is a set of instructions that performs a specific task for a main routine, requiring direction back to the proper place in the main routine on completion of the task. AKA – Methods, Member functions, functions or procedures

Why Subroutines? Another way to break up an application is by using either functions or subroutines. Functions and subroutines are used to make our programs more readable by breaking large amounts of code into smaller more concise parts. By breaking code into functions and subroutines code can be written once and reused often, thus saving space and debugging time. This is called Modular Programming or Top Down Programming A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no answer is returned.

Subroutines in assembler Need to find the subroutine......and return afterwards Pass parameters to the subroutine......and return results Provide local storage within the procedure

2 main primitives jal address ``jump and link'' jumps to the required address stores address of the following instruction in $ra (register 31) (actually copies PC into $ra) jr $ra return to the instruction following the jal

Simple Example.text.globl __start __start: jal printHello li $v0, 10 syscall# exit..... printHello: li $v0, 4 la $a0, prompt syscall jr $ra.data pi:.word 3 prompt:.asciiz"Hello world! " endl:.asciiz "\n"

Passing parameters & getting return values Parameters can be passed in in $a0 - $a3 Return values go in $v0-$v1

Parameters Example.text.globl __start __start: li $v0, 4 la $a0, enter syscall li $v0, 8 la $a1, 8 la $a0, name syscall jal printHello li $v0, 4 la $a0, endl syscall li $v0, 10 syscall printHello: move $a1, $a0 li $v0, 4 la $a0, prompt syscall li $v0, 4 move $a0, $a1 syscall jr $ra.data name:.asciiz " " enter:.asciiz"Whats your name?\n" prompt:.asciiz"Hello " endl:.asciiz "\n"

Nested subroutines You have a big problem with this approach if you have subroutines that call other subroutines $ra register will get overwritten $a registers will get overwritten $v registers will get overwritten So you need a different approach… Store those registers in memory in a special data structure called the stack

The Stack Most CPUs have PUSH and POP instructions MIPS does not Must use load and store instructions Stack pointer Initialized to address of stack area

Stack Layout $SP points to the top of the stack POP increases $SP PUSH Decreases $SP

To push an item onto the stack Places an item on the stack Sub $sp, $sp, 4 # Make room sw $a0, 0($sp) #

To pop an item off the stack Reads an item from the stack lw $a0, 0($sp) # read item add $sp, $sp, 4 # adjust pointer

Initialising the stack Normally must be done by programmer Reserving memory Initializing the value of the stack pointer SPIM does this for us SPIM initializes stack pointer to high address of stack area

Basic approach push $ra onto stack before a procedure call pop $ra from stack after procedure call use a register to store the address of the current top of stack handle push and pop by doing arithmetic in this register The stack in MIPS grows backwards!

Basic Factorial algorithm int factorial(int x) { if (x == 1) { return 1; } else { return x * factorial(x -1); }

Example - Factorial.text.globl __start __start: li $a0, 3 jal FACT move $a0, $v0 li $v0, 1 syscall li $v0, 10 syscall FACT: beq $a0, 1 RET1 sub $sp,$sp,4 # Push $a0 sw $a0, ($sp) sub $sp,$sp,4 # Push $ra sw $ra, ($sp) sub $a0, $a0, 1 jal FACT lw $ra,0($sp) # Pop $ra addi $sp,$sp,4 lw $a0,($sp) # Pop $a0 addi $sp,$sp,4 mul $v0, $v0, $a0 jr $ra RET1: li $v0, 1 jr $ra

Stack overflow Can be caused by too many subroutines on the stack. Run out of stack space: E.g.: void a() { a(); } int factorial(int x) { if (x == 1) { return 1; } else { return x * factorial(x); }