Homework 4

22C:18, Fall 1996

Due Tuesday Sep. 24, 1996, in discussion

Douglas W. Jones

Notice: MP1, the first machine problem, is due on Oct. 1! Exam 1 is Oct 4! There will be no class Mon. Sept. 23 (Yom Kippur) and I will not check my E-mail between late Sunday afternoon and late Monday evening.

  1. Write appropriately commented SMAL Hawk code that is as close as you can come to being equivalent to the following little C program fragment:
    	unsigned int t = 100;
    	while (t > 1) {
    		if (t & 1) {
    			t = (t * 3) + 1;
    		} else {
    			t = t / 2;
    		}
    	}
    	output('STOP');
    
    (Aside: The above program fragment does terminate for every positive integer initial value of t that anyone has ever tried, but nobody has ever proven that it terminates for all positive integers.)

  2. Consider the statement t = (t * 3) + 1 from the above. Assuming that t is represented by R1. this could be done with the following SMAL Hawk code:
               ADDS  R1,R1,1
               ADDSI R1,1
    
    Modify the code form problem 1 to detect overflow and output 'ERR!' instead of the 'STOP' the code outputs normally. Overflow, in this case, means that the result of multiplication or addition was greater than or equal to 2 to the 32, since we're only concerned with unsigned values.

    You may turn in one program listing incorporating problem 2 into your solution for problem 1.

  3. Part A: Translate the following C declaration of an array of two-element records to appropriately commented SMAL Hawk assembly language:
    	struct rec {
    		int x,y;
    	} z[8];
    
    Part B: In terms of the above and your answer to part A, translate the following code fragment to SMAL Hawk assembly language:
    	for (i=0;i<7;i++) {
    		z[i].x = z[i+1].y;
    	}