Skip to main content
Engineering LibreTexts

5.2: PrintNewLine Subprogram

  • Page ID
    27121
  • \( \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 will print a new line character. This subprogram will allow the program to stop inserting the control character "\n" into their programs. It will be used here to illustrate how to return from a subprogram.

    Program 5-2: Implementing and calling the PrintNewLine 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
        li $v0, 4
        la $a0, prompt
        syscall
        li $v0, 5
        syscall
        move $s0, $v0
        
        # print the value back to the user
        jal PrintNewLine
        li $v0, 4
        la $a0, result
        syscall
        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:    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
    

    5.2. 1 Commentary on Exit subprogram

    1. This program now has three .text segments, but the reason for this is becomes obvious when the code is examined. The PrintNewLine subprogram requires a value to be stored in the .data segment, the newline character. The .text segments are needed to inform the assembler that program instructions are again contained in the code. This is emphasized here as it points out a good rule to follow when writing subprograms. Always begin the subprogram with a .text statement. If the assembler already thinks it is in a .text segment, there is no effect, but the subprogram is protected from the case where the assembler thinks it is assembling a .data segment when reaching the subprogram. In real life files can change often, and omitting a simple .text of .data segment when it should be present can lead to myriads of unnecessary problems.
    2. The PrintNewLine subprogram shows how to return from a subprogram using the instruction "jr $ra". The next section of this chapter will explain how this actually works. For now, this can be thought of as a return statement.
    3. A label was needed in the PrintNewLine subprogram to contain the address of the newline variable. In MIPS assembly, you cannot use labels with the same name15. This subprogram will eventually become part of a utility package which will be used by a number of programs. Care must be taken to make sure that any label used will conflict with a label in a program. So the convention of putting a double underscore (__) before the variable, a string representing the subprogram it is in (PNL), and another underscore before the variable name (newline) is used. It is hoped that this will solve nearly all name conflicts.
    4. The $a0 and $v0 registers have been changed, but this is not listed as a side effect of the program. Part of the definition of the registers says that the save registers ($s0..$s9) must contain the same value when the subprogram returns as they did when the subprogram was called. For these simple subprogram, that means that these registers cannot be used. All other registers have no guarantee of the value after the subprogram is called. A programmer cannot rely on the values of $a0 or $v0 once a program is called. So while changing them is a side effect of calling the function, it does not have to be listed as it is implied in the execution of the method.

    15 More accurately, in MIPS you cannot use labels with the same name unless the labels are resolved to addresses so that they do not conflict. If separate assembly and linking are performed, any none global symbols can be used in any file, so long as it is in that file once. This text does not handle separate compilation and linking, so the rule that label names cannot be repeated is correct for all programs in this text.


    This page titled 5.2: PrintNewLine 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?