\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{Introduction to Looping}
\date{}
\author{N. Narasimhamurthi}
\maketitle
\section{Objective}
To become familiar with elementary loops and simple input/outputs functions.

\subsection{Simple Input/Output}
One of the basic functionality provided by any operating system is input/output routines.
BUFFALO provides several useful functions for performing input/output. In this lab, we will
look at two output functions provided by BUFFALO. Unlike in high level languages, functions
in machine language are known by their addresses. The two functions we will be using are in ROM at
locations {\tt \$FFB8} and {\tt \$FFBB}\footnote{We will indicate HEX values with the prefix \$. However,
data that you would be entering in BUFFALO, as part of memory modify or register modify will be shown without
the prefix \$ although it will be understood that the numbers are written in HEX.}. Rather than use these
hard to remember and hard to recognize
numbers, it is customary to give them meaningful names.
Unless you have a good reason to do otherwise, it
is best to use the name suggested by the vendor, in this case Motorola.
The 'official' names for these
functions are {\tt OUTA} and {\tt OUT1BYT} respectively.
In assembly language, we make the connection
between a name (technically known as a label) and a value using the EQU command as shown
\begin{verbatim}
OUTA         EQU     $FFB8
OUT1BYT      EQU     $FFBB
\end{verbatim}
NOTE: Labels should be written starting from column 1. If a line does not have a label, it should
start with a space or a tab or a comment character.
\subsubsection{The function {\tt OUTA}}
The function {\tt OUTA} will transmit whatever is in register A over the serial communication line
that is connected to the PC. What the PC does with this value depends on the terminal program that
is used to communicate with the HC11. Under normal circumstances, the terminal program
will interpret the value as an ASCII code and display the corresponding character on the screen.
\paragraph{Exercise:} Power up HC11 and at the BUFFALO prompt
try the following and write down what you see.
\begin{enumerate}
\item
Issue the command RM (for register modify) and press the space bar till you see the {\bf A} register.
Enter the value 31 and press enter as shown below:
\begin{verbatim}
 >rm
P-AAAA Y-AAAA X-AAAA A-AA B-AA C-D0 S-004A
 P-AAAA
 Y-AAAA
 X-AAAA
 A-AA 31

 >
\end{verbatim}
Now execute the command
\begin{verbatim}
 CALL FFB8
\end{verbatim}
\item
Repeat with the {\bf A} register modified with the following values:
{\tt 32, 33, 34, 21, 22, 23, 24, 25, 41, 42 43 44}
\end{enumerate}
If you are using the simulator, turn on the log feature (by pressing both the shift keys). If you are
working with a real HC11, you can copy and paste the contents of the terminal screen.
\subsubsection{The function {\tt OUT1BYT}}
This is a more involved output function. To start with, the value to be printed must be in memory.
If the value is in a register you will have to store it in memory first. Next, the value in the
{\bf X} register should be the address where the value is stored. Thus this function should be told 'where'
and not 'what'. When you call this function, the function will send {\em two} characters to the PC. If the
terminal program interprets these two characters as ASCII codes and displays the corresponding two
characters, then the display would be the value written in HEX. {\em In addition, the function will
increment the value in the {\bf X} register}. This is useful when displaying  a series of memory locations.
\paragraph{Exercise:}
\begin{enumerate}
\item
Using the MM command (memory modify) enter the following values in memory locations
{\tt 3000, 3001}, $\cdots$: {\tt 30 31 32 33 41 42 43 44}. Verify the values using the memory
dump, MD,  command.
\item
Using RM, the register modify command, change the value in the {\bf X} register to {\tt 3000}
\item
Execute the command {\tt CALL FFBB} and write down what the output was and also the value in the
{\bf X} register after the command is executed.
\item
Repeat the the command {\tt CALL FFBB} and write down the output and the new value in the {\bf X} register.
\item
Repeat the last part until you have performed 7 calls to {\tt \$FFBB}.
\end{enumerate}
\subsection{Branching}
Conditional branching in HC11 is controlled by the state of one or more hardware flags.
The state of a flag depends on the most recently executed instruction
that affects the flag. Thus, if your branching depends on the result of some instruction, then it is
your responsibility to make sure that none of the instructions between the instruction you are interested
in and the branching instruction affects the flags that control the branching instruction.
Thus, it is a good idea to follow the instruction that sets the flag by the branching instruction.
In this lab, we will use the following conditional branch instructions:
\begin{center}
\begin{tabular}{|l l|}
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \hline
  BEQ & Branch if the Z flag is set \\
  BNE & Branch if the Z flag is not set \\ \hline
\end{tabular}
\end{center}
Now, the {\tt Z} flag is set after most instructions if the result of the instruction is a {\em zero}; or else it
is cleared, i.e. not set. The two most important instructions that are often used to set/clear the flag are
\begin{center}
\begin{tabular}{|l l|}
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \hline
  CMP & Perform a subtraction and discard the answer.  However, set the flags. \\
  TST & Same as CMP except subtract the number zero\\ \hline
\end{tabular}
\end{center}
Thus, after the {\tt CMP} command, the {\tt Z} flag would be set if the two values that are compared are
equal. Similarly, the {\tt TST} command will compare a value (memory or register) with zero and set the Z flag
if the value is zero.
\paragraph{Exercise:}
\begin{enumerate}
\item
Using the HC11 reference book, identify 5  instructions that do {\em not} affect the {\bf C} flag, but affects some other flag.
\item
Using the HC11 reference book, can you identify any  instruction that does {\em not} affect the {\bf V} flag, but affects some other flag?
\item
Using the HC11 reference book, identify 5  instructions that always clears the {\bf C} flag.
\item
Using the HC11 reference book, identify an  instruction that always sets the {\bf C} flag.
\end{enumerate}
\subsection{Looping}
\subsubsection{Counting loops}
This is by far the simplest and most used loop structure. In a counting loop, we perform a specific operation
a given number of times. A counter controls how many times the loop is executed. The counter could be
stored in memory or kept in a register. If it is kept in a register, it is your responsibility to make sure
that the register is not inadvertently changed either by your code or by some third party function that you call.
If you decide to use a register, your best bet is to use either the {\bf B} register or {\bf Y} register.
The structure of the loop is as follows:
\begin{itemize}
\item[1:] Initialize the counter to the number of times the loop is to be performed
\item[2:] Perform any other initializations
\item[3:] Test if the counter is zero. If so quit the loop
\item[4:]Perform the desired task
\item[5:] Perform any re-initializations
\item[6:] Decrement the counter
\item[7:] Go back to 3
\item[8:] Come here when you quit the loop
\end{itemize}
Note that there are two places where the code jumps to. One to (3) and the other to (8). When writing the code
in assembly language, we would need two labels. In the code that follows, I have used the labels {\tt FOO}
and {\tt BAR}.
\paragraph{Exercises:}
\begin{enumerate}
\item
Assemble the following code in your PC, transfer the S19 file to HC11, run the program and write down the
output of the program.
\begin{verbatim}
;Name:
;email:
;date:
;
OUTA         EQU     $FFB8

            ORG $2100
            LDAB    #$9 ; USING REGISTER B AS A COUNTER
            LDAA    #$31 ; OTHER INITIALIZATION
FOO         TSTB        ; SUBTRACT ZERO FROM B
            BEQ BAR     ; QUIT IF THE Z FLAG IS SET, I.E. B=0
            JSR OUTA    ; DO THE TASK
            INCA        ; RE-INITIALIZE
            DECB        ; DECREMENT THE COUNTER
            BRA  FOO    ; GO BACK

BAR
            SWI
\end{verbatim}
\item
Modify the above program so that the program prints the upper case letters {\tt A} to {\tt Z}. Clearly indicate
the changes you made.
\item
The following function is equivalent to the function shown above\footnote{You can have as many functions as you
want in the same file. Make sure that when you change the ORG, the functions do not overlap. You can determine
this by looking at the LST file}.
\begin{verbatim}
; continued from the previous function

            ORG $2200
            LDAB    #$9 ; USING REGISTER B AS A COUNTER
            LDAA    #$31 ; OTHER INITIALIZATION
JUBJUB
            JSR OUTA    ; DO THE TASK
            INCA        ; RE-INITIALIZE
            DECB        ; DECREMENT THE COUNTER
            BNE  JUBJUB    ; GO BACK


            SWI
\end{verbatim}
Verify that {\tt CALL 2100} and {\tt CALL 2200} produces the same output. Explain why this is so, and how
and why the loop terminates.
\end{enumerate}
\subsubsection{One, two! One, two! And through and through ... Marching through memory}
Often loops are combined with marching through memory and operating on consecutive memory location. In this case,
the {\bf X} (and/or {\bf Y}) register is initialized to a starting memory address. Inside the loop, memory is
accessed using {\tt IND,X} addressing mode. This operates on memory whose address is computed using the
value in {\bf X} register. At the bottom of the loop, {\bf X} is incremented, so that next time around the
loop, the operation is performed on the next memory location\footnote{In some cases the memory has to be accessed
in the reverse order. In this case, {\bf X} starts at the end, and at the bottom of the loop, {\bf X} is decremented.}.
The following program shows prints using {\tt OUTA} the values stored in 9 consecutive locations starting from
location {\tt \$3000}. Note we
are using FCB which is the assembly language equivalent of memory modify.
\begin{verbatim}

               ORG    $2300

               LDAB   #$09
               LDX    #$3000 *Don't forget the #

VORPAL         TSTB
               BEQ    SWORD

               LDAA   0,X
               JSR    OUTA

               INX
               DECB
               BRA    VORPAL

SWORD          SWI

; FOLLOWING SAME AS MM 3000 FOLLOWED BY: 55 6F 66 4D 2D 44 62 72 6E
               ORG $3000
               FCB  $55, $6F, $66, $4D, $2D, $44, $62, $72, $6E
\end{verbatim}
Run the above program using {\tt CALL 2300}\footnote{Don't forget to assemble it and then
transfer the S19 file to the HC11!}. Modify the above program as shown below and explain
what the program does. Do you see why we do the {\tt DEX} before we access memory?
\begin{verbatim}

               ORG    $2400

               LDAB   #$09
               LDX    $3000
               ABX

SNICKER        TSTB
               BEQ    SNACK

               DEX

               LDAA   0,X
               JSR    OUTA

               DECB
               BRA    SNICKER

SNACK          SWI

               ORG $3000
               FCB  $55, $6F, $66, $4D, $2D, $44, $62, $72, $6E
\end{verbatim}

Now for some other useful examples. Explain what each of them does. Run the programs
and using memory dump, verify that your explanation is correct. Each of the functions start
with an {\tt ORG} command.
\begin{verbatim}
                ORG   $2500
                LDAB  #$10
                LDX   #$3000

CALLOOH         TSTB
                BEQ   CALLAY

                LDAA  0,X
                ADDA  $10,X
                STAA  $20,X

                INX
                DECB
                BRA CALLOOH
CALLAY
            SWI

            ORG $3000
            FCB $55, $6F, $66, $4D, $44, $62, $72, $6E
            FCB $41, $42, $43, $44, $45, $46, $47, $48
            FCB 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

; NOTE: YOU CAN ENTER NUMBERS EITHER IN DECIMAL OR HEX!
; AFTER YOU RUN THE PROGRAM, DO
; MD 3000 302F
; TO SEE WHAT THE PROGRAM DOES
\end{verbatim}

\begin{verbatim}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                ORG     $2600
                LDAA    #$0A
                JSR     OUTA
                JSR     OUTA

                LDAB    #$10
                LDX     #$3020

KINGS           TSTB
                BEQ     CABBAGES

                JSR     OUT1BYT
                LDAA    #$2C
                JSR     OUTA
                LDAA    #$20
                JSR     OUTA

                DECB
                BRA     KINGS
CABBAGES
                LDAA    #$0A
                JSR     OUTA
                JSR     OUTA

                SWI
\end{verbatim}

\end{document}
