Skip to main content
Library homepage
Engineering LibreTexts

2.4: Program to Prompt and Read an Integer from a User

  • Page ID
  • \( \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 program that will be studied prompts a user to input a number, then reads that number into a register and prints it back to the user.

    Program 2-2: Program to read an integer from the user
    # Program File: Program2-2.asm
    # Author: Charles Kann
    # Program to read an integer number from a user, and # print that number back to the console.
        # Prompt for the integer to enter
        li $v0, 4
        la $a0, prompt
        # Read the integer and save it in $s0
        li $v0, 5
        move $s0, $v0
        # Output the text
        li $v0, 4
        la $a0, output
        # Output the number
        li $v0, 1
        move $a0, $s0
        # Exit the program
        li $v0, 10
    prompt: .asciiz "Please enter an integer: " 
    output: .asciiz "\nYou typed the number "

    2.4.1 Program 2-2 Commentary

    The following commentary covers new information which is of interest in reading Program 2-2.

    • In this program, blocks of code are commented, not each individual statement. This is a better way to comment a program. Each block should be commented as to what it does, and if it is not obvious, how the code works. There should not be a need to comment each line, as a programmer should generally be able to understand the individual instructions.
    • A new operator was introduced in this program, the move operator. The move operator moves the text from one register to another. In this case, the result of the syscall read integer service 5 is moved from register $v0 to a save register $s0.
    • Two new syscall services have been introduced. The first is service 5. Service 5 synchronously waits for the user to enter an integer on the console, and when the integer is typed returns the integer in the return register$v0. This service checks to see that the value entered is an integer value, and raises an exception if it is not.
    • The second new syscall service is service 1. Service 1 prints out the integer value in register $a0. Note that with service 4 that string that is at the address in $a0 (or referenced by $a0) is printed. With the service 1 the value in register $a0 is printed. This difference between a reference and a value is extremely important in all programming languages, and is often a difficult subject to understand even in a HLL.
    • In this program, an escape character "\n" is used in the string named output. This escape character is called the new line character, causes the output from the program to start on the next line. Note that the actually sequence of characters written to the output can vary based on the operation system and environment the program is run on, but the escape character will create the correct output sequence to move to the start of a new line.

    This page titled 2.4: Program to Prompt and Read an Integer from a User 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?