\documentclass[12pt]{article}
\evensidemargin 0.50in \textwidth 5.75in

\topmargin 0in \headsep .325in \textheight 8.25in

\def\myfig#1#2#3{
 \begin{figure}[ht]
 \special{isoscale #1, \the\hsize 3.0 in}
 \vspace{3.0in}
  \caption{#2}
\label{#3}
 \end{figure}
}

\begin{document}
\title{Tables}
\date{}
\author{N. Narasimhamurthi}
\maketitle
\section{Objective}
To become familiar with table driven code.

\section{What you should do} You will have to turn in your {\tt
LST} files for the programs you write for this lab. Check with the
TA for additional instructions. In most of the examples, the name
is left blank as \verb+_____________+. Make sure you enter your name
in its place.

\section{Tables}
What is a table? Technically, table is same as an array except we
use the term {\em table} to refer to arrays whose elements are
constants. To operate on the table, we need two pieces of
information, {\em where, in memory, the table starts( {\bf the
starting address}) }, and {\em how many elements are in the table (
{\bf the size or dimension})}. In this lab, we will initially
consider the case where each element requires one byte of memory.
\subsection{Setting up a table}
To setup a table in memory, we generally use {\tt FCB}, {\tt FDB}
and {\tt FCC}. For example, if we want to setup a table of prime
numbers, we would write (using decimal since it is easier to
read):
\begin{verbatim}
...
    ORG $3000 *data section
...
...
primes fcb 2,3,5, 7, 11, 13, 17, 19, 23, 29 *table of some primes
nprimes equ 10 *number of entries in the table
\end{verbatim}
If you want to set up a table of ascii code for digits, you would write
\begin{verbatim}
...
    ORG $3000 *data section
...
...
digits1 fcb  $30, $31, $32, $33, $34, $35, $36, $37, $38, $39
ndigits1 equ 10 *number of entries in the table
\end{verbatim}
A {\bf better} way to do the same is to write
\begin{verbatim}
...
    ORG $3000 *data section ..$
...
...
digits fcc  /0123456789/
ndigits equ 10 *number of entries in the table
\end{verbatim}
Assembler will convert {\tt FCC} to a sequence of {\tt FCB}.
\paragraph{Exercise:} Write an ASM file with the two versions of the digits
tables, assemble the file and look at the {\tt LST} file. Verify that the two
tables are identical.
\subsection{Working with tables}
We will write functions to perform some basic tasks with the
tables. In all these functions, we will use the following
convention:
\begin{enumerate}
\item The starting address will be passed to the function in the {\bf X} register.
\item The size of the table will be passed to the function in the {\bf B} register.
\end{enumerate}
\subsubsection{Table lookup}
This is the simplest and the most useful function. We want to know
if an element is in the table. For now, we will work with a table
of 8-bit quantities. The value to be looked up will be passed in
the {\bf A} register. The function will have to return a {\tt
Yes/No} value. A convenient way to return a {\tt Yes/No} value is
to use a hardware flag. Let us use the {\bf C}arry flag. The
function will set the flag if the answer is yes; or else it will
clear the carry.
\begin{verbatim}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; lookup: Function to lookup a value in a table
;         Checks if the value in A register is in the table
;
; Entry: Starting address in X, size in B, value in A
; Exit:  Carry set if the value in A is in the table;
;              cleared if not in the table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

lookup
lkploop
        tstb
        beq     notthere *this is the basic counting loop
        cmpa    0,x
        beq     foundit
        inx
        decb
        bra     lkploop
notthere
       clc
       rts
foundit
       sec
       rts
\end{verbatim}
Here is a program that uses the function:
\begin{verbatim}
;Name:
;email:
;date:
;
; Standard buffalo equates
; Make sure you have ALL the equates in the file.
;

ucase       equ $ffa0
wchek       equ $ffa3
dchek       equ $ffa6
init        equ $ffa9 * use with caution
input       equ $ffac
output      equ $ffaf
outlhlf     equ $ffb2
outrhlf     equ $ffb5
outa        equ $ffb8
out1byt     equ $ffbb
out1bsp     equ $ffbe
out2bsp     equ $ffc1
outcrlf     equ $ffc4
outstrg     equ $ffc7
outstrgo    equ $ffca
inchar      equ $ffcd
vecinit     equ $ffd0

    org $3000
; setup some strings ...
preamble
    fcc /=====================================/
    fcb 10
    fcc /Lab on using Tables/
    fcb 10, 10 *use 10 to start a new line
    fcc /Name: _____________________/
    fcb 10
    fcc /This program is an infinite loop! /
    fcb 10
    fcc /Hit the reset button to quit/
    fcb 10
    fcc /=====================================/
    fcb 10,10,10, 4

yesstr  fcc /  is a vowel/
    fcb 10, 4
nostr   fcc /  is not a vowel/
    fcb 10, 4

; setup the table of vowels

vowels  fcc /aeiouAEIOU/
nvowels equ 10

    org $2100
    ldx #preamble
    jsr outstrg
mainloop
    jsr inchar
    ldx #vowels
    ldab #nvowels
    jsr lookup
    bcs isvowel
    ldx #nostr
    jsr outstrgo
    bra mainloop
isvowel ldx #yesstr
    jsr outstrgo
    bra mainloop


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; lookup: Function to lookup a value in a table
;         Checks if the value in A register is in the table
;
; Entry: Starting address in X, size in B, value in A
; Exit:  Carry set if the value in A is in the table;
;              cleared if not in the table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

lookup
lkploop
        tstb
        beq     notthere *this is the basic counting loop
        cmpa    0,x
        beq     foundit
        inx
        decb
        bra     lkploop
notthere
       clc
       rts
foundit
       sec
       rts
\end{verbatim}
\paragraph{Exercise:} Type the above program, assemble it, transfer the {\tt S19} file
to the 68HC11, and run the program with {\tt CALL 2100}. When the
program starts, type the following text {\tt Pack my box with five
dozen liquor jugs}.
\paragraph{Exercise:} The lookup program affects the {\bf X} and {\bf B} registers.
Modify the function so that the function initially stores these two registers
in the stack, and restores them before returning. Verify that the program works
correctly.
\paragraph{Exercise:} Modify the program by adding
another table, the table of symmetric
characters: {\tt AHIMOTUVWXYimnouvwxy}. The program should, in
addition to checking to see if a character is a vowel, it should
also check to see if it is a symmetric character. Run the program
and enter the following text: {\tt Axiomboard}. You should get an
output similar to the following:
\begin{verbatim}
 done
 >c 2100

 =====================================
Lab on using Tables

Name: _____________________
This program is an infinite loop!
Hit the reset button to quit
=====================================


A  is a vowel and is a symmetric character
x  is not a vowel and is a symmetric character
i  is a vowel and is a symmetric character
o  is a vowel and is a symmetric character
m  is not a vowel and is a symmetric character
b  is not a vowel and is not a  symmetric character
o  is a vowel and is a symmetric character
a  is a vowel and is not a  symmetric character
r  is not a vowel and is not a  symmetric character
d  is not a vowel and is not a  symmetric character
\end{verbatim}
\subsubsection{Input with validation}
Another use of the table lookup is to validate input from the user. Suppose
we want the user to enter a social security number. In this case, we want to make
sure that we accept only digits and ignore non-digits (the user may be in the
habit of typing spaces, dashes etc. You want to silently ignore these). So it will be
useful to write a function, called {\tt rddigit} that will accept only digits.
The following program shows a typical usage:
\begin{verbatim}
;Name:
;email:
;date:
;
; Standard buffalo equates
; Make sure you have ALL the equates in the file.
;

ucase       equ $ffa0
wchek       equ $ffa3
dchek       equ $ffa6
init        equ $ffa9 * use with caution
input       equ $ffac
output      equ $ffaf
outlhlf     equ $ffb2
outrhlf     equ $ffb5
outa        equ $ffb8
out1byt     equ $ffbb
out1bsp     equ $ffbe
out2bsp     equ $ffc1
outcrlf     equ $ffc4
outstrg     equ $ffc7
outstrgo    equ $ffca
inchar      equ $ffcd
vecinit     equ $ffd0

    org $3000
; setup some strings ...
preamble
    fcc /=====================================/
    fcb 10
    fcc /Lab on using Tables/
    fcb 10, 10 *use 10 to start a new line
    fcc /Name: _____________________/
    fcb 10
    fcc /This program is an infinite loop! /
    fcb 10
    fcc /Hit the reset button to quit/
    fcb 10
    fcc /=====================================/
    fcb 10,10,10, 4


; setup the table of digits

digits  fcc /0123456789/
ndigits equ 10


    org $2100
    ldx #preamble
    jsr outstrg
mainloop
    jsr rddigit
    bra mainloop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; rddigit: Behaves like inchar, except ignores non-digits
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

rddigit jsr input *This does not wait for the user
    tsta
    beq rddigit *Looks like the user has not typed anything
;
; if we get here, the user typed something. Verify it first
;
    ldx #digits
    ldab #ndigits
    jsr lookup
    bcc rddigit *oops, not in the table. Go back for more
;
; if we get here, the input was ok
; echo it back as the user would like some feedback
;

    jsr     outa
    rts


;;; Add the code for lookup function here

\end{verbatim}
\paragraph{Exercise:} Type the above program and run it. Enter the following input and
explain what you see:\paragraph{Exercise:} Type the above program
and run it. Enter the following input and explain what you see:
{\tt 123-34-1879}.

\paragraph{Exercise:}
Modify the above program so that it accepts exactly 10 digits and
stops with an {\tt SWI} after reading 10 characters. In other
words, convert the main loop into a counting loop. {\em You should
keep the count in the {\bf B} register}.  Verify your program with
the input: {\tt 823--xx-34-1879}.
\subsubsection{Translations using tables}
Some time we want to translate a value to another value. For
example if the user types the character {\tt 8}, your program will
receive the ascii code for the character. You now will have to
convert it to its value, viz {\tt 8}. This is easy since you just
have to subtract {\tt \$30}. However, if the user enters data in
HEX, then he would expect the program to translate {\tt A} and
{\tt a} to 10, {\tt B} and {\tt b} to 11 and so on. For problems
of this nature, we first write down the translation table
\begin{center}
\begin{tabular}{|l|l|}
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \hline
  value & translation \\ \hline
  \$30 or '0' & 0 \\
  \$31 or '1' & 1 \\
  \$32 or '2' & 2 \\
 etc. & etc\\
  \$39 or '9'& 9 \\
  \$41 or 'A' & 10 \\
  \$61 or 'a' & 10 \\
   \$42 or 'B' & 11 \\
  \$62 or 'b' & 11 \\
   etc. & etc\\ etc. & etc\\
 \hline
\end{tabular}
\end{center}
In our assembly program we set up two tables. It is extremely
important that the two tables be ordered as follows: The table of
values first immediately followed by the table of translations.
For example, to perform  the above translation, we will write
\begin{verbatim}
hexchars fcc /0123456789AaBbCcDdEeFf/
hextrans fcb 0,1,2,3,4
         fcb 5,6,7,8,9
         fcb 10,10, 11,11, 12,12, 13,13, 14,14, 15,15
nhexchars equ 22
\end{verbatim}

To illustrate the use of the translation table, let us write a
function that will translate telephone numbers. For example, if the
input to the program is a mixture of numbers and letters as in
{\tt 1-800-CALLATT} the program should
translate it to  {\tt 1-800-2255288}. The translation table can be
found on any telephone and is:
\begin{center}
\begin{tabular}{|l|l|}
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \hline
  value & translation \\ \hline
  'A', 'B', 'C' & 2 \\
 'D', 'E', 'F' & 3 \\
'G', 'H', 'I' & 4 \\
'J', 'K', 'L' & 5 \\
'M', 'N', 'O' & 6 \\
'P', 'Q', 'R', 'S' & 7 \\
'T', 'U', 'V' & 8 \\
'W', 'X', 'Y', 'Z' & 9 \\
 \hline
\end{tabular}
\end{center}
The following program sets up the above table and uses it
to translate phone numbers.
\begin{verbatim}
;Name:
;email:
;date:
;
; Standard buffalo equates
; Make sure you have ALL the equates in the file.
;

ucase       equ $ffa0
wchek       equ $ffa3
dchek       equ $ffa6
init        equ $ffa9 * use with caution
input       equ $ffac
output      equ $ffaf
outlhlf     equ $ffb2
outrhlf     equ $ffb5
outa        equ $ffb8
out1byt     equ $ffbb
out1bsp     equ $ffbe
out2bsp     equ $ffc1
outcrlf     equ $ffc4
outstrg     equ $ffc7
outstrgo    equ $ffca
inchar      equ $ffcd
vecinit     equ $ffd0

    org $3000
; setup some strings ...
preamble
    fcc /=====================================/
    fcb 10
    fcc /Lab on using Tables/
    fcb 10, 10 *use 10 to start a new line
    fcc /Name: _____________________/
    fcb 10
    fcc /This program is an infinite loop! /
    fcb 10
    fcc /Hit the reset button to quit/
    fcb 10
    fcc /=====================================/
    fcb 10,10,10, 4


; setup the tables\begin{verbatim}
alphabet fcc /ABCDEFGHIJKLMNOPQRSTUVWXYZ/
nums     fcc /22233344455566677778889999/
nalphabet equ 26


    org $2100
    ldx #preamble
    jsr outstrg
    jsr outcrlf

mainloop
    jsr inchar
    jsr ucase ; convert to upper case if necessary
    jsr translate ; perform the translation
    psha          ; save it in the stack for now

; print 2 spaces so the output looks neat
    ldaa    #' '
    jsr     outa
    jsr outa

    pula        ; get it back
    jsr outa
    jsr outcrlf
    bra mainloop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;translate: Translates telephone codes.
;Entry: Register A has the value to be translated
;Exit: If A has an upper case letter, then its content is replaced
;      by the the translation given on the phone is performed
;          ABC -> 2, DEF ->3, etc.
;      If A has any other character, it is left alone.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

translate
    ldx #alphabet
    ldab #nalphabet
    jsr lookup
    bcc bye ; Not an alphabet. Leave it alone

    ldaa nalphabet,x ; This is the key to translation
bye
    rts



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; lookup: Function to lookup a value in a table
;         Checks if the value in A register is in the table
;
; Entry: Starting address in X, size in B, value in A
; Exit:  Carry set if the value in A is in the table;
;              cleared if not in the table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

lookup
lkploop
        tstb
        beq     notthere *this is the basic counting loop
        cmpa    0,x
        beq     foundit
        inx
        decb
        bra     lkploop
notthere
       clc
       rts
foundit
       sec
       rts


\end{verbatim}
\paragraph{Exercise: }
Type the above program and enter the  input: {\tt 1-800-UMD-ALUM}. You
should see the following output. Clearly explain how the translation gets done.
\begin{verbatim}
 =====================================
Lab on using Tables

Name: _____________________
This program is an infinite loop!
Hit the reset button to quit
=====================================



 1  1
 -  -
 8  8
 0  0
 0  0
 -  -
 U  8
 M  6
 D  3
 -  -
 A  2
 L  5
 U  8
 M  6
\end{verbatim}
\end{document}
