go to previous page   go to home page   go to next page hear noise

Answer:

The relevant section is filled in, below.

loop:                             # get a line
         la      $a0,line         # argument: address of buffer
         li      $a1,132          # argument: length of buffer
         jal     getline          # get line from user
         
         la      $a0,line         # if "Q"
         jal     testEnd          # return to caller
         beqz    $v0,endloop          

                                  # convert to capitals
         la      $a0,line         # argument: address of buffer
         li      $a1,132          # argument: length of buffer
         jal     convert          # convert

Subroutine getLine

Subroutine getLine reads a line into a buffer. The buffer is in the data section of the caller. The address of the buffer is passed as a parameter.

Flowchart for getline
# getLine -- read in a line of user input
#
# on entry:
#    $a0 -- address of input buffer
#    $a1 -- length of buffer
#
# on exit:
#    new characters in caller's buffer

         .text
         .globl  getLine
getLine: 
         # prolog
         sub     $sp,$sp,4
         sw      $s0,($sp)        # push $s0
         
         sub     $sp,$sp,4        # push the return address
         sw      $ra,($sp)
  
         move    $s0,$a0          # save buffer address          
         la      $a0,prompt       # prompt the user
         li      $v0,4            # service 4
         syscall

         move    $a0,$s0          # restore buffer address
         li      $v0,8            # service 8
         syscall                  # read in a line to the buffer
         
         # epilog
         lw      $s0,($sp)        # Restore $s0
         add     $sp,$sp,4    
         
         lw      $ra,($sp)        # pop return address
         add     $sp,$sp,4         
         jr      $ra              # return to caller 

         .data
prompt:
         .asciiz ">"

Notice how getLine reads data into an input buffer defined externally to itself. The parameters in $a0 and $a1 specify this buffer. It would be a design mistake to have getLine read into its own buffer or to use the symbolic address of a buffer in another subroutine.

The buffer address parameter and the length parameter are similar to the parameters used in many C functions. Study this example to help in your future (or present) understanding of C pointer variables.


QUESTION 26:

Does getLine overwrite the previous line each time it is called?


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