CSE 378 HomeWork 1
Winter 2004, Due: Wednesday 1/14/03
Purpose
The exercises here are intended to help you gain familiarity with
the MIPS instruction set and instruction encoding.
You'll do that by writing short sequences of MIPS instructions
that achieve some desired effect.
You will turn-in programs written in machine code, represented as
one or more 8-digit hexadecimal numbers (each of which is one MIPS R2000
instruction).
Before You Begin
- Teams
You should have received mail indicating who your (randomly selected)
team member is. If not, please get in touch with me.
- Reference Information
The most relevant parts of the R2000 instruction set are listed in Appendix
A.10 of the text. Their operation is described in Chapter 3 (or can be
devined from what is in Chapter 3.) The full instruction set is described in
a reference linked to the miscellaneous documentation page for
this course.
- Documentation Gotcha
"Instructions" that are tagged with pseudoinstruction
in A.10 are NOT
instructions, and cannot be used. (You'll note that they do not have
instruction encodings listed, which would make it hard to try to use them
in any case.) What are they? They're assembler-supported sort-of-instructions
(i.e, pseudoinstructions). We'll get to them...
- Registers
You can use any registers you want, EXCEPT DO NOT USE register 31.
Also, remember that register 0 is 0, no matter what you try to do to it.
- Do No Harm
You should not change the values of any operands in any exercise, unless
explicitly asked to do so as part of the function desired.
For instance, if the exercise is to put the larger of registers 1 and 2
into register 3, registers 1 and 2 should have their original values when
your code completes (which register 3 obviously is overwritten).
- Conceptual Warning
The problems below say things like "register 8 holds a signed
integer" and "register 8 points at a word in memory...".
Those statements are nonsense -- registers hold 32-bits and that's
it; registers pointing to memory name a byte, and that's it.
More precise wordings of those statements would be
"register 8 holds a 32-bit value to be used as a signed
integer" and "register 8 names a byte in memory that we'll
assume is the first of four bytes whose concatenation represents a
signed integer."
However, precisely worded questions were very tedious to read, so I
resorted to imprecision.
DO NOT take away the impression that data viewed at this level has a type,
though.
- How To Proceed
I suggest you work on each problem together, rather than dividing them
up (for instance).
In any case, if you find the problem more than trivial, you'll be better
off trying to write down the solution using some more readable notation
than directly as machine instructions
(e.g., something like the assembly language used in the book, but with
register numbers rather than names).
Once you are satisfied your code works, you can produce the equivalent
machine instructions.
Exercises
These exercises do not require any branches or immediate values
- Copy Register 8's (R8's) value into R9.
- R8 holds a 32-bit signed integer. R9 holds the value 0x00000001.
Put the complement of R8 in R10.
(E.g., 0x00000001 in R8 yields 0xFFFFFFFF in R10.)
- R8, which is of course 32-bits long, holds a bit string.
R9 holds the value 0x00000001.
Put in R10 the sum of bits 0 and bits 1 of R8.
For instance, if R8 hold 0x1234567, R10 should end up with (decimal) value 2;
if R8 holds 0x00000000, R10 should be set to 0; etc.
- R8 holds the main memory address of an array of bytes.
R9 holds a (signed) integer index, which we'll call N.
Set the Nth element in the array pointed at by R8 to 0x00.
(Use 0-based array indexing - R8 points at the 0th element.
Do not worry about "array bounds" issues.)
- R8 holds the main memory address of an array of 32-bit integers.
R9 holds a (signed) integer index, which we'll call N.
Set the Nth element of the array to 0.
(Ditto...)
- R8 and R9 each contain signed integers.
Form an 8-bit character by adding R8 and R9 and taking bits [3-10]
from the result.
Set R10 to the 32-bit integer value of that character.
- R8 contains a signed integer.
Put R8+R8-R8 in R8.
(This is a doubly trick question.)
Natural solutions to these exercises involve a
single branch instruction (each),
but no immediate values.
- R8 contains some bits. R9 contains 0x00000001.
Put in R10 the number of bits of R8 that are 1.
For example, if R8 is 0x30104800, R10 should end up with
value 5.
- R8 and R9 contain signed integers.
Put the larger of the two in R10.
- R8 points at an array of ASCII characters.
Each character is in the range [a-z], or in the range [A-Z],
or is a space.
The array is terminated by a byte with value 0x00.
R9 contains 0x00000020.
Convert the string pointed at by R8 to upper case.
(This can/should be done with a single branch instruction.)
These involve immediate values (but no branches).
- R8 holds the main memory address of an array of words.
R9 holds a (signed) integer index, which we'll call N.
Set the Nth word of the array to 0, using 1-based indexing
(R8 points to the 1st element).
- Put 0x04000400 in R8.
- R8 points at an aray of integers. Put the address
of the 13th element of that array in R9 (using 1-based addressing).
Turn-in
We'll use the turnin
program. Details to be announced.