Homework 3

22C:18, Summer 1997

Due Friday, June 27

Douglas W. Jones
  1. Write an appropriately commented SMAL Hawk assembly language program that is as close as you can come to being equivalent to the following C program:
    main ()
    {
    	int t = 50;
    	while (t > 1) {
    		if (t & 1) {
    			t = (t * 3) + 1;
    		} else {
    			t = t / 2;
    		}
    		printf("%d ",t);
    	}
    	printf("STOP");
    }
    
    You will find a solution to this problem in the solutions given in the fall 1996 offering of this course. Do not use that solution! Instead, use a solution that makes effective use of the output routines provided by the Hawk monitor. These routines make the above code far simpler to write than a full fledged machine problem would have been! Don't forget, calls to the Hawk system routines use R1 through R7, while they guarantee no damage to the contents of R8 and up.

    (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. Note that we declared t as being type int in the above code, so overflows involve results of 231 or more.

    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 {
    		long int x;
    		char y,z;
    	} z[4];
    
    Part B: In terms of the above declarations and your answer to part A, translate the following nonsensical code fragment to SMAL Hawk assembly language:
    	for (i=0; i < 3; i++) {
    		z[i].x = z[i+1].y + z[i+1].z;
    	}
    
  4. Write an appropriately documented HAWK macro to load a halfword from memory, with no alignment constraings. Here is a macro that solves the same problem, assuming that the addressed halfword is aligned properly:
    	MACRO	LOADHS	=dst,=x
    	  LOADS	dst,x
    	  EXTH	dst,dst,x
    	ENDMAC
    
    The problem you must solve (other than worrying about macro usage) is that the R[x] is not constrained to point to a properly aligned halfword, but may point to any byte in memory, and you must make your code fetch that byte and its successor into r[dst], sign extending the result. Full credit will depend on whether or not your code properly sets the N and Z condition codes to report on the value loaded in r[dst].

    You will probably need a scratch register; the convention of using R15 for this purpose is encouraged. This problem can be solved in under 10 instrucitons. Typically, the only time you would expect to encounter non-aligned halfwords or words in a Hawk computer is when reading data that was tightly packed, for example, for storage on disk or transmission over the network.

  5. By hand (using the information in the SMAL and Hawk manuals), assemble the following code that calls the macro you defined in the previous problem:
    	LIL	R5,#00012345
    looptop:
    	LOADHS	R6,R5
    	BZS	loopexit
    	ADDSI	R5,2
    	BR	looptop
    loopexit:
    
    Present your result as a string of consecutive halfwords, in hexadecimal. You may check your result using the assembler, but you must be able to produce the values by hand -- a problem very like this problem will appear on the exam!