Assignment 7, due Oct 15

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

Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list! All assignments will be due at the start of class on the day indicated (usually a Friday). The only exceptions to this rule will be by advance arrangement unless there is what insurance companies call "an act of God" - something outside your control. Homework must be turned in on paper and in class! Late work may be turned in to the teaching assistant's mailbox, but see the late work policy. Never push late work under someone's door!

Homework

  1. Background: Consider the problem of comparing two stirngs. In C or C++, you can write a string compare function as follows:
    int strcmp(char * a, char * b) {
        while ((*a != NUL) && (*a == *b)) {
           a++;
           b++;
        }
        return (*a - *b);
    }
    

    The two parameters to this function, a and b are pointers to two strings null-terminated strings. In C and C++, that is the same as saying that a and b are pointers to the first characters of those strings. Recall that, in C and C++, if a is a pointer to a character, *a is the character it points to.

    a) What value does the above function return for a call to strcmp(x,y) if the string x is equal to the string y, if the string x comes lexicographically before y, and if the string x comes lexicographically after y. (0.5 points)

    b) Rewrite the above C code in Hawk assembly language. You should try to optimize things so that each iteration of the loop only fetches *a and *b once and only subtracts once. (1.5 points)

  2. Background: Consider this code fragment:
    /* precondition: s points to a null terminated string */
    while (*s != 0) {
        if (*s == ' ') *s = '-';
        s++;
    }
    /* postcondition: spaces in the string have been changed to dashes */
    

    Problem: Translate the above code to Hawk assembly language. Assume that the variable s is R3 and feel free to use all registers from R3 up.