1 / 15

MIPS R3000 Subroutine Calls and Stack

MIPS R3000 Subroutine Calls and Stack. CS 286 – Project #2. Department of Computer Science Southern Illinois University Edwardsville Fall, 2019 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu. Subcall/001. Subroutine Calls in MIPS R3000. CS 286 – Project #2.

mshelley
Download Presentation

MIPS R3000 Subroutine Calls and 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. MIPS R3000 Subroutine Calls and Stack CS 286 – Project #2 Department of Computer Science Southern Illinois University Edwardsville Fall, 2019 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu Subcall/001

  2. Subroutine Calls in MIPS R3000 CS 286 – Project #2 • 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. Subroutine Calls in MIPS R3000 main: the first address in your memory Low Address High Address the last address in your memory CS 286 – Project #2 • 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. Subroutine Calls in MIPS R3000 main:    “jal” instruction Jump to “xyz” “jr $ra” instruction xyz: Go back to the instruction right after the “jar” instruction CS 286 – Project #2 • In MIPS R3000 assembly language: jal xyz (a) Calling a subroutine (b) Returning to a calling routine jr $31 jr $ra Subcall/004

  5. Subroutine Calls in MIPS R3000 main:    jal xyz xyz: jr $31 jr $ra CS 286 – Project #2 • “jr $ra” instruction makes sure that the program execution flow goes back to “the right return point” jal xyz    Subcall/005

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

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

  8. Concept of stack (1) li $s0, 10 xyz: LOOP:    add $s0, $s0, 5    slt $s0, $t3, $zero • restore all registers I modified • release my stack space jr $ra sub $s0, $s0, 1 Stack Area jne $s0, $zero, LOOP $s0 CS 286 – Project #2 main: jal xyz • reserve my stack space • save all registers I modify    li $s0, 200 jal xyz    jr $31 xyz: Subcall/008

  9. Concept of stack (2) main: “recursive subroutine call” save $s0 save $s0 save $s0 li $s0, 10 li $s0, 10 li $s0, 10 jal xyz jal xyz jal xyz add $s0, ... add $s0, ... add $s0, ...    xyz: xyz: xyz: restore $s0 restore $s0 restore $s0 jr $ra jr $ra jr $ra Stack Each called instance of a subroutine reserves its stack space and saves the register it modifies $s0 $s0 $s0 $s0 CS 286 – Project #2 jal xyz    jal xyz    jr $31 Subcall/009

  10. Subroutine Calls in MIPS R3000  “jr $ra” jumps to the address in $ra register $ra $ra Resume to “main” main: xyz: xyz: jal xyz (next instruction) jal xyz    next inst. xyz: jr $ra jr $ra jr $ra Stack CS 286 – Project #2 jal xyz jal xyz jr $31  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. Subroutine Calls in MIPS R3000 $ra $ra $ra main: xyz: xyz: jal xyz (next instruction) next inst. next inst. xyz: jr $ra jr $ra Stack $s0 $s0 $s0 $ra $ra $ra CS 286 – Project #2 This structure is a necessary condition for a recursive structure save $s0 save $s0 save $s0 save $ra save $ra save $ra jal xyz jal xyz restore $ra restore $ra restore $ra restore $s0 restore $s0 restore $s0 jr $31 jr $ra Subcall/011

  12. Subroutine Calls in MIPS R3000 main: xyz: xyz: jal xyz jal xyz    xyz: jr $ra jr $ra jr $ra Stack Stack Frame for “main” Stack Frame for “xyz(1)” Stack Frame for “xyz(2)” Stack Frame for “xyz(3)” CS 286 – Project #2 jal xyz jal xyz jr $31 Subcall/012

  13. Example (3) Set up the stack frame and save the registers CS 286 – Project #2 xyz: # create stack frame subu $sp, $sp, 32 # save $raregister sw $ra, 0($sp) # save other GPR’s sw $s0, 4($sp) sw $t0, 8($sp)        Subcall/013

  14. Example (4) Restore the registers and release the stack frame CS 286 – Project #2 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 jr $ra Subcall/014

  15. Subroutine Calls in MIPS R3000 Low Address +28 -32 $fp $sp $fp $sp Stack Frame 8 registers High Address 4 bytes CS 286 – Project #2 $sp = $sp-32 $fp = $sp+28 Subcall/015

More Related