# Spring 2001 CSCI 255 Lab 12

This lab is scheduled for the week of 16 April - 19 April.

## Goals and Methods

This week we'll look at:

• Translating C code to LC-2 assembler
• Using pointers and arrays

## Getting started

Begin by executing the following commands to create a directory csci/255/lab12 and copy one file into it.

• mkdir -p csci/255/lab12
• cd csci/255/lab12
• cp /usr/local/csci/255/lab12/lab12.asm .

The copied file is the skeleton of an LC-2 program. You'll finish it during the lab.

## The program

By the end of this lab you should have a LC-2 program, stored in lab12.asm, that prompts the user five times for a single hexadecimal digit ('0' to '9', 'a' to 'f', or 'A' to 'F') and then prints the English and French (Anglais et Français) words for that number. Here's an example of what should happen when you give the program three of those five numbers:

```
Enter the hexadecimal number: C
twelve
douze

Enter the hexadecimal number: b
eleven
ouze

Enter the hexadecimal number: 0
zero
zéro

```

And, here's an example of a C program to solve the problem:

``````
#include <stdio.h>

char *English[16] = {
"zero",   " one",       "two",      "three",
"four",     "five",     "six",      "seven",
"eight",    "nine",     "ten",      "eleven",
"twelve",   "thirteen", "fourteen", "fifteen"
} ;

char Francais[16][9] = {
"z\xe9ro",  "un",       "deux",     "trois",
"quatre",   "cinq",     "six",      "sept",
"huit",     "neuf",     "dix",      "onze",
"douze",    "treize",   "quatorze", "quinze"
} ;

int ah2i(int x) {
if ('0' <= x && x <= '9')
return x - '0' ;
if ('A' <= x && x <= 'F')
return x - ('A' - 10) ;
if ('a' <= x && x <= 'f')
return x - ('a' - 10) ;
return -1 ;

}

main(int argc, char *argv[]) {
int i, c, b ;
for (i=5; i != 0; --i) {
fputs("\nEnter the hexadecimal number (0-9,A-F): ", stdout) ;
c = getc(stdin) ;
b = ah2i(c) ;
if (b == -1)
fputs("\n?\n", stdout) ;
else {
fputc('\n', stdout) ;
fputs(English[b], stdout) ;
fputc('\n', stdout) ;
fputs(Francais[b], stdout) ;
fputc('\n', stdout) ;
}
}

}
```
```

## The starting point

You've copied an LC-2 program into the file lab12.asm in your directory. This program contains definitions for the `English` and `Francais` data structures, named `ENG` and `FRN` for the LC-2, along with the code for the `ah2i` routine.

You are left with writing the code for the loop of the `main` routine. However, there is one problem. Note the slight, but significant, difference between the types of the `English` and `Francais` arrays:

• `char *English[16]`
• `char Francais[16][9]`

The C variable `English` is an array of sixteen pointers to strings. The C variable `Francais` is a sixteen by nine array of characters. This difference is reflected in the LC-2 variables `ENG` and `FRN`. `ENG` is the address of an array containing sixteen strings. The address of the English word for the number 5 can be found in the 5'th entry of `ENG`.

On the other hand, `FRN` is a two-dimensional array. It contains sixteen entries, but each entry is nine characters long. The address of the French word for the number 5 is 45 (or 5 times 9) memory locations from the beginning of `FRN`.

You must keep this difference in mind when you are generating the addresses sent to the `PUTS` trap routine to print the English and French words. By the way the easiest way to compute `9*X` on the LC-2 is as `X<<3+X`.

## Going home

Write and test lab12.asm and don't delete your file!