SlideServe
Menu
Préférences
© SlideServe · v2026

MIPS Subroutines

etan avatar
8 diapositives · 377 Views · Sep 13, 2014

MIPS Subroutines. Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return address in $31 Note: If subroutine calls are nested $31 must be saved on the stack and restored by the user.

MIPS Subroutines - slide 1
01 / 8MIPS Subroutines
1 / 17

Télécharger la présentation

MIPS Subroutines
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return address in $31 Note: If subroutine calls are nested $31 must be saved on the stack and restored by the user.

  2. Example Subroutine Call & Return #calling program #subroutine A . subroutineA: . #call subroutineA . jal subroutineA . . #return . jr $31 . .

  3. MIPS Stack Operations All stack (LIFO) operations must be hand coded by using other MIPS instructions $29 points to the top of the Stack memory ($29 is also called the Stack Pointer, SP) The Stack starts at a high memory address and grows to a lower memory address The Stack grows by adding –4 and shrinks by adding +4 to Stack Pointer Putting data on the stack is called a “PUSH” and removing data from the Stack is called a “POP”

  4. MIPS Stack PUSH # $SP = $SP – 4 # adjust stack pointer with addi addi $sp,$sp,-4 # memory($sp) = register value # save data on stack with sw sw $reg,0($sp)

  5. MIPS Stack POP # register value = memory($sp) # get data from stack with lw lw $reg,0($sp) # $SP = $SP + 4 # adjust stack pointer with addi addi $sp,$sp,4

  6. Programming trick when multiple Stack Ops needed Use only one addi instruction! Example: addi $sp,$sp,-8 #PUSH sw $reg1,4($sp) sw $reg2,0($sp) . . . lw $reg1,4($sp) lw $reg2,0($sp) addi $sp,$sp,8 #POP Saves both execution time and memory!

  7. Subroutine & Stack Ops Complex subroutines will need to PUSH several registers and the return address (if it has a jal) upon entry Next, the subroutine performs all of it’s operations. These will destroy or modify the registers that were saved on stack Prior to returning, it will POP saved registers and the return address (net $sp change must be 0! – thiswill cause major problems if it is not correct!!!!) Finally, it returns to the calling program with a jr $31

  8. Example MIPS Subroutines with Stack Operations Factorial & Sort example programs found in Chapter 3 Factorial example program in Appendix A