Assignment 5 solutions

Part of the homework for 22C:50, Summer 2004
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Do problem 12 in Chapter 5, but assume a stack instruciton set.

    12) Modify the parser shown in Figure 5.9 so it generates output in this assembly language.

    		procedure expression;
    		var more: boolean;
    		    op: char;                             {--added--}
    		begin
    		     op = ' '; {initially, no operator }  {--added--}
    		     repeat
    			  if lex = '(' then begin
    			       lex_scan;
    			       expression;
    			       if lex = ')' then lex_scan else error;
    			  end else begin
    			       value;
    			  end;
    			  case operator of                {--added--}
    			   ' ': {no operation!};          {--added--}
    			   '+': writeln(' ADD');          {--added--}
    			   '-': writeln(' SUB');          {--added--}
    			   '*': writeln(' MUL');          {--added--}
    			   '/': writeln(' DIV');          {--added--}
    			  end;                            {--added--}
    			  more := lex in ['+','-','*','/'];
    			  op = lex;                       {--added--}
    			  if more then lex_scan;
    		     until not more;
    		end {expression};
    

  2. Write a set of macros that allow a C programmer to write code in this horrible notation:
    assign(i,times(plus(i,1),times(j,2)));
    
    		#define assign(var,val) var = (val)
    		#define times(v1,v2) ((v1) * (v2))
    		#define add(v1,v2) ((v1) + (v2))
    

  3. The fictional Ultimate RISC computer has one machine instruction, move, which moves an operand from the source address to the destination address. What the programmer wants to be able to write for this instruction is:
    	MOVE src,dst
    

    The result should be identical to having written this:

    	W    src
    	W    dst
    
    Write a macro, using the macro notation presented in Chapter 6, that will produce this result.
    		MACRO MOV,src,dst
    		  W     src
    		  W     dst
    		ENDMAC