1 / 21

Subroutines, parameters and the stack

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.

evania
Télécharger la présentation

Subroutines, parameters and the stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subroutines, parameters and the stack Bryan Duggan

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related