Skip to main content
Engineering LibreTexts

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

  • Page ID
    27095
  • 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.
    .text
    main:
        # Prompt for the integer to enter
        li $v0, 4
        la $a0, prompt
        syscall
        
        # Read the integer and save it in $s0
        li $v0, 5
        syscall
        move $s0, $v0
        
        # Output the text
        li $v0, 4
        la $a0, output
        syscall
    
        # Output the number
        li $v0, 1
        move $a0, $s0
        syscall
    
        # Exit the program
        li $v0, 10
        syscall
    .data
    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.
    • Was this article helpful?