Assignment 7, due Oct 17

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

Remember to write your name on what you turn in! Homework must be turned in on paper and in class!

Homework

  1. Background: The C and C++ programming languages support two distinct 8-bit data types, signed char and unsigned char (just to make things complicated, the type char has sometimes been ambiguously defined as one or the other, whichever is convenient). Consider this code fragment:
            unsigned char a[10];  /* an array of 10 unsigned bytes */
            signed char   b[10];  /* an array of 10 signed bytes */
            int i; /* the loop index */
    
            ... /* some intervening code */
            
            for (i = 0; i < 10; i++) if (b[i] < 0) a[i] = 0;
    

    a) The arrays A and B are the only local variables of the current subroutine. Write appropriate SMAL code for the activation record of the subroutine. You can safely assume that the activation record also contains a return address. (0.5 points)

    b) Translate the above for loop to SMAL Hawk assembly language. You can safely use all registers except R2 to hold your variables. The loop control variable i should clearly be in a register. (1.0 points)

  2. Background: Toward the end of Chapter 7, code is given for a fast version of STRLEN. Consider the problem of writing a fast version of STRNCPY that uses the same tricks. This question asks you to reason about that code without writing it!

    Note in the fast version of STRLEN, there is a prefix (with labels SLNB-), and then a very short loop SLNLP), and then a somewhat longer suffix, after the label SLNLX).

    a) The code for STRLEN had 4 special cases in its prefix. How many special cases will there be in the improved version of STRNCPY? Give a concise explanation of your answer, don't just give a naked integer. (0.5 points)

    b) In the code for the improved version of STRNCPY, there must be multiple independent loop bodies for each of several special cases. Enumerate and very briefly describe the special cases. (0.5 points)

    c) Give the code for the fastest of the loop bodies identified in part (b). The loop entry point can be SCPL0 and the exit can be to SCPX0. Assume R4 and R3 point to the source and destinatio string addresses, and that R5 is the destination string length. You may safely use R1 as a temporary. (0.5 points)