Skip to main content
Engineering LibreTexts

5.8: Create a utils.asm File

  • Page ID
    28518
  • The final step in adding the subprograms developed in this chapter is to put them in a separate file so that any program you write can use them. To do this, create a file called utils.asm, and copy all of the subprograms (including comments) into this file. Once this is done, this utilities file needs to be correctly documented. The preamble comment (the comment at the start of the file) in the utility file should, at a minimum, contain the following information:

    • The name of the file, so that it can be found.
    • The purpose of the file, in this case what type of subprograms are defined in it.
    • The initial author of the file.
    • A copyright statement
    • A subprogram index so that a user can quickly find the subprograms in the file, and determine if they are of use to the programmer. Ideally this index should be in alphabetic order.
    • A modification history. Every change to this file should be documented here. Even innocuous changes like new comments should be recorded. If currently working programs start to fail for some reason, it makes it much easier to find if this modification history is up to date. Heaven save you from the wrath of a programmer who has spent days trying to track down a bug, only to find that you have made an undocumented change to this file!

    The entire file utils.asm is included here to show what it should look like when completed.

    Program 5-5: File utils.asm
    
    #  File:        utils.asm
    #  Purpose:     To define utilities which will be used in MIPS programs.
    # Author:       Charles Kann
    #
    #  Instructors are granted permission to make copies of this file
    #  for use by # students in their courses. Title to and ownership
    #  of all intellectual property rights
    #  in this file are the exclusive property of
    #  Charles W. Kann, Gettysburg, Pa.
    #
    #  Subprograms Index:
    #  Exit -        Call syscall with a server 10 to exit the program
    #  NewLine -     Print a new line character (\n) to the console
    #  PrintInt -    Print a string with an integer to the console
    #  PrintString - Print a string to the console
    #  PromptInt -   Prompt the user to enter an integer, and return it to the calling program.
    #
    #  Modification History
    #  12/27/2014 - Initial release
    
    # subprogram:    PrintNewLine
    # author:        Charles Kann
    # purpose:       to output a new line to the user console
    # input:         None
    # output:        None
    # side effects:  A new line character is printed to the user's console
    .text
    PrintNewLine:
        li $v0, 4
        la $a0, __PNL_newline
        syscall
        jr $ra
    .data
        __PNL_newline:    .asciiz "\n"
    
    # subprogram:    PrintInt
    # author:        Charles W. Kann
    # purpose:       To print a string to the console
    # input:         $a0 - The address of the string to print.
    #                $a1 - The value of the int to print
    # returns:       None
    # side effects:  The String is printed followed by the integer value.
    .text
    PrintInt:
        # Print string. The string address is already in $a0
        li $v0, 4
        syscall
        
        # Print integer. The integer value is in $a1, and must
        # be first moved to $a0.
        move $a0, $a1
        li $v0, 1
        syscall
        
        #return
        jr $ra
    
    # subprogram:    PromptInt
    #  author:       Charles W. Kann
    #  purpose:      To print the user for an integer input, and to return that input value to the caller.
    #  input:        $a0 - The address of the string to print.
    #  returns:      $v0 - The value the user entered
    #  side effects: The String is printed followed by the integer value.
    .text
    PromptInt:
        # Print the prompt, which is already in $a0
        li $v0, 4
        syscall
        
        # Read the integer value. Note that at the end of the
        # syscall the value is already in $v0, so there is no
        # need to move it anywhere.
        move $a0, $a1
        li $v0, 5
        syscall
        
        #return
        jr $ra
    
    # subprogram:    PrintString
    # author:        Charles W. Kann
    # purpose:       To print a string to the console
    # input:         $a0 - The address of the string to print.
    # returns:       None
    # side effects:  The String is printed to the console.
    .text
    PrintString:
        addi $v0, $zero, 4
        syscall
        jr $ra
    
    # subprogram:    Exit
    # author:        Charles Kann
    # purpose:       to use syscall service 10 to exit a program
    # input:         None
    # output:        None
    # side effects:  The program is exited
    .text
    Exit:
        li $v0, 10
        syscall 
    
    • Was this article helpful?