################################################################################
##
## LSU EE 4720 Spring 2004 Homework 1 Solution
##
##
 ## Assignment: http://www.ece.lsu.edu/ee4720/2004/hw01.pdf


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

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

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

        # [x] Code should run quickly.
        # [x] Can modify registers $a0-$a3, $t0-$t9 and $v0 only.
        # [x] Do not modify $s0-$s7, $sp, $fp, or $ra
        # [x] Fill as many delay slots as possible.

wc:
        add $v0, $0, $0   # Initialize count.
        addi $t1, $0, 32  # Blank

        # Loop until non-blank found.
BLOOP:
        lb $t0, 0($a0)       # Load character
BLOOPT:
        beq $t0, $t1, BLOOP  # Continue if blank.
        addi $a0, $a0, 1

        # Non-blank found, increment word count, then find a blank.

        beq $t0, $0, EOS     # Check for end of string.
        lb $t0, 0($a0)
        addi $v0, $v0, 1     # Increment word count.

        # Loop until blank found.
WLOOP:
        beq $t0, $t1, BLOOP
        addi $a0, $a0, 1
        bne $t0, $0, WLOOP
        lb $t0, 0($a0)

EOS:
        jr $ra
        nop


################################################################################
## Problem 2 Solution

# The difference in performance is due to the different compilers used
# for preparing the benchmarks.
#
# According to the disclosures, the two systems have identical
# hardware however the benchmark suites were prepared using different
# compilers (or different versions of the same compiler, but they're
# still different).

# The better system uses what looks like the next version of the same
# compiler.  No doubt the newer compiler can do new optimizations, but
# these optimizations can only be used on certain pieces of code.
# Some of the benchmarks have pieces of code that can benefit from the
# new optimizations, while some benchmarks do not have such pieces of
# code (or do not have them in frequently executed areas).