This lab is scheduled for 1 and 2 May.
This week we'll look at:
Begin by executing the following commands to create a directory csci/255/lab11 and copy two files into it.
The copied file is the skeleton of an LC-2 program. You'll finish it during the lab.
By the end of this lab you should have an LC-2 program, stored in lab11.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) ;
}
}
}
You've copied two LC-2 programs into your directory: lab11crt0.asm and lab11main.asm. You do not modify lab11crt0.asm, however you will need to assemble it.
The file lab11crt0.asm contains
definitions for the English
and
Francais
data structures,
code for the ah2i
routine, and
a small startup routine that will call your main routine
at location x3000.
When your main routine is called, R6 will point to a stack frame for your routine. R5 will point to a global object table. This global table has four entries:
0 | address of your main routine (x3000) |
1 | address of a2hi routine |
2 | address of English array |
3 | address of Francais array |
When you need to access any of the above items, you will
do so by using the LC-2 ldr
instruction with the
appropriate offset from register R5
.
Your job is to write the main routine.
If your state with the code of the file lab11main.asm,
you are left with the task of writing the code for the loop
of the main
routine.
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
of the file lab11crt0.asm.
ENG
is the address of an
array containing sixteen strings.
For example, 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
.
Write and test lab11main.asm and don't delete your file!