# SPIM program declare.s
# Data segment
.data
ch: .byte 'C'                   # C equivalent: char ch;
#                                               ch = 'C';
chint: .word                    # Reserves space for ch considered as 
                                #  an integer
posint: .word 32767             # C equivalent: int posint;
#                                               posint = 32767;
negint: .word -32767            # C equivalent: int negint;
#                                               negint = -32767;
f: .float 3.2767e4              # C equivalent: float f;
#                                               f = 3.2767e4;
fint: .word                     # Reserves space for f considered as 
                                #  an integer
newline: .byte '\n'
# Text segment (instructions)
.text
__start:         # Start of arithmetic & logical operations
#
# The first block of code prints ch as a character
# This SPIM code is equivalent to the C command printf("%c\n",ch);
#
lb $a0, ch       # Load the byte labeled ch into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints the byte as a character, on the console
                 # See the system call info in spim.inst.txt !!!
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
                 # See system call info!!!
#
# The next block of code copies the contents of ch into the location 
#  labeled chint
#
lb $a0, ch       # Load the byte labeled ch into register $a0
                 #  The byte will be right-justified in the register
sw $a0, chint    # Store the word from register $a0 to the location 
                 #  labeled chint (this step is necessary, 
                 #  because we plan to re-use chint; see below)
#
# The next block of code prints out the value of chint as an integer 
#  (not a character)
# This SPIM code is equivalent to the C command printf("%d\n",ch);
#
li $v0, 1         # Put the integer 1 into register $v0
syscall          # This prints the contents of $a0 as an integer,
                 #  on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints chint as a floating-point number
# This SPIM code is equivalent to the C command printf("%e\n",ch);
#
lwc1 $f12, chint # Load the value at location chint into register 
                 #  $f12, which is in coprocessor 1, so we can't just 
                 #  use lw
li $v0, 2             # Put the integer 2 into register $v0
syscall          # This prints the floating-point number on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of posint as an integer
# This SPIM code is equivalent to the C command printf("%d\n",posint);
#
lw $a0, posint   # This loads the value stored at posint into $a0
li $v0, 1         # Put the integer 1 into register $v0
syscall          # This prints the contents of $a0 as an integer,
                 #  on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of posint as a floating-point
#  number
# This SPIM code is equivalent to the C command printf("%e\n",posint);
#
lwc1 $f12, posint # Load the value at location posint into register
                 #  $f12, which is in coprocessor 1, so we can't just 
                 #  use lw
li $v0, 2             # Put the integer 2 into register $v0
syscall          # This prints the floating-point number on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of negint as an integer
# This SPIM code is equivalent to the C command printf("%d\n",negint);
#
lw $a0, negint   # This loads the value stored at negint into $a0
li $v0, 1         # Put the integer 1 into register $v0
syscall          # This prints the contents of $a0 as an integer,
                 #  on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of negint as a floating-point
#  number
# This SPIM code is equivalent to the C command printf("%e\n",negint);
#
lwc1 $f12, negint # Load the value at location negint into register
                 #  $f12, which is in coprocessor 1, so we can't just 
                 #  use lw
li $v0, 2             # Put the integer 1 into register $v0
syscall          # This prints the floating-point number on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of a pointer to posint
# This SPIM code is equivalent to the C commands 
#  register p; p = &posint; printf("%p\n",p);
#
la $a0, posint   # This loads the address of posint into $a0
                 #  C equivalent: register p; p = &posint;
li $v0, 1         # Put the integer 1 into register $v0
syscall          # This prints the contents of $a0 as an integer,
                 #  on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of the floating-point number
#  stored at f, on the console
# This SPIM code is equivalent to the C command printf("%6.6f\n",f);
#
lwc1 $f12, f     # Load the value at location f  into register
                 #  $f12, which is in coprocessor 1, so we can't just 
                 #  use lw
li $v0, 2             # Put the integer 2 into register $v0
syscall          # This prints the floating-point number on the console
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
#
# The next block of code prints the value of the floating-point number
#  stored at f, but interpreted as an integer, on the console
# This SPIM code is equivalent to the C command printf("%d\n",f);
#
lwc1 $f0, f      # Load the value at location f into register
                 #  $f0, which is in coprocessor 1, so we can't just 
                 #  use lw
mfc1 $a0, $0     # This does a bit-for-bit copy from $f0 to the
                 #  register $a0 in the CPU
sw $a0, fint     # Store the bitwise copy of f into location fint
li $v0, 1         # Put the integer 1 into register $v0
syscall          # This prints the copy of f on the console,
                 #  as an integer
lb $a0, newline  # Load the byte labeled newline into register $a0
li $v0, 11       # Put the integer 11 into register $v0
syscall          # This prints a newline on the console
li $v0, 10       # Put the integer 10 into register $v0
syscall          # Return control to the OS 


