Assignment 7, Solutions

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

Problems

  1. Background: If you write a file of 32-bit words on a machine that is a highbyter and then read it on a lowbyter machine, you'll get each 32-bit word with the bytes in the wrong order. If you wrote out the word 0123456716 you'd read it back as 6745230116. Note that the order of the bits in each byte is not changed.

    A problem: Write a Hawk subroutine that takes R3 as a pointer to a memory location and reverses the order of the bytes in that location. Your code should follow all of the usual Hawk calling conventions and it should be well documented. (1 point)

    REVERSE:        ; reverse the order of bytes in a word
                    ; given R3 points to the word
                    ; returns with the bytes in that word reversed
    
            LOADS   R4,R3           ; get a copy of the word
            EXTB    R6,R4,R3        ; get byte 0
            SL      R6,8            ;   shift result
            ADDSI   R3,R3,1         ;   advance to next byte
            EXTB    R5,R4,R3        ; get byte 1
            OR      R6,R5           ;   merge byte into result
            SL      R6,8            ;   shift result
            ADDSI   R3,R3,1         ;   advance to next byte
            EXTB    R5,R4,R3        ; get byte 2
            OR      R6,R5           ;   merge byte into result
            SL      R6,8            ;   shift result
            ADDSI   R3,R3,1         ;   advance to next byte
            EXTB    R5,R4,R3        ; get byte 3
            OR      R6,R5           ;   merge byte into result
            STORES  R6,R3           ; put result in memory (ignores R3 bits 0,1)
    
            JUMPS   R1              ; return
    

  2. Background: Consider a null-terminated linked list of strings. Each object in the list contains a pointer to the next object (creatively named NEXT) and a pointer to the text of the string (TEXT). Initially, assume that R3 points to the first object in the list.

    A problem: Write a Hawk program fragment equivalent to the following C-ish code to traverse the list and print the first character of each string in the list, except where the pointer to the string is null and except when the string is of length zero. (1 point)

            while (R3 != NULL) {
                   char * T = (*R3).TEXT;
                   if (T != NULL) {
                          char CH = *T;
                          if (CH != NUL) dspch(CH);
                   }
                   R3 = (*R3).NEXT;
            }
    

    Note: In C, the declaration char * p declares p to be a pointer to a character. The expression *p, in this context, is the character pointed to by p. The notation o.f refers to field f of object o.

    LOOP:
            TESTR   R3
            BZS     LOOPQT          ; while (R3 != NULL) {
            LEACC   R4,R3,TEXT      ;   t = R4 = (*R3).TEXT
            BZS     ENDIFT          ;   if (t != NULL) {
            LOADS   R5,R4
            EXTB    R5,R5,R4        ;      ch = R5 = *t;
            BZS     ENDIFCH         ;      if (ch != NUL) {
            STORES  R3,R2,R3SAVE    ;         -- must save R3
            MOVE    R3,R5           ;         -- put ch in place as parameter
            ADDI    R2,R2,ARSIZE    ;         -- push AR
            LIL     R1,DSPCH
            JSRS    R1,R1           ;         dspch( ch )
            ADDI    R2,R2,-ARSIZE   ;         -- pop ar
            LOADS   R3,R2,R3SAVE    ;         -- must restore R3
    ENDIFCH:                        ;      }
    ENDIFT:                         ;   }
            LOAD    R3,R3,NEXT      ;   R3 = (*R3).NEXT
            BR      LOOP
    LOOPQT:                         ; }
    

  3. Background: Consider this digital logic function, with inputs a and b, and output c.

    x = not( a and b)
    y = not( a and x)
    z = not( x and b)
    c = not( y and z)

    a) Make a truth table showing x, y z, and c as a function of a and b. (0.5 points)
     a  b   x  y  z  c 
    0 0 1 1 1 0
    0 1 1 1 0 1
    1 0 1 0 1 1
    1 1 0 1 1 0

    b) What logic function is this? (0.5 points)

    Exclusive Or