CSE 378 HomeWork 1

Winter 2005
Due: (Revised) Wednesday 1/12/05

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.

Note that we don't intend you to run your programs, except by hand simulation.

Before You Begin

  • 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 2 (or can be devined from what is in Chapter 2.) The full instruction set is described in MIPS32 Architecture for Programmers, Volume II, a copy of which is reference linked from the miscellaneous documentation page for this course.

  • Documentation Gotcha "Instructions" that are tagged with pseudoinstruction in A.10 are NOT instructions, and should not be used.

  • Registers You can use any registers you want, EXCEPT DO NOT USE register $31. Also, remember that $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 (while 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.

  • Assembler Exercises

    Hand in short bits of assembler code for these exercises.

    1. Copy $8's value into $9.

    2. Put 0x12348ABC in $9. (Be careful.)

    3. $8 holds a 32-bit signed integer. Put the complement of $8 in $9. (E.g., 0x00000001 in $8 yields 0xFFFFFFFF in $9.)

    4. $8 holds the main memory address of the 0th byte of an array of bytes. $9 holds a (signed) integer index, which we'll call N. Set the Nth element in the array pointed at by $8 to 0x00. You can use $10 as a temporary (i.e., you can destroy whatever value it has when your code starts). (Do not worry about "array bounds" issues.)

    5. $8 holds the address of the 0th byte of an array of 32-bit integers. Set the 4th element of the array (the one with index 3) to 0.

    6. $8 holds the address of the 0th byte of an array of 32-bit integers. $9 holds a (signed) integer index, which we'll call N. Set the Nth element in the array pointed at by $8 to 0x00000001. Use as few additional registers as temporaries as you can.

    7. Swap the contents of $8 and $9. Do not modify any other registers.

    8. $8 contains a signed integer. Put $8+$8-$8 in $8. You can use $9 as a temporary. (This is a doubly trick question.)

    9. $8 and $9 contain signed integers. Put the larger of the two in $10.

    10. $8 contains some bits. Put in $9 the number of bits of $8 that are 1. For example, if $8 is 0x30104800, $9 should end up with value 0x00000005. You can use $10 and $11 as temporaries.

    11. $8 points at the 0th byte of 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. Convert the string pointed at by $8 to upper case. You can use $9 and $10 as temporaries.

    Machine Language

    Give the machine code, in hex, for the following bit of assembler. Assume the first instruction is loaded in memory at address 0x00001000.
                 add  $8, $8, $12
    loop:        addi $8, $8, 12
                 lw   $9, -4($8)
                 bne  $9, $0, skip
    	     sub  $8, $8, $9
    skip:        j    loop
    

    Turn-in

    Put your answers in a textfile called hw1. Run the command 'turnin -ccse378 hw1' on attu to submit your assignment.