Download presentation
Presentation is loading. Please wait.
Published byΤερψιχόρη Παπακωνσταντίνου Modified over 5 years ago
1
MIPS R3000 Subroutine Calls and Stack
CS 286 – Project #2 MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Summer, 2019 Dr. Hiroshi Fujinoki Subcall/001
2
CS 286 – Project #2 Subroutine Calls in MIPS R3000
This PPT presentations describe how to write an assembly program for MIPS R3000 processor that uses subroutine calls The concept of “Stack Frame” in MIPS R3000 processor Implementation of recursive subroutine calls in MIPS R3000 assembly program Subcall/002
3
CS 286 – Project #2 Subroutine Calls in MIPS R3000
the first address in your memory the last address High Address Low Address main: In this presentation, it is assumed that the low address appears at the top Each program executes from the top to the bottom jr $31 Subcall/003
4
right after the “jar” instruction Go back to the instruction
CS 286 – Project #2 Subroutine Calls in MIPS R3000 main: In MIPS R3000 assembly language: jal xyz (a) Calling a subroutine Jump to “xyz” “jal” instruction (b) Returning to a calling routine right after the “jar” instruction Go back to the instruction jr $31 “jr $ra” instruction xyz: jr $ra Subcall/004
5
CS 286 – Project #2 Subroutine Calls in MIPS R3000
“jr $ra” instruction makes sure that the program execution flow goes back to “the right return point” main: jr $31 xyz: jr $ra jal xyz jal xyz Subcall/005
6
CS 286 – Project #2 Subroutine Calls in MIPS R3000
a danger in using subroutine calls in MIPS assembly programming Destroying register contents during a subroutine call S0 register as a loop counter main: jr $31 jal xyz li $s0, 10 LOOP: sub $s0, $s0, 1 jne $s0, $zero, LOOP xyz: jr $ra li $s0, 200 add $s0, $s0, 5 Processor We have one processor slt $s0, $t3, $zero Subcall/006
7
CS 286 – Project #2 Subroutine Calls in MIPS R3000
“recursive subroutine call” main: xyz: jr $ra xyz: jr $ra xyz: jr $ra jal xyz li $s0, 10 add $s0, ... li $s0, 10 add $s0, ... li $s0, 10 add $s0, ... jal xyz jal xyz jal xyz jr $31 Why are recursive subroutine calls problematic in using processor registers? It’s guaranteed that the same registers will be used in every recursive call ... What should we do this to solve this problem? Subcall/007
8
CS 286 – Project #2 Concept of stack (1) Stack Area main: li $s0, 10
LOOP: sub $s0, $s0, 1 jne $s0, $zero, LOOP xyz: jr $ra jal xyz reserve my stack space save all registers I modify $s0 li $s0, 200 jal xyz add $s0, $s0, 5 slt $s0, $t3, $zero restore all registers I modified release my stack space jr $31 xyz: Stack Area Subcall/008
9
“recursive subroutine call”
CS 286 – Project #2 Concept of stack (2) “recursive subroutine call” main: xyz: jr $ra jal xyz li $s0, 10 add $s0, ... xyz: jr $ra jal xyz li $s0, 10 add $s0, ... save $s0 restore $s0 xyz: jr $ra jal xyz li $s0, 10 add $s0, ... save $s0 restore $s0 jal xyz save $s0 restore $s0 jal xyz $s0 jr $31 Stack Each called instance of a subroutine reserves its stack space and saves the register it modifies $s0 $s0 $s0 Subcall/009
10
CS 286 – Project #2 Subroutine Calls in MIPS R3000
“jr $ra” jumps to the address in $ra register Resume to “main” main: xyz: jr $ra xyz: jr $ra xyz: jr $ra jal xyz jal xyz $ra (next instruction) jal xyz jal xyz $ra next inst. jr $31 Stack Load the address of the next instruction to “$ra” register Jump (set PC register) to the first instruction in the called subroutine Subcall/010
11
for a recursive structure
CS 286 – Project #2 Subroutine Calls in MIPS R3000 main: This structure is a necessary condition for a recursive structure xyz: xyz: jr $ra xyz: jr $ra jal xyz save $s0 save $s0 save $s0 save $ra save $ra save $ra $ra (next instruction) jal xyz jal xyz $ra next inst. $ra next inst. restore $ra restore $ra restore $ra restore $s0 restore $s0 restore $s0 jr $31 jr $ra Stack $ra $s0 $ra $s0 $ra $s0 Subcall/011
12
CS 286 – Project #2 Subroutine Calls in MIPS R3000 Stack main:
xyz: jr $ra xyz: jr $ra xyz: jr $ra jal xyz jal xyz jal xyz jal xyz jr $31 Stack Stack Frame for “xyz(3)” Stack Frame for “xyz(2)” Stack Frame for “xyz(1)” Stack Frame for “main” Subcall/012
13
CS 286 – Project #2 Example (3) Set up the stack frame
xyz: # create stack frame subu $sp, $sp, 32 # save $ra register sw $ra, 0($sp) # save other GPR’s sw $s0, 4($sp) sw $t0, 8($sp) Set up the stack frame and save the registers Subcall/013
14
CS 286 – Project #2 Example (4) Restore the registers
xyz: # restore registers lw $s0, 4($sp) lw $t0, 8($sp) # restore $ra for the caller lw $ra, 0($sp) # restore the caller's stack pointer addu $sp, $sp, 32 Restore the registers and release the stack frame jr $ra Subcall/014
15
CS 286 – Project #2 Subroutine Calls in MIPS R3000 $sp = $sp-32
High Address Low Address $sp = $sp-32 $fp = $sp+28 $sp -32 +28 $fp 8 registers $sp Stack Frame $fp 4 bytes Subcall/015
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.