Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail:

Similar presentations


Presentation on theme: "MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail:"— Presentation transcript:

1 MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 312 Computer Organization and Architecture Subcall/001

2 CS 312 Computer Organization and Architecture Subcall/002 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

3 CS 312 Computer Organization and Architecture Subcall/003 Subroutine Calls in MIPS R3000 main: jr $31 High Address Low Address the first address in your memory the last address in your memory In this presentation, it is assumed that the low address appears at the top Each program executes from the top to the bottom

4 CS 312 Computer Organization and Architecture Subcall/004 Subroutine Calls in MIPS R3000 main: jr $31 xyz: jal xyz jr $ra      Go back to the instruction right after the “jar” instruction In MIPS R3000 assembly language: (a) Calling a subroutine (b) Returning to a calling routine “jal” instruction “jr $ra” instruction Jump to “xyz”

5 CS 312 Computer Organization and Architecture Subcall/005 Subroutine Calls in MIPS R3000 main: jr $31 jal xyz           xyz: jr $ra “jr $ra” instruction makes sure that the program execution flow goes back to “the right return point”

6 xyz: jr $ra main: jr $31 jal xyz           CS 312 Computer Organization and Architecture Subcall/006 Subroutine Calls in MIPS R3000 li $s0, 10 LOOP: sub $s0, $s0, 1 jne $s0, $zero, LOOP li $s0, 200 Processor We have one processor add $s0, $s0, 5      slt $s0, $t3, $zero       a danger in using subroutine calls in MIPS assembly programming Destroying register contents during a subroutine call S0 register as a loop counter

7 CS 312 Computer Organization and Architecture Subcall/007 Subroutine Calls in MIPS R3000 main: jr $31 jal xyz      xyz: jr $ra jal xyz xyz: jr $ra jal xyz xyz: jr $ra jal xyz    “recursive subroutine call” Why are recursive subroutine calls problematic in using processor registers? li $s0, 10 add $s0,... li $s0, 10 add $s0,... li $s0, 10 add $s0,... It’s guaranteed that the same registers will be used in every recursive call... What should we do this to solve this problem?

8 CS 312 Computer Organization and Architecture Subcall/008 Concept of stack (1) 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      slt $s0, $t3, $zero      xyz: Stack Area reserve my stack space save all registers I modify restore all registers I modified release my stack space $s0

9 CS 312 Computer Organization and Architecture Subcall/009 main: jr $31 jal xyz           “recursive subroutine call” xyz: jr $ra jal xyz li $s0, 10 add $s0,... $s0 save $s0 restore $s0 $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 Stack Each called instance of a subroutine reserves its stack space and saves the register it modifies Concept of stack (2)

10 CS 312 Computer Organization and Architecture main: jr $31 xyz: jr $ra jal xyz xyz: jr $ra Stack jal xyz (next instruction) $ra  Load the address of the next instruction to “$ra” register  Jump (set PC register) to the first instruction in the called subroutine  “jr $ra” jumps to the address in $ra register next inst. $ra xyz: jr $ra jal xyz Subroutine Calls in MIPS R3000 Subcall/010 Resume to “main”    jal xyz

11 xyz: jr $ra CS 312 Computer Organization and Architecture Subroutine Calls in MIPS R3000 main: jr $31 xyz: save $s0 restore $s0 jr $ra jal xyz xyz: jr $ra Stack jal xyz (next instruction) $ra next inst. $ra save $s0 jal xyz save $ra $s0 $ra save $ra $s0 $ra next inst. $ra restore $ra $s0 $ra restore $ra Subcall/011 save $s0 restore $s0 save $ra

12 CS 312 Computer Organization and Architecture Subcall/014 main: jr $31 Stack jal xyz xyz: jr $ra Stack Frame for “main”Stack Frame for “xyz(1)” xyz: jr $ra Stack Frame for “xyz(2)” xyz: jr $ra Stack Frame for “xyz(3)” Subroutine Calls in MIPS R3000 jal xyz    jal xyz

13 CS 312 Computer Organization and Architecture Subcall/015 Subroutine Calls in MIPS R3000 4 bytes High Address Low Address 8 registers Stack Frame $fp $sp = $sp-32 $sp $fp $fp = $sp+28 $sp -32 +28

14 CS 312 Computer Organization and Architecture Subcall/018 Example (3)   xyz: # create stack frame subu$sp, $sp, 32 # save ($fp, $ra) registers sw$ra, 8($sp) sw$fp, 4($sp) # set up the new frame pointer addu$fp, $sp, 28 # save other GPR’s sw$s0, 28($sp) Set up the stack frame and save the registers

15 CS 312 Computer Organization and Architecture Subcall/019 Example (4) xyz:   jr$ra # restore registers lw$s0, 28($sp) # restore SP, FP, $ra for the caller lw$ra, 8($sp) lw$fp, 4($sp) # restore the caller's stack pointer addu$sp, $sp, 32 Restore the registers and release the stack frame


Download ppt "MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail:"

Similar presentations


Ads by Google