################################################################################
##
## LSU EE 4720 Fall 2005 Homework 2
##
##
## Due Wednesday, 5 October 2005
## Instructions:
#
# (1) Copy this assignment to your own computer or to a class account,
# or just print it.
# A class account is not needed for this assignment but will
# be needed for the next one.
#
# (2) Find the problem in this file and solve it.
#
# 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. You can also hand this assignment in on paper or
# via email to koppel@ece.lsu.edu
#
# Note:
# For this assignment you do not need to use spim, however it can
# be helpful and so there are instructions for spim below.
## 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
## 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.
################################################################################
## Problems
# The pack routine further below takes two arguments, a pointer to a
# C-style string, in register $a0, and a pointer to some memory, in
# register $a1. The routine writes a packed (compressed) version of the
# string to the memory at $a1. Each pair of bytes of the packed string
# holds three characters of the original string. Not every character in
# the original string can be packed, those characters are converted into
# "." (periods, ascii value 0x2e).
## Problem 1
# Add comments to the pack routine. The comments should describe
# how the instructions realize the goal of packing the string. The
# comments should NOT simply explain what the instruction itself does,
# assume the reader already knows that. For example:
# addi $a0, $a0, 1 # Increment $a0 by 1. BAD COMMENT
# addi $a0, $a0, 1 # Advance to next character in string. GOOD COMMENT
# The first comment above is bad because, since the reader already knows
# MIPS, it does not help the reader understand the code.
# Try to comment most instructions.
# Add comments to the beginning of each section (see the code) explaining
# what the code in the section does.
# For an example of the desired commenting style see the solution to Fall
# 2003 Homework 2: http://www.ece.lsu.edu/ee4720/2003f/hw02sol.html
## Problem 2
# Show the format of the packed data. That is, show where the three
# characters are placed (using bit numbers or a diagram) and show how
# the end of the packed string is identified.
## Problem 3
# The routine as currently written will translate upper-case characters
# to "." (periods) in the packed string. Modify the routine so that
# upper-case characters are translated to lower-case letters in the
# packed string. DO NOT translate the characters one at a time, as in the
# upper-case routine presented in class. Use the same technique the
# pack routine uses.
# A correct solution requires only three instructions and the addition
# of some data. See the HINT in the pack routine.
## Note
# In the next assignment, Homework 3, you will need to write the unpack
# routine. Feel free to start now.
################################################################################
## Demonstration Routine
# This routine calls the pack and unpack routines on a sample
# string below (STR1) and displays the original and unpacked strings
# side by side.
# The comments in this routine are intended for those who are
# learning, in particular EE 4720 students. In your solution to
# Problem 1 do not comment at this level.
.data # Indicate to the assembler that the stuff below is data.
#
# String to demonstrate pack and unpack routines.
#
DEMO_STRING: # Address of stuff immediately following.
# Place string in memory here, end string with NULL (0).
.asciiz ".,!Time flies like an arrow, fruit flies like a banana."
.byte 0, 0, 0 # Put three zeros in memory here.
#
# Memory space for packed and unpacked strings.
#
.align 4 # Make sure the address below is a multiple of 4.
BUFFER_PACK:
.space 256 # Allocate 256 characters for use by the pack routine.
.align 4
BUFFER_UNPACK:
.space 256 # Allocate 256 characters for use by the unpack routine.
.align 4
#
# Message printed by the demonstration routine.
#
MSG:
.asciiz "Original:\n\"%/a2/s\"\nUnpacked: \n\"%/a1/s\"\n"
.text # Indicate to the assembler that the stuff below is code.
.globl __start
__start:
# Load address of string and address of memory for packed
# string and call the pack routine.
#
la $a0, DEMO_STRING
la $a1, BUFFER_PACK
jal pack
nop
# Load address of packed string and address of memory for unpacked
# string and call the unpack routine.
#
la $a0, BUFFER_PACK
la $a1, BUFFER_UNPACK
jal unpack
nop
# Call a system (simulator) routine to display results and
# then exit.
#
la $a0, MSG
la $a1, BUFFER_UNPACK
la $a2, DEMO_STRING
addi $v0, $0, 11
syscall
addi $v0, $0, 10
syscall
nop
################################################################################
## Pack Routine
## Register Usage
#
# $a0: Procedure call argument. Address of string to pack.
# $a1: Procedure call argument. Address of memory for packed string.
# There are no return values.
# [ ] Add comments to this routine.
# Comments should explain what routine does.
# [ ] Add comments explaining what sections do.
# [ ] Show format of packed data.
# [ ] Show how end of string indicated in packed data.
# [ ] Modify so that upper-case translated to lower case.
# For an example of commenting style see the solution to Fall
# 2003 Homework 2:
# http://www.ece.lsu.edu/ee4720/2003f/hw02sol.html
.data
.align 4
LUT:
.space 256
KEEP:
.asciiz ".,!?;abcdefghijklmnopqrstuvwxyz "
.byte 0 # HINT, needed for solution to last part. <---- HINT!!
.text
pack:
#
## Section
#
la $t0, LUT
addi $t2, $t0, 252
PLOOP1:
sw $0, 0($t0)
bne $t0, $t2, PLOOP1
addi $t0, $t0, 4
#
## Section
#
la $t0, LUT
la $t1, KEEP
addi $t3, $0, 0
PLOOP2:
lbu $t2, 0($t1)
add $t4, $t0, $t2
sb $t3, 0($t4)
addi $t1, $t1, 1
bne $t2, $0 PLOOP2
addi $t3, $t3, 1
addi $t3, $0, -1
sb $t3, 0($t0)
#
## Section
#
PLOOP:
lbu $t4, 0($a0)
add $t1, $t0, $t4
lb $t2, 0($t1)
add $t5, $t2, $0
lbu $t4, 1($a0)
add $t1, $t0, $t4
lb $t3, 0($t1)
or $t5, $t5, $t3
sll $t3, $t3, 5
or $t2, $t2, $t3
lbu $t4, 2($a0)
add $t1, $t0, $t4
lb $t3, 0($t1)
or $t5, $t5, $t3
sll $t3, $t3, 10
or $t2, $t2, $t3
sh $t2, 0($a1)
addi $a1, $a1, 2
bgez $t5 PLOOP
addi $a0, $a0, 3
#
## Section
#
ori $t2, $t2, 0x8000
jr $ra
sh $t2, -2($a1)
################################################################################
## Unpack Routine
## Register Usage
#
# $a0: Procedure call argument. Address of packed string.
# $a1: Procedure call argument. Address of memory for unpacked string.
# There are no return values.
# This will be Homework 3.
.data
NYI:
.asciiz "Not yet implemented."
.text
unpack:
la $t0, NYI
ULOOP:
lb $t1, 0($t0)
addi $t0, $t0, 1
sb $t1, 0($a1)
bne $t1, $0, ULOOP
addi $a1, $a1, 1
jr $ra
nop