A Sample Program Using Stack-Based Linkage


The following program asks the user for two integers, x and y, and then prints out the maximum value of x+x and 5+y.

Finding the Maximum Value of Two Expressions, x+x and 5+y
          .data
 xprompt: .asciiz  "Enter a value for x --> "
 yprompt: .asciiz  "Enter a value for y --> "
 rprompt: .asciiz  "The maximum expression is: "

          .text
          .globl main 
 main:
          la      $a0, xprompt     # prompt the user
          li      $v0, 4           # service 4 -- print string
          syscall
          li      $v0, 5           # service 5 -- read int
          syscall                  # $v0 = integer
          move    $s0, $v0         # save x
         
          la      $a0, yprompt     # prompt the user
          li      $v0, 4           # service 4 -- print string
          syscall
          li      $v0, 5           # service 5 -- read int
          syscall                  # $v0 = integer
         
                                   # prepare arguments
          move    $a0, $s0         # x         
          move    $a1, $v0         # y
          jal     maxExp           # maximum expression returned in $v0
          move    $s0, $v0         # keep it safe

          la      $a0, rprompt     # output title
          li      $v0, 4           # service 4 -- print string
          syscall

          move    $a0, $s0         # get maximum
          li      $v0, 1           # print int
          syscall
                                                       
          li      $v0, 10          # exit
          syscall


 ## maxExp -- compute the maximum of three expressions
 ##
 ## Input:
 ## $a0 -- a signed integer, x
 ## $a1 -- a signed integer, y
 ##           
 ## Returns: 
 ## $v0 -- the maximum value of x+x and 5+y
 ##
 ## Registers:
 ## $s0 --  x + x
 ## $s1 --  5 + y

          .text
          .globl maxExp
 maxExp:
          # prolog
          sub     $sp, $sp, 4      # push the return address
          sw      $ra, ($sp)
          sub     $sp, $sp, 4      # push $s0
          sw      $s0, ($sp)
          sub     $sp, $sp, 4      # push $s1
          sw      $s1, ($sp)

          # body
          add     $s0, $a0, $a0    # x + x
          add     $s1, $a1, 5      # 5 + y
         
          move    $a0, $s0         # compute the max of x + x
          move    $a1, $s1         #   and 5 + y
          jal     maxInt           # current max in $v0
         
          # epilog
          lw      $s1, ($sp)       # pop $s1 
          add     $sp, $sp, 4                                    
          lw      $s0, ($sp)       # pop $s0 
          add     $sp, $sp, 4                                     
          lw      $ra, ($sp)       # pop return address
          add     $sp, $sp, 4         
          jr      $ra              # return to caller
   
## maxInt -- compute the maximum of two integer arguments ## ## Input: ## $a0 -- a signed integer ## $a1 -- a signed integer ## ## Returns: ## $v0 -- maximum .text .globl maxInt maxInt: # body move $v0, $a0 # max = $a0 # if $a1 > $a0 move $v0, $a1 # max = $a1 endif: # endif # epilog jr $ra # return to caller
 

An Execution Example
   Enter a value for x --> 6
   Enter a value for y --> 5
   The maximum expression is: 12