go to previous page   go to home page   go to next page highlighting

Answer:

The return value (the maximum) is already in $v0. So all the epilog needs to do is return to the caller.


Epilog

It is OK if the return value is already in $v0 when the epilog is reached. Here is the complete subroutine:

## maxInt -- compute the maximum of two integer arguments
##
## Input:
## $a0 -- a signed integer
## $a1 -- a signed integer
##
## Returns:
## $v0 -- maximum

         .text
         .globl maxInt

maxInt:
          # save return address
          sub    $sp,$sp,4        # push $ra
          sw     $ra,($sp)

          # body
          move   $v0,$a0          # max = $a0
          bgt    $a0,$a1,endif    # if $a1 > $a0  
          nop
          move   $v0,$a1          #    max = $a1
endif:                            # endif 
          # epilog  
          lw     $ra,($sp)        # pop return address
          add    $sp,$sp,4
          jr     $ra              # return to caller
          nop

This subroutine could be put in its own file, perhaps maxInt.asm, and separately assembled. Later on it could be used with programs we don't even know about, as long as they follow the Stack-based Linkage Convention.

With QTSPIM, main and each subroutine can be placed in a separate file and separately loaded. Use the "File" menu. Use the "Reinitialize and Load File" menu choice for main, and then use "Load File" with each subroutine. Check that the QTSPIM program counter starts points at main. (Look in Simulator/Run Parameters.)

Here is a main that can be used with this subroutine. It has a prolog and epilog that follow the linkage convention.

## maxIntMain
##
## Call maxInt to find the maximum of two hard-coded integers
##
         .text
         .globl main

main:
          # prolog
          # (Nothing needed here.)
          
          # body
          li     $a0,12           # A = 12
          li     $a1,34           # B = 34  
          jal    maxInt           # $v0 = max
          nop
          
          move   $s0,$v0          # $s0 = max
          la     $a0,capt         # print caption
          li     $v0,4
          syscall 
          move   $a0,$s0
          li     $v0,1            # print max
          syscall
          
          # return to QTSPIM
          li     $v0,10
          syscall  
          
          .data
capt:     .asciiz "Maximum: "

Recall that with the current verstion of qtSPIM the return to the system should be done using service 10 of the trap handler.


QUESTION 14:

Must a nop follow the jal?


go to previous page   go to home page   go to next page