################################################################################
##
## LSU EE 4720 Spring 2004 Homework 2
##
##
 ## Due Friday, 27 February 2004

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

 ## Instructions:
  #
  # (1) Copy this assignment, local path name
  #     /home/classes/ee4720/com/s/hw2.s, to a directory ~/hw in your
  #     class account. (~ is your home directory.)  Use this file for your
  #     solution.
  #
  # (2) Find the problem in this file and solve it.
  #
  #     A procedure shell has been provided for the first problem.  Place
  #     your solution there.
  #
  #     The first block of assembler code runs your solution.
  #     That code can be modified.
  #
  #     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.)
  #
  # (3) 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 work.
  #
  # 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

 ## 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 does not have to be
# modified but it can be if it would help.  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
NOTSOINNOCENT:

# Note: The original JPL press release did not contain the secret message,
# it was added for this problem.

	.ascii "NASA's Opportunity rover drove about 3.5 meters (11 feet) \n"
	.ascii "early Thursday toward a rock outcrop in the wall of a small\n"
	.ascii "crater on Mars, and mission controllers plan to send it the\n"
	.ascii "rest of the way to the outcrop late Thursday. \n"
	.ascii "\n"
	.ascii "Opportunity's twin, Spirit, successfully reformatted its    \n"
	.ascii "flash memory on Wednesday. Flash is a type of rewritable    \n"
	.ascii "memory used in many electronic devices, such as digital\n"
	.ascii "cameras, to retain information even while power is \n"
	.ascii "off. Problems with the flash memory interfered with Spirit's   \n"
	.ascii "operations from Jan. 22 until this week. Engineers  \n"
	.ascii "prescribed the reformatting to prevent recurrence of the \n"
	.ascii "problem.   \n"
	.ascii "   \n"
	.ascii "On Thursday, Spirit's main assignment is to brush off an\n"
	.ascii "area on the rock nicknamed Adirondack to prepare for a  \n"
	.ascii "dust-free examination of its surface. On Friday, controllers \n"
	.ascii "at NASA's Jet Propulsion Laboratory, Pasadena, Calif., plan\n"
	.ascii "to have Spirit grind off a small patch of Adirondack?s outer   \n"
	.ascii "surface and inspect the rock's interior. Spirit may start\n"
	.ascii "driving over the weekend toward a crater about 250 meters   \n"
	.ascii "(about 270 yards) to the northeast.  \n"
	.ascii "\n"
	.ascii "For Opportunity, halfway around Mars from Spirit,  \n"
	.ascii "controllers changed plans Thursday morning. They postponed a    \n"
	.ascii "trenching operation until the rover gets to an area of its  \n"
	.ascii "landing-site crater where the soil has a higher    \n"
	.ascii "concentration of large- grain hematite. That mineral holds\n"
	.ascii "high interest because it usually forms under wet \n"
	.ascii "conditions. The main science goal for both rovers is to find  \n"
	.ascii "geological clues about past environmental conditions at the   \n"
	.ascii "landing sites, especially about whether conditions were ever \n"
	.ascii "watery and possibly suitable for sustaining life.  \n"
	.ascii " \n"
	.ascii "Instead of trenching, Opportunity will be commanded after it\n"
	.ascii "next wakes up to drive about 1.5 meters (about 5 feet)  \n"
	.ascii "farther, possibly to within arm's reach of one of the rocks   \n"
	.ascii "in the exposed outcrop. \n"
	.ascii "   \n"
	.ascii "Before it began driving on Wednesday, Opportunity finished   \n"
	.ascii "using its alpha particle X-ray spectrometer for the first  \n"
	.ascii "time. This spectrometer, which assesses what chemical  \n"
	.ascii "elements are present, took readings on an area of soil that \n"
	.ascii "the rover had previously examined with its microscope.  \n"
	.ascii "   \n"
	.ascii "Each martian day, or sol, lasts about 40 minutes longer \n"
	.ascii "than an Earth day. Spirit begins its 34rd sol on Mars at  \n"
	.ascii "3:22 a.m. Thursday, Pacific Standard Time. Opportunity \n"
	.ascii "begins its 14th sol on Mars at 3:43 p.m. Friday, PST.\n"
	.ascii "  \n"
	.ascii "   \n"
	.ascii "JPL, a division of the California Institute of Technology in \n"
	.ascii "Pasadena, manages the Mars Exploration Rover project for   \n"
	.ascii "NASA's Office of Space Science, Washington, D.C. Images and\n"
	.ascii "additional information about the project are available from    \n"
	.ascii "JPL at http://marsrovers.jpl.nasa.gov and from Cornell  \n"
	.asciiz "University, Ithaca, N.Y., at http://athena.cornell.edu.\n"

MSG:
        .asciiz "Secret Message: %/a1/s"

PLAINTEXT:
        .word 100


        .text
        .globl __start
__start:
        la $a0, NOTSOINNOCENT
        la $a1, PLAINTEXT
        jal extract
        la $a0, MSG
        la $a1, PLAINTEXT
        addi $v0, $0, 11
        syscall
        addi $v0, $0, 10
        syscall
        nop


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

# The text at the NOTSOINNOCENT label above contains a secret message
# hidden in the blanks just before the new line (\n) characters.  (The
# secret message was added for this problem.)  Those extra blanks are
# sort of obvious above because each line is surrounded by quotes but
# with an ordinary email program the blanks would go unnoticed.

# Each group of 14 lines in the text specifies a number and each line
# specifies a digit in that number's radix-5 (base-5) representation.
# The most-significant digit is first.  The number of spaces before
# the \n is the digit value.  For example, the most significant first
# three digits of the first number in the message above are 100.  The
# text sample above, being 57 lines, has four complete numbers, plus
# an extra digit.  Consider only complete numbers (ignore extra
# digits, in this case the last line.)

# Each number specifies four characters (in ASCII) of the secret
# message.  The first character is bits 31:24 of the number's binary
# representation, the second character is bits 23:16, etc.

# When the routine below is called register $a0 will hold the address
# of the seemingly innocent text and register $a1 will hold an address
# of some writable memory.  The routine should find the secret message
# and write it at that address.

# Here are some things your routine has to do:
#
#  Count the number of blanks at the end of a line.
#    This is a relatively straightforward extension of techniques
#    used in Homework 1.
#
#  Convert a 14-digit radix-5 number into an integer.
#    First one needs to know the difference between the digits in a number's
#      representation and that number's value in a register.  This may
#      cause some initial confusion, once the confusion clears the rest
#      should be straightforward.  See Fall 2003 Homework 2 Problem 1
#      (http://www.ece.lsu.edu/ee4720/2003f/hw02sol.html)
#    Be reasonably efficient with the conversion.  Avoid multiplies.
#
#  Write the secret message to memory.
#    There's an easy way to do this.

# Notes:
#
#  Yes, radix 4 or 8 would be better choices.  But it's radix 5.
#  Yes, there are 14-digit radix-5 numbers that cannot fit in a register
#   but the text has no such numbers.

        ## Register Usage
        #
        # $a0: Procedure call argument:  Address of text.
        # $a1: Procedure call argument:  Address at which to write decoded msg.
        # There are no return values.

        # [ ] The secret message is a short English sentence.
        #     Make sure your message is not just a part of the original text.
        # [ ] Avoid multiplication instructions and especially division and mod.
        # [ ] Try to use an efficient way to write the secret message.
        # [ ] Fill as many branch delay slots as possible.
        # [ ] Do not put an insn in a loop if it could be placed before the loop.
        # [ ] Streamline the code.  This is important.
        # [ ] Make sure the message is null terminated.

        # Solutions can use registers $t0-$t9 and can modify $a0 and $a1
        # Handy constants:  '\n' = 10,  ' ' = 32

extract:
        # Solution goes here.


        jr $ra
        nop