subroutines, parameters and the stack bryan duggan

21
Subroutines, parameters and the stack Bryan Duggan

Upload: ashlynn-obrien

Post on 05-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Subroutines, parameters and the stack Bryan Duggan

Subroutines, parameters and the stack

Bryan Duggan

Page 2: Subroutines, parameters and the stack Bryan Duggan

What is this lecture about?

What is a subroutine?Subroutines in assemblerJal, jr instructionsPassing parameters, getting return valuesNested subroutinesThe stackStack overflow

Page 3: Subroutines, parameters and the stack Bryan Duggan

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.

Page 4: Subroutines, parameters and the stack Bryan Duggan

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...

Page 5: Subroutines, parameters and the stack Bryan Duggan

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

Page 6: Subroutines, parameters and the stack Bryan Duggan

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 ProgrammingA 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.

Page 7: Subroutines, parameters and the stack Bryan Duggan

Subroutines in assembler

Need to find the subroutine......and return afterwardsPass parameters to the subroutine......and return resultsProvide local storage within the procedure

Page 8: Subroutines, parameters and the stack Bryan Duggan

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

Page 9: Subroutines, parameters and the stack Bryan Duggan

Simple Example.text.globl __start

__start:jal printHello

li $v0, 10syscall # exit .....

printHello:li $v0, 4la $a0, promptsyscalljr $ra

.datapi: .word 3prompt: .asciiz "Hello world! "endl: .asciiz "\n"

Page 10: Subroutines, parameters and the stack Bryan Duggan

Passing parameters & getting return values

Parameters can be passed in in $a0 - $a3Return values go in $v0-$v1

Page 11: Subroutines, parameters and the stack Bryan Duggan

Parameters Example.text.globl __start

__start:li $v0, 4la $a0, entersyscall

li $v0, 8la $a1, 8la $a0, namesyscall

jal printHello

li $v0, 4la $a0, endlsyscall

li $v0, 10syscall

printHello:move $a1, $a0li $v0, 4la $a0, promptsyscall

li $v0, 4move $a0, $a1syscall

jr $ra

.dataname: .asciiz " "enter: .asciiz "Whats your name?\n"prompt: .asciiz "Hello "endl: .asciiz "\n"

Page 12: Subroutines, parameters and the stack Bryan Duggan

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 overwrittenSo you need a different approach…Store those registers in memory in a special data structure called the stack

Page 13: Subroutines, parameters and the stack Bryan Duggan

The Stack

Most CPUs have PUSH and POP instructionsMIPS does not Must use load and store instructions Stack pointer Initialized to address of

stack area

Page 14: Subroutines, parameters and the stack Bryan Duggan

Stack Layout

$SP points to the top of the stack

POP increases $SP

PUSH Decreases $SP

Page 15: Subroutines, parameters and the stack Bryan Duggan

To push an item onto the stack

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

Page 16: Subroutines, parameters and the stack Bryan Duggan

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

Page 17: Subroutines, parameters and the stack Bryan Duggan

Initialising the stack

Normally must be done by programmerReserving memoryInitializing the value of the stack pointerSPIM does this for usSPIM initializes stack pointer to high address of stack area

Page 18: Subroutines, parameters and the stack Bryan Duggan

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 registerThe stack in MIPS grows backwards!

Page 19: Subroutines, parameters and the stack Bryan Duggan

Basic Factorial algorithm

int factorial(int x) {if (x == 1) {

return 1;}else {

return x * factorial(x -1);}

Page 20: Subroutines, parameters and the stack Bryan Duggan

Example - Factorial.text.globl __start__start:

li $a0, 3jal FACT

move $a0, $v0li $v0, 1syscall

li $v0, 10syscall

FACT:beq $a0, 1 RET1

sub $sp,$sp,4 # Push $a0sw $a0, ($sp)

sub $sp,$sp,4 # Push $rasw $ra, ($sp)

sub $a0, $a0, 1

jal FACT

lw $ra,0($sp) # Pop $raaddi $sp,$sp,4 lw $a0,($sp) # Pop $a0addi $sp,$sp,4

mul $v0, $v0, $a0jr $ra

RET1:li $v0, 1jr $ra

Page 21: Subroutines, parameters and the stack Bryan Duggan

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);}