################################################################################
##
## LSU EE 4720 Fall 2003 Homework 2
##
##
 ## Due Monday,  6 October 2003

.data
name:
        .asciiz ""  # Put your name between the quotes.

 ## Instructions:
  #
  # (1) Find the undergraduate workstation laboratory, room 126 EE
  #     building.
  #
  # (2) Locate your account.  Accounts were distributed in class
  #     on Wednesday, 24 September and Friday 26 September.  If you
  #     did not get an account please E-mail: koppel@ece.lsu.edu
  #
  # (3) Log in to a Sun workstation.
  #     The account should start up with a WIMP interface (windows, icons,
  #     mouse, pull-down menus)  ( :-) ) but one or two things need
  #     to be done from a command-line shell.  If you need to brush up
  #     on Unix commands follow http://www.ece.lsu.edu/v/4ltrwrd/.
  #
  # (4) If you haven't already, follow the account setup instructions here:
  #     http://www.ece.lsu.edu/ee4720/proc.html#entry1
  #
  # (5) Copy this assignment, local path name
  #     /home/classes/ee4720/com/s/hw02.s, to a directory ~/hw in your
  #     class account. (~ is your home directory.)  Use this file for your
  #     solution.
  #
  # (6) Find the problems in this file and solve them.  Problem 0 is just
  #     a check that everything is working correctly, Problem 1 is a program
  #     that you must write and run, and Problem 2 requires you to add comments
  #     to an existing program.
  #
  #     A procedure shell has been provided for the first problem.  Place
  #     your solution there.
  #
  #     The first block of assembler code runs a test on your solution.
  #     That code can be modified, for example, putting the incorrect test
  #     case first.
  #
  #     Your entire solution should be in this file.
  #
  #     Do not rename the line labels in this file and be sure to use the
  #     directory and filename given above.  (Line labels may be added.)
  #
  # !!  Problem one is not a research problem.  It would not be difficult
  #     to find the solution to a similar problem somewhere.  Don't.
  #     Solve it yourself, though asking for help from the professor,
  #     TA, or other students is okay.
  #
  # (7) Your solution will automatically be copied from your account by
  #     the TA-bot, probably at 23:59 on Monday 6 October 2003.


 ## Additional Resources
  #
  # MIPS Architecture Manual Volume 2 (Contains a list of instructions.)
  #      http://www.ece.lsu.edu/ee4720/mips32v2.pdf
  #      Note: SPIM implements MIPS-I instructions.
  #
  # SPIM Documentation:
  #      Appendix A of Patterson and Hennessey.
  #      http://www.ece.lsu.edu/3755/spim.pdf
  #
  # Account Setup and Emacs (Text Editor) Instructions
  #      http://www.ece.lsu.edu/ee4720/proc.html
  #      To learn Emacs look for and follow instructions for the Emacs tutorial.
  #
  # Unix Help
  #      http://www.ece.lsu.edu/v/4ltrwrd/


 ## Grading Criteria
  #
  # The code must pass the tests in this file, and similar tests.
  #
  # The code must be reasonably efficient.
  #
  # Points will be DEDUCTED for needlessly setting a register to zero!!!!
  # For example, instead of doing this:
  #   add $2, $0, $0   # This instruction not needed!!!!!!!!!!!!!!!!!!!
  #   or  $2, $3, $4
  # Do this:
  #   or  $2, $3, $4


################################################################################
## Problem 0

# Do the setup described in the instructions above.
#
# Before making any changes to this file (other than comments) run
# the assembler/simulator SPIM using the following steps:
#
#  Load this file into an Emacs buffer using the class-account Emacs.
#
#  If setup was done correctly comments should be red, "Problem 0" above
#  should be in a black, bold, sans-serif font and the assembler below
#  should look something like fruit salad, with pale blue mnemonics,
#  italicized pseudo instructions, purple assembler directives, green
#  line labels, etc.
#
#  Start the SPIM assembler/simulator by pressing [F9].
#    -> A window entitled "xspim" should pop up.  The top pane should show
#       register values, the next pane should have buttons, the third
#       should show the program in binary and assembler forms, the fourth
#       pane shows the data area, and the bottom pane (which might extend
#       past the bottom of the screen) shows messages.
#
#  Run the program by clicking the "run" button then clicking "ok" on the
#  dialog box that pops up.
#    -> A window entitled "SPIM Console" should pop up, the window
#       should be asking you to include your name in this file.  After
#       a name is entered and Spim is re-run it should show four wrong
#       conversions (for Problem 1).  For Problem 0 that's success!!!


 ## Note on SPIM
#
# Clicking the close button (usually the upper-right-hand button on
# the window frame) of any window will immediately exit SPIM.
#

 ## Troubleshooting
#
# Make sure that the "run" dialog box shows 0x0400000 for the starting
# address.  If not, __start was not properly defined, possibly due to
# an error before __start.
#
# Check the messages (bottommost) pane for syntax and other errors.  It
# may be necessary to shorten other panes to make the messages pane visible.
# Common syntax errors include using "addi" instead of "add", or vice versa.
# Another common error is mistyping a label in a branch or jump target.
#
# Check the "Text Segments" pane to make sure all of your program is there.
# If not, there may have been an error reading the program.
#
# If your program fails a test or otherwise does not produce the
# expected output modify the test code so that the particular test it
# fails comes first (if it's not already).  Then, single-step the code
# (using the "step") button until you find the problem.
#
# If you've hit a wall ask the TA or instructor help.  It's better to
# err on the side of too many questions than too few.


################################################################################
## Main Routine

# This routine runs the test code for the first problem and makes sure
# you put your name at the top of the file.  It is also used in
# Problem 2. It does not have to be modified (other than comments),
# but it can be if it would help.  For example, you might comment out
# a test for one of the problems when working on the other.  If you're
# thinking of improving your grade by deleting the test cases which
# your code gets wrong you'll need to come up with a better plan.

        .data
who_are_you:
        .asciiz "Please insert your name at the top of the file where indicated."
test_data:
        .asciiz "-4"
        .word -4
        .asciiz "123 "
        .word 123
        .asciiz "  3224834 "
        .word 3224834
        .asciiz " - 7912 "
        .word -7912
        .asciiz "X"
msg:
        .asciiz "The value of string %/s4/s returned was  %/t1/d  (%/t2/s).\n"
textc:
        .asciiz "Correct"
textw:
        .asciiz "Wrong"
msgt:
        .asciiz "A total of %/s2/d correct and %/s3/d wrong.";


        .text
        .globl __start
__start:
        la $s0, name
        lb $s0, ($s0)
        bne $s0, $0  TESTS
        la $a0, who_are_you  # [sic]  <- Explain this comment.
        addi $v0, $0, 11
        syscall
        addi $v0, $0, 10
        syscall

TESTS:
        la $s0, test_data
        addi $s7, $0, -4
        or $s2, $0, $0
        andi $s3, $s3, 0
TLOOP:
        lbu $t0, 0($s0)
        addi $t0, $t0, -88
        beq $t0, $0, TDONE
        xor $a0, $s0, $0
        jal atoi
        add $s4, $s0, $0
TEOS:
        lbu $t0, 0($s0)
        bne $t0, $0, TEOS
        addi $s0, $s0, 1

        addi $s0, $s0, 3
        and $s0, $s0, $s7
        lw $t1, 0($s0)
        bne $t1, $v0, TWRONG
        addi $s0, $s0, 4
        la $t2, textc
        j TMSG
        addi $s2, $s2, 1

TWRONG:
        la $t2, textw
        addi $s3, $s3, 1

TMSG:
        or $t1, $v0, $v0
        addi $v0, $0, 11
        la $a0, msg
        syscall

        j TLOOP
        nop

TDONE:
        addi $v0, $0, 11
        la $a0, msgt
        syscall

        addi $v0, $0, 10
        syscall


################################################################################
## Problem 1 Solution Shell

# When the routine below is called register $a0 has the address of a
# null-terminated string, that string is an integer (in ASCII).  For
# example, "1", " 23423", " - 71 ".  Note that the integer may be
# negative and that there may be spaces before and after the minus
# sign and digits.  Assume that there are no spaces between digits and
# that only digits, a minus sign, and spaces are in the string.
#
# The routine should place the integer value of that string in
# register $v0, and then return.  Make sure you understand the
# difference between an ASCII string representation of an integer (the
# call argument) and a 32-bit two's complement representation of the
# integer (the return value).
#
# When your write your code follow the additional guidelines below.
# If the register use guidelines are not followed the test code might
# not work.  Speed and multiplication technique are an important
# part of grading.

# FYI:  ASCII blank is 32 and - (minus) is 45.

atoi:

        ## Register Usage
        #
        # $a0:  Procedure argument: pointer to string.
        # $v0:  Return value: integer corresponding to string.

        # [ ] Assume that multiplication takes 7 cycles.
        # [ ] Code should run quickly, avoid multiplication if that helps.
        # [ ] Can modify registers $a0-$a3, $t0-$t9 and $v0 only.
        # [ ] Do not modify $s0-$s7, $sp, $fp, or $ra
        # [ ] Fill as many delay slots as possible.

        # Insert solution here.


        jr $ra
        nop



################################################################################
## Problem 2

# First, read the material on SPIM system call routines in
#  http://www.ece.lsu.edu/ee3755/2002/l11.html

# Then, add comments to the main routine above.  (The material between
# Main Routine and Problem 1 Solution Shell)

# Important: The comments you add should explain why something is
# being done, they should not just explain what the instruction does.

# For example, the comment below is bad because the reader should
# already know what an add does:
#
# addi $t0, $t0, 1  # Add 1 to $t0

# The comment below is good because it tells the reader what the
# register value is used for and why it's being incremented.
#
# addi $t0, $t0, 1  # Point to the next character in the input string.

# Consider the following joke: A lost balloonist floating near an
# office building shouts to a person inside, "Where am I?"  The person
# answers, "Ten meters above the ground, in a balloon."  The balloonist
# responds, "I must be in Redmond, WA, at M$ headquarters."
# "How did you know that?"  The balloonist replies, "Everything you
# said is correct, but unhelpful."

# Also, try to be concise.