################################################################################
##
## LSU EE 3755 Fall 2012 Homework 4
##
##
 ## Due Friday,  16 November 2012

 # See http://www.ece.lsu.edu/ee3755/2012f/hw04.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 Linux 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/ee3755/proc.html
  #
  # (5) Copy this assignment, local path name
  #     /home/faculty/koppel/pub/ee3755/s/hw04.s
  #     to a directory ~/hw04 in your class account. (~ is your home
  #     directory.) Use this file for your solution.
  #
  # (6) Find the problems in this file and solve them.
  #
  #     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.)
  #
  #
  # (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/ee3755/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 wrong
#       word counts.  You'll need to fix those for Problem 2,
#       but 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."
doi:
        .asciiz "One two THREE f_o$ur"
        .byte 4
        .asciiz "The quick brown fox jumps over the lazy dog"
        .byte 9
        .asciiz "o"
        .byte 1
        .asciiz " o"
        .byte 1
        .asciiz "o "
        .byte 1
        .asciiz "123 infinity"
        .byte 1
        .asciiz ""
        .byte 0
        .asciiz "0"
        .byte 0
        .byte 255

word_chars:
        .asciiz "ABCDEFGHIJKLMNOPQRSTUVWXYZ$_abcdefghijklmnopqrstuvwxyz"

char_class:
        .space 256

msg:
        .ascii  "String: \"%/s7/s\"\n"
        .asciiz "         Words %/s6/d  Correct Words %/t0/d  %/t1/s\n";
msg_cor_error:
        .asciiz ""
        .asciiz "** Error **"

        .align 4

        .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 $a0, word_chars
        la $a1, char_class
        la $s3, msg_cor_error
        jal char_table_init
        nop
        la $s5, doi
        la $a1, char_class
T_NEXT:
        addi $s7, $s5, 0
        jal word_count
        addi $a0, $s5, 0
        addi $s6, $v0, 0;
T_LOOP:
        lb $t0, 0($s5)
        bne $t0, $0, T_LOOP
        addi $s5, $s5, 1
        lb $t0, 0($s5)
        beq $t0, $s6, T_CORRECT
        addi $t1, $s3, 0
        addi $t1, $t1, 1
T_CORRECT:
        la $a0, msg
        addi $v0, $0, 11
        syscall
        addi $s5, $s5, 1
        lbu $t0, 0($s5)
        addi $t0, $t0, -255
        bne $t0, $0, T_NEXT
        nop

        addi $v0, $0, 10
        syscall


################################################################################
## Problem 1
##
## Initialize Character Table

# Comment this code for a competent MIPS programmer.


char_table_init:
        ## Register Usage
        #
        # Call:   $a0  String containing word characters.
        #         $a1  Location of character classification array.
        # Return: $v0  Number of characters.
        #

        addi $t1, $0, 1
        addi $t2, $a0, 0
WV_LOOP:
        lbu $t0, 0($a0)
        beq $t0, $0, WV_DONE   # Exit the loop if at end of input string.
        add $t0, $t0, $a1
        sb $t1, 0($t0)
        j WV_LOOP
        addi $a0, $a0, 1
WV_DONE:
        jr $ra
        sub $v0, $a0, $t2


################################################################################
## Problem 2
##
## word_count: Count words in string.

# Complete the routine below.

word_count:
        ## Register Usage
        #
        # Call:   $a0  String to analyze.
        #         $a1  Character class table.
        # Return: $v0  Number of words

        # [ ] Use the character table to determine if a char is part of a word.
        # [ ] 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.

        # Put solution below.


DONE:
        jr $ra
        nop