CS 3723
 Programming Languages 
   MIPS Initial Examples:  
Input / Output


Hello World Program: First a MIPS program that illustrates input of a double, output of a double, and output of a string.

MIPS Code for "Hello World"
# hello_world.s: prints "Hello World 2.1"
# everything on a line after a "#" is a comment
# mips expects to call a function "main"
        .globl main
main:                      # main is a global label
        move    $s7, $ra   # save return address

# read double
        li      $v0, 7     # magic code to read double
        syscall            # double is now in $f0

# print HW
        li      $v0, 4     # magic code to print string
        la      $a0, HW    # load address of string HW into $a0
        syscall            # HW now printed

# print input double from above
        li      $v0, 3     # magic code for print double
        mov.d   $f12, $f0  # move contents of $f0 int $f12
        syscall            # double now printed

# print NewL
        li      $v0, 4     # magic code to print string
        la      $a0, NewL  # load address of string NewL into $a0
        syscall            # NewL now printed

        move    $ra, $s7   # restore $ra (not needed here)
        jr      $ra        # return to where main was called from
# start of data
        .data   # storage
HW:     .asciiz "Hello World "
NewL:   .asciiz "\n"
% spim hello_world.s
SPIM Version 7.4 ... blah, blah ...
2.1
Hello World 2.10000000000000009
% spim hello_world.s
SPIM Version 7.4 ... blah, blah ...
4.5
Hello World 4.5


Details of Features illustrated:
  • Main Program: [Shown in blue above.] This program is enclosed in the "frame" of a main program. The "system" will look for a global label "main" to branch to. The return address in the system for the return from main is stored in the register $ra. Register $ra is needed for function calls, so it is "saved" in the register $s7 in this program. [However, this program has no functions, so the save and restore below is not needed.

    When this main program is done, register $ra is "restored" from $s7. Then the "jump register" instruction"

      jr   $ra

    returns control to where this was called from.

    The move is called a "pseudo-instruction" because the assembler replaces it with one or more actual MIPS instructions, in fact, with some form of add, such as

      addu   $s7, $ra, $zero     or
      addi   $s7, $ra,     0

  • Data: The .data assembler directive determines storage for the program as described below it. In this case the program only created two strings.

  • I/O using syscall: The syscall statement asks the operating system to take control and do something. It first looks at the register $v0 for a numeric code telling it what to do. [Possible codes are show on page A-44 of Appendix A from Patterson and Hennessy's "Computer Organization" book.] Codes in use here are:

    • 4: Print a string. The system prints the string at the address given in the register $a0. The la "load address" pseudo-instruction loads in the proper address.

    • 3: Print a double. The system prints the double in register $f12.

    • 7: Read a double. The system reads a double and puts it into register $f0, where the program can find it. In the program here notice that the double in $f0 is moved into $f12 in order to print it. That move is a double instruction:

        mov.d   $f12, $f0

  • load immediate: The program repeatedly uses the li "load immediate" instruction, such as

      li   $v0, 3

    which above sticks a constant 3 into register $v0

(Revision date: 2014-10-08. Please use ISO 8601, the International Standard.)