Skip to main content
Engineering LibreTexts

5.5: Input Parameter with PrintString Subprogram

  • Page ID
    27124
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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
    

    This page titled 5.5: Input Parameter with PrintString Subprogram is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III.

    • Was this article helpful?