Skip to main content
Engineering LibreTexts

5.5: Input Parameter with PrintString Subprogram

  • Page ID
    27124
  • The next subprogram to be implemented, the PrintString subprogram abstracts the ability to print a string. This subprogram will require an input parameter to be passed into the program, the address of the string to print. Remembering the register conventions from chapter 2, the registers $a0..$a4 are used to pass input parameters into a program, so the register $a0 will be used for the input parameter to this subprogram. The subprogram will then load a 4 into $v0, invoke syscall, and return.

    The program from Chapter 5.4 is modified to use the PrintString program. The changes to the program are highlighted in yellow. The only two new concepts in this program are the ability to pass parameters into the program, and that the documentation at the beginning of the program should include the input parameter. So no commentary will be provided after this program.

    Program 5-3: Input parameter with the PrintString subprogram
    
    # File:        Program5-2.asm
    # Author:      Charles Kann
    # Purpose:     To illustrate implementing and calling a subprogram named PrintNewLine.
    .text
    main:
        # read an input value from the user
        la $a0, prompt
        jal PrintString
        li $v0, 5
        syscall
        move $s0, $v0
        
        # print the value back to the user
        jal PrintNewLine
        la $a0, result
        jal PrintString
        li $v0, 1
        move $a0, $s0
        syscall
        
        # call the Exit subprogram to exit
        jal Exit
    
    .data
        prompt: .asciiz "Please enter an integer: "
        result: .asciiz "You entered: "
    
    # 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:    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?