CSE 410 Assignment 1

Spring 2007

Due: In class, Monday 4/9/2007

Reading: Chapter 2 in P&H 3rd edition, or Chapter 3 in P&H 2nd edition; Appendix A, particularly the sections on MIPS assembly language (on the CD or downloadable from the spim web site).

  1. Add comments to the following MIPS code and describe in one sentence what it computes. Assume that $a0 is used for the input and initially contains n, a positive integer. Assume that $v0 is used for the output.
  2. Here is a snippet of MIPS assembly: Here are the contents of a portion of memory before this snippet runs: Here are the initial values of the registers before this snippet runs: (Note $t0 and $t1 are not in the list because they are used only as temporary registers, and their initial values do not matter.)
  3. Trace through the MIPS code above. Each time you reach the "j start" line, write down the contents of addresses 400-416 and registers $s0, $s1, $s2, $t0 and $t1 by filling in the tables above (you might need to add more columns to the tables). In one sentence describe what this program does? (Note: the "addi" instruction is just like "add" except it allows you to add in a small constant directly rather than specifying a second register. For example, if $s0 contains 3, then after 'add $s2, $s0, 5' executes $s2 will contain 8.)

    Extra Credit: Both a conditional branch ("beq $s1, $s2, exit") and an unconditional jump ("j start") are executed each time through the loop. Only poor compilers would produce code with this loop overhead. Rewrite the assembly code so that it uses at most one branch or jump each time through the code. How many instructions are executed before and after the optimization? (Note: You may find it necessary to put one or more instructions before the "start" label.)

    In problems 4-6, resist the temptation to use a calculator that does the conversion between binary, decimal, and hexadecmial automatically.

  4. For the next 2 items, convert the given decimal number to a 32-bit, binary number AND also write the resulting binary number in hexadecimal notation. (Hint: remember that converting between binary and hexadecimal is very simple).
    1. 393
    2. 4,217

  5. For the next 2 items, convert the given binary numbers to both hexadecimal and decimal. (Hint: it may be less tedious to convert from hexadecimal to decimal than from binary to decimal.)
    1. 0000 0000 0100 0000 1111 1101 1100 1101
    2. 0011 1111 1001 1111 0000 1000 0011 0101

  6. For the next 2 items, convert the given hexadecimal number to both binary and decimal (treat both numbers as unsigned [positive] 32-bit numbers).
    1. 0x01001001
    2. 0xDEADBEEF

  7. The following program tries to copy words from the address in register $a0 to the address in register $a1, counting the number of words copied in register $v0. The program stops copying when it finds a word equal to zero. You do not have to preserve the contents of $v0, $a0, and $a1. This terminating word should be copied but not counted. There are multiple bugs in this program; fix the bugs and turn in a bug-free version.
  8. Show the MIPS instruction or instructions for the C statement: Assume that c corresponds to register $t0 and the array x has a base address of 4,000,000 (base 10). Also assume that the statement involves ordinary 4-byte integer variables and arrays.

  9. Show how the following MIPS instructions are encoded in memory: