Assignment A14

Due date

This assignment must be submitted to the Moodle submission page for Assignment A14 by 10:00 AM on Monday, 5 December.

You can use whatever format you wish for answering Questions 1 to 3, but you must submit it as an ASCII text file. MS Word 2007 or Open Office will save in this format. Question 4 should be submitted as a C program that is included within the text file.

The references

Use the rules discussed in the handouts on Translating C to LC-3: Variables, Translating C to LC-3: Functions, Translating C to LC-3: Control structures, and Translating C to LC-3: Expressions to perform the tasks of this assignment.

The example

In this question you are going to use the following function.

int gcd(int m, int n) {
  int t ;
  while (m != n) {
    if (m < n) {
      t = m ;
      m = n ;
      n = t ;
    } else {
      m = m - n ;
    }
  }    
  return(m) ;
}

The tasks

Question 1

Write a simple table appropriate for the function gcd that follows the calling conventions used in class.

Question 2

Suppose the gcd function is called using the following statement:

x = gcd(ferd, arie) ;

Assuming that ferd is at offset 5 from R5 and arie is at offset 7 from R5 and write appropriate function invocation and function return code sequences in LC-3 assembler for this call. This question involves little more than a cut-and-paste from the functions handout followed by a few substitutions.

Question 3

Write appropriate function prologue and epilogue code sequences in LC-3 assembler for this call. This is the code that will be included in the compilation of gcd.

Again, this question involves little more than a cut-and-paste from the functions handout.

Question 4

“Compile” the body of the gcd function into simplified C in which (1) at most one operator appears on the right hand side of an assignment statement and (2) the only "control" structures are one of the following three:

Your program should be in legal ANSI C format.