Assignment 5, due Sep 24

Part of the homework for 22C:60, Fall 2009
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Always write your name legibly as it appears on your University ID and on the class list!

Homework

  1. Background: Consider this loop:
            i = 0;
            while (i < 10) {
                    -- code in the loop body --
                    i = i + 1;
            }
    
    Assume that register 3 is used for the variable i. Note that the Hawk branch instructions only have an 8-bit displacement field, so they can only change the program counter within plus or minus about 128 halfwords.

    a) Give equivalent SMAL/Hawk assembly code, written under the assumption that the unknown code in the loop body takes under 100 machine instructions. (1 point)

    b) Give equivalent SMAL/Hawk assembly code, written under the assumption that the unknown code in the loop body takes over 200 machine instructions. (0.5 point)

  2. Background: Suppose you have a linked list, where each item in the list consists of just one word, the pointer to the next record in the list. To measure the length of this list in C or C++, you might write code like this, assuming that the variable p is a pointer to the first item word in the list:
            i = 0;
            while (p != null) {
                    i = i + 1;
                    p = p->next;
            }
    
    In the following, assume that the variable p is register 3 and that the variable i is register 4. Assume that all items in the list are properly word-aligned in memory.

    a) Give equivalent SMAL/Hawk assembly code. (.5 point)

    b) Give equivalent SMAL/Hawk assembly code assuming that each item in the list is 2 words long, where the second word in each item is the pointer to the next item in the list. (.5 point)

    c) Where the original loop body said i=i+1 change it to i=i+p->value, so that the loop adds up the values of all the items in the list. Each item is 2 words long, as in part b), where the first word of the item is its value and the second word of each item is the pointer to the next item. You will probably need to use a third register to solve this; use register 5. (.5 point)