Assignment 7, Solutions

Part of the homework for 22C:60 (CS:2630), Fall 2011
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of class on the day indicated (usually Friday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: Consider the "string compare" operation:
    	strcmp(a,b)
    

    This compares two strings a and b. If they are equal, it returns zero. If the first string is greater than the second, it returns a positive value. If the first string is less than the second, it returns a negative value. The return value of zero occurs when all characters are equal up to and including the final NUL marking the end of string. The nonzero return values indicate whether the first character where the two strings differ is greater or less ...

    A problem: Write SMAL Hawk code for strcmp(). Use the standard Hawk calling conventions, so pointers to the strings a and b will be passed in R3 and R4, and the result should be returned in R3. (1.0 points).

    STRCMP: ; Expects R3, R4 pointers to two strings s1 and s2
            ; Returns R3>0 if s1 > s2
            ;         R3=0 if s1 = s2
            ;         R3<0 if s1 < s2
            ; Uses R5,R6 as temporaries, wipes out R4
    STRCLP:                         ; loop {
            LOADS   R5,R3
            EXTB    R5,R5,R3        ;   c1 = *s1
            LOADS   R6,R4
            EXTB    R6,R6,R4        ;   c2 = *s2
            SUB     R5,R5,R6        ;   d = c1 - c2
            BZR     STRCQT          ;   if (d != 0) break
                                    ;   -- assert c1 = c2
            TESTR   R6
            BZS     STRCQT          ;   if (c2 == 0) break
            ADDSI   R3,1            ;   s1++
            ADDSI   R4,1            ;   s2++
            BR      STRCLP
    STRCQT:                         ; }
            MOVE    R3,R5
            JUMPS   R1              ; return d
    
  2. Exercises from Chapter 8 of the Notes:

    a) Do exercise a). Work this out by: First, drawing the schematic diagram and labeling the output of each gate. Second, writing out a truth table with one output column for each labeled point in the circuit. Work it through to the final output and then compare this with each of the known functions of two inputs. (0.5 points).

    abxyzc
    001000
    011011
    101101
    110000

    Therefore, c = ab -- this is an exclusive or gate

    g) Follow the instructions. Note (emphatically) that the borrow signal from one bit to the next signals the need to borrow. So, it is an output from each less significant bit to the next more significant bit, just like carry. (0.5 points)

    aibi bi+1di
    0000
    0111
    1001
    1100

    k) (0.5 points).

    a3b3s3V
    0000
    0011
    0100
    0110
    1000
    1010
    1101
    1110

    l) (0.5 points).