CS/ECE 5785/6785: Homework 2

(back to: main 5785 page)


You must solve the following problems on your own (not in groups). You may discuss the problems with other students but you must not discuss specific solutions, borrow code from another student, look at another student's code, look for code on the web, or ask for solutions on mailing lists or message boards. All of these are cheating.

Part 1

This question refers to ``Getting the least out of your C compiler.'' When you compile code, compile it using the CodeWarrior compiler for Coldfire. Turn on as many optimization options as possible. Put all of the C code that you write for this problem into a single file called least.c
  1. Write two C functions, each taking a short argument and returning a char, that implement the logic in the code examples from Section 4.2. Answer the following two questions in your homework. Is the generated code different? If so, is it harmlessly different, or is one of them actually worse? Why? Also hand in commented versions of the two assembly language functions output by the compiler, where you explain the effect of each instruction. Don't state the obvious (i.e., ``add instruction adds two numbers'') but rather explain why the instructions together implement the desired functionality. If the compiler emits the same code for both of your functions, then of course you only need to explain one of them.
  2. Do the same thing for the second set of code examples in Section 4.9. The function foo should take a pointer to char as argument and return an int. You should not write this function, just give the compiler a prototype.
  3. Do the same thing for the code described in Section 4.10. You should write two functions, one containing a loop counting up to 100, one counting down. The loops should be summing up the elements of a 100-element global array. Each function takes no arguments and returns an int (the sum).

Part 2

Saturating arithmetic operations are those where overflowing results "stick" at the maximum or minimum representable result. In other words, if you are using saturating addition and subtraction operators, (INT_MAX + 1) == INT_MAX and (INT_MIN - 1) == INT_MIN. See this header file. It provides the prototypes for four saturating arithmetic operations that you must implement. Your functions should live in a file called sat_ops.c that you create. Some other requirements:

What to Hand In

This assignment is due BEFORE CLASS on Thursday September 23.

This homework requires electronic handin of two C files, each answering one of the parts. The handin directory is "hw2". See the submission instructions for more details.

This page is maintained by John Regehr, mail me if you find a mistake or if any content is unclear.