Assignment 6, due Oct. 2

Part of the homework for 22C:60, Fall 2009
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!

Problems

  1. Background: The Hawk monitor includes a routine, DIVU, that returns the quotient (and remainder) after division of one unsigned integer by another. The operands are 32-bit unsigned integers. There is no DIV routine in the monitor for taking the quotient of signed integers. The Hawk monitor really would be nicer if it also contained this routine (specified in the style of the interface specifications in monitor.h)
            EXT     DIV     ; divide two signed integers
                            ;   takes R3=dividend R4=divisor
                            ;   returns R3=quotient
                            ;   may wipe out R4-7
    

    Also, recall that, in elementary school math, we were taught that:

    	(-A)/B = -(A/B)
    	A/(-B) = -(A/B)
    	(-A)/(-B) = A/B
    

    (There are real problems with the above definition of division involving negative integers, but the above will suffice for now).

    A problem: Write a subroutine that conforms to the above interface spec. It should operate by taking the absolute value of any negative operands, then dividing by a call to DIVU, and finally, negating the quotient if the signs of the operands require this. (1.5 points)

    Note. The logic of the program would be trivial in a high level language, just 3 if statements and a subroutine call. The hard part is that it involves one subroutine calling another, and it involves local variables. You are not required to assemble or run your code, just turn in legible, correct and appropriately commented assembly language for the subroutine itself.

  2. Background: Here is a stupid way to add two unsigned integers:
            unsinged int add( unsigned int a, unsigned int b )
            {
                unsigned int sum;
                if (b = 0) {
                    sum = a;
                } else {
                    sum = add( a + 1, b - 1 );
                }
                return sum;
            }
    

    A problem: Write equivalent SMAL Hawk code. (1.5 points)

    Note. This problem is actually easier than part a, but it is recursive. Small optimizations to your code can markedly improve readability. You are not required to assemble or run your code, just turn in legible, correct and appropriately commented assembly language for the subroutine itself.