Fall 2009: ECE 209 C to LC3 vol. I -- simplified

Here are the five C functions we'll use in our initial set of LC3 translation assignments.

Swap integers

void swap(int *a, int *b) {
  int t ;
  t = *a ;
  *a = *b ;
  *b = t ;
}

Swap integers within array

void Swap2and3(int a[]) {
  int *α1, *α2 ;
  α1 = a+2 ;
  α2 = a+3 ;
  swap(α1, α2) ;
}

Find biggest odd integer in array

int findMostOdd(int a[], int n) {
    int r = 0 ;
    int i ;
    int α1, α2 ;

    i = 0 ;
    goto λ3 ;
  λ0:
    α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:
    ++i ;
  λ3:
    if (i<n) goto λ0 ;
    return r ;
}

Slow Fibonacci

int badFib(int n) {
    int α0, α1, α2 ;
    if (n > 1) goto λ1 ;
    α0 = 1 ;
    goto λ0 ;
  λ1:  
    α1 = n-1 ;
    α1 = badFib(α1) ;
    α2 = n-2 ;
    α2 = badFib(α2) ;
    α0 = α1 + α2 ;
  λ0:
    return α0 ;
}

Fetch and Add

static int accumulator = 0 ;

int FetchAndAdd(int y) {
   accumulator = accumulator + y ;    
   return accumulator ;
}