################################################################################
##
## LSU EE 4720 Spring 2004 Homework 1
##
##
 ## Due Wednesday,  18 February 2004

 # See http://www.ece.lsu.edu/ee4720/2004/hw01.pdf for additional questions.

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

 ## Instructions:
  #
  # (1) Find the undergraduate workstation laboratory, room 126 EE
  #     building.
  #
  # (2) Locate your account.  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/hw1.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.
  #
  #     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.


 ## 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.  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."

TESTS:  .asciiz "one two  three f five  "
        .asciiz "one two  three f five"
        .asciiz "one"
        .asciiz "  one"
        .asciiz ""
MSG:    .asciiz "%/$s0/d words in \"%/$s1/s\"\n"

        .text
        .globl __start
__start:
        la $s0, name
        lb $s0, ($s0)
        bne $s0, $0,  DOTESTS
        la $a0, who_are_you
        addi $v0, $0, 11
        syscall
        addi $v0, $0, 10
        syscall

DOTESTS:
        la $s1, TESTS
TLOOP:
        jal wc
        add $a0, $s1, $0
        add $s0, $v0, $0
        la $a0, MSG
        addi $v0, $0, 11
        syscall
        nop
        addi $s3, $s1, 1
SLOOP:
        lb $s2, 0($s1)
        bne $s2, $0, SLOOP
        addi $s1, $s1, 1

        bne $s1, $s3, TLOOP


        addi $v0, $0, 10
        syscall
        nop



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

# When the routine below is called register $a0 has the address of a
# null-terminated string.
#
# When the routine returns register $v0 should hold the number of
# words in the string.  For purposes of this problem a word consists
# of a group of any characters except a space.  There can be one or more
# spaces between words.  For simplicity, only consider a space (ASCII 32),
# ignore tabs, linefeeds, and other whitespace characters.
#
# 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.

atoi:

        ## Register Usage
        #
        # $a0:  Procedure argument: pointer to string.
        # $v0:  Return value: number of words in the string.

        # [ ] Code should run quickly.
        # [ ] 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.
        #
        # Note: This is an easy problem, more challenging ones will
        # be assigned.

wc:

        # Insert solution here.





        jr $ra
        nop