rss
twitter
facebook

Home

This sample code illustrates basic I/O

# NAME:
# TA's NAME:
# DESCRIPTION:

#
#----------------------------------------------#
#
# This sample code illustrates basic I/O
# USING SYSCALLS to the console, along with a
# small amount of array manipulation.
#
#----------------------------------------------#

# This is the data segment

        .data       
string0:    .asciiz     "This is a text string\n"
item0:    .word        99
array0:    .word        11 22 33 44 55 66 77 88 99 #array of constants

#----------------------------------------------#

# This is the the code ("text") segment
    .text
        .globl main   

main:           

# EXAMPLE I:  Print a string on the console.  The method is to load the
# address of the string into $a0 and then use a syscall to print the string.

    la $a0, string0        # Load the base address of the string into $a0
    li $v0, 4        # Set $v0 to 4, this tells syscall to print the
                # text string specified by $a0
    syscall            # Now print the text string to the console

# EXAMPLE II: Load an integer from the data segment and print it.

    lw $a0, item0        # Load the value of item0 into $a0
    li $v0, 1        # Set $v0 to 1, this tells syscall to print the
                # integer specified by $a0   
    syscall            # Now print the integer

# EXAMPLE III: Read an integer from the console.

    li $v0, 5        # Set $v0 to 5, this tells syscall to read an
                # integer from the console
    syscall            # Now read the integer.
                #
                # The integer is now in $v0, and we can use
                # it as we see fit.

# EXAMPLE IV: Print an integer (the one that is the 4th element of an array)

        li  $t0, 3        # Set t0 to 3, this is the index of the
                # element we are fetching from the array
        li  $t1, 4        # Set t1 to 4, this is the size in bytes of an
                # element (this is an integer array)
        mul $t2, $t1, $t0    # t2 = t1 * t0, so t2 is the BYTE offset of the
                  # element we are fetching from the array.
    lw  $a0, array0($t2)    # Load the element of the array; note that
                # the first element in the array has an offset
                # of zero
    li  $v0, 1        # Set $v0 to 1, this tells syscall to print the
                # integer specified by $a0
    syscall            # Now print the integer

# EXAMPLE V:  Halt the program.

    li $v0, 10        # set $v0 to 10, this tells syscall to end
                # execution of this program
    syscall
   

0 comentários:

 
Powered by Blogger