Fall 2009 ECE 209 Homework 6

Example programs

First look in the C to LC3 example programs we'll use for the first set up C to LC3 translation examples.

Due dates and ground rules

This assignment is due on 29 October. You can turn in a written copy in class or you can email your homework to brock@cs.unca.edu.

You may work with a partner on the LC3 to C assignments, but you must change your partner between assignments.

The task

Translate the five C programs on the C to LC3 page from real C to a very primitive form of C where (1) no if, for, or while statements are allowed except for an if with a single goto as its then case; (2) the expression on the right-hand side of an assignment statement has at most one operator; and (3) all arguments to functions are single variables.

Here is an example of doing this for the hardest part of the C examples, the if in the middle of the for for finding the largest odd element of an array.

  {
    if ((a[i] & 1) && (r==0 || a[i] > r))
       r = a[i] ;
  }
  {
     int α1, α2, α3, α4 ;
     α1 = a[i] ;
     α2 = α1 & 1 ;       /* that's the AND instruction */
     if (α1 == 0) goto λ2 ;
     α3 = r==0 ;
     if (α3 != 0) goto λ1 ;
     α4 = α1 > r ;
     if (α4 == 0) goto λ2 ;
    λ1:
     r = α1 ;
    λ2:
  }    

The rest of the homework is much easier than this. By the way, you can "optimize" the reduced C a bit more.

  {
     int α1, α2 ;
     α1 = a[i] ;
     α2 = α1 & 1 ;       /* that's the AND instruction */
     if (α1 == 0) goto λ2 ;
     if (r  == 0) goto λ1 ;
     if (α1 <= r) goto λ2 ;
    λ1:
     r = α1 ;
    λ2:
  }