Homework 5

22C:18, Fall 1996

Due Tuesday Oct. 1, 1996 in discussion

Douglas W. Jones

For this homework assignment, consider the following multiplication, procedure, coded in classic K&R C.

		(int) times( a, b )		
		  int a;     /* multiplier */
		  int b;     /* multiplicand */
		{
		  int c = 0; /* product */
		  int i;
		  if (a & 0x80000000) {
		    c = -b;  /* handle the sign bit */
		  }
		  for (i=31; i>0; i--) {
		    c <<= 1; /* handle the rest */
		    a <<= 1;
		    if (a & 0x80000000) {
		      c += b;
		    }
		  }
		  return c;
		}
In responding to the following questions, please confine yourself to "simple brute force" translation of this code to assembly code. Don't try to optimize! Don't try to make effective use of registers!
  1. Write appropriately commented SMAL Hawk code that is as close as you can come to being equivalent to the following small main program:
    		int e;
    		     
    		(void) main()
    		{
    		  e = 12;
    		  e = times( 5, e );
    		  exit(0);
    		}
    

  2. Write appropriately commented SMAL Hawk code for the procedure itself!

  3. Write SMAL Hawk macros analogous to the Hawk instructions LOADCC, STORE, and LOADSCC and STORES (all of which load and store 32 bit quantities) that operate on 16 bit halfwords. These should be called LDHCC, STH, LDHSCC and STHS (ugh).

  4. In some ways, macros and procedures do the same thing. In fact, historically, macros were sometimes called open subroutines while procedures were called closed subroutines. Under what circumstances must you use one? Under what circumstance must you use the other? Can you propose rules of thumb to help you select one or the other in those circumstances where either can be used?

  5. Hand translate the following bit of SMAL code to Hawk machine language, showing your result in hexadecimal as a sequence of address-value pairs, where the address shows the location in Hawk memory where the corresponding 32 bit value should be stored.
    		. = #200
    	DISPPTR = #10000
    	;------------------------
    	DSPCH:                  ; output char in R3
    				; link through R1
    				; wipes out R4-5
    		LIL     R4,DISPPTR
    		LOADS   R5,R4   ; get display pointer
    		ADDSI   R5,1
    		STORES  R5,R4   ; save updated pointer
    		LOADS   R4,R5
    		STUFFB  R4,R3,R5
    		STORES  R4,R5   ; update display
    		JUMPS   R1      ; return
    
    Note, you can check your results by using the assembler and Hawk emulator, but you'll want the experience of doing this by hand for the upcoming exam!