Fox11_68HC11

Transcription

Fox11_68HC11
Programming the FOX11-68HC11 Board
Programming the FOX11-68HC11 Board
Contents
Section 1: Getting Started: .......................................................................................................... 3
Setup: ...................................................................................................................................... 3
Hooking up the FOX11 68HC11 Board: ................................................................................ 4
Com Port Setup and Configuration: ........................................................................................ 4
The AsmIDE Environment: .................................................................................................... 6
The Test Program:................................................................................................................... 8
Section 2: Registers and Memory: ............................................................................................ 11
Types of Memory:................................................................................................................. 11
68HC11 Registers: ................................................................................................................ 11
Addressing Modes: ............................................................................................................... 13
Section 3: Assembling, Executing, and Debugging: ................................................................ 16
Creating a program: .............................................................................................................. 16
Assembling a program: ......................................................................................................... 18
Debugging a program: .......................................................................................................... 19
Loading and executing a program: ....................................................................................... 21
Section 4: Instructions and Directives: ..................................................................................... 23
Directives: ................................................................................................................................. 23
Instructions:............................................................................................................................... 26
Section 5: Machine Code Sequences and Decoding Machine Language Instructions: ............ 32
Section 6: Flowcharts:............................................................................................................... 34
Flowchart symbology: .......................................................................................................... 35
Section 7: Mathematical Operations and the Flags of the Condition Code Register: .............. 36
Condition Code Register: ...................................................................................................... 36
Multiprecision Arithmetic:.................................................................................................... 36
Section 8: The Stack and Indexable Data Structures: ............................................................... 37
The Stack: ............................................................................................................................. 37
Indexable Data Structures ..................................................................................................... 39
Section 9: Subroutines: ............................................................................................................. 40
Section 10: Buffalo Input/Output (I/O) Routines: .................................................................... 43
Section 11: Interrupts and Resets:............................................................................................. 46
1
Programming the FOX11-68HC11 Board
Interrupt: ............................................................................................................................... 46
Interrupt Vector:.................................................................................................................... 47
Writing/Handling the Interrupt Request: .............................................................................. 47
References: ................................................................................................................................ 49
2
Programming the FOX11-68HC11 Board
Section 1: Getting Started:
Setup:
The first step to getting the FOX11 68HC11 board set up is to install the Development
Environment Software. The CD containing the software should have been included with the
board. After inserting the CD the “setup.bat” file should be run, seen in Fig. 1.1. This will
automatically install the development environment in the C:\Ep2IDE folder.
Fig. 1.1- Setup.bat
3
Programming the FOX11-68HC11 Board
Hooking up the FOX11 68HC11 Board:
The next step in getting the FOX11 board set up is to connect it to a PC. Connecting the FOX11
board to a PC is done through a serial cable that is included with the FOX11 kit. Both ports on
the side of the board may be used, the other end of the cable is connected through the PC’s serial
port. If no serial port is available on the PC a separate USB to Serial connector may be needed,
these can be found at any electronics store. Once the serial connection has been made, connect
the power supply to the board.
Com Port Setup and Configuration:
Once the FOX11 board has been connected it is time to open the development environment.
Navigate to the C:\Ep2IDE folder and run the “AsmIDE.exe” program, this should bring up the
development environment seen in Fig. 1.2. Before beginning any programming the
Communications Port will need to be set up.
Open the View>Options menu, this will open the AsmIDE options menu. The first tab that is
seen will be the “COM Port” options. Using the “Select COM Port” drop down menu choose
the number that corresponds to the COM Port that was automatically assigned when the FOX11
board was connected to the PC. If unsure which port to use it can be determined by opening the
Control Panel located under the Windows Start menu and opening the Device Manager>Ports
(COM & LPT) menu.
Next, ensure that the “Enable the Terminal Window” box is checked and then move on to the
“Assembler” tab. The settings should match those seen in Fig. 1.3. Under the “Wytec hc11
tools” set the “Name of your Wytec board” to the EVBplus2JR option. The “Your MPU type:”
should be 2=68HC11E0,E1,E9…
4
Programming the FOX11-68HC11 Board
Fig.1.2 View of AsmIDE Development Environment
On the final tab, “Associations”, make sure the box “File Extension for your assembler files:”
Contains “.asm”, if it does not type it into the box. The .asm files are the programs that the user
will create and this ensures that the assembler knows which files to use in assembling the
programs. Once this is done click the “Associate Assembler files with AsmIDE” button and then
click “OK”.
5
Programming the FOX11-68HC11 Board
Fig. 1.3 Assembler tab settings
The AsmIDE Environment:
Figure 1.4 shows the AsmIDE environment, as can be seen there are two main windows that are
important to the user. These will all be discussed in further detail in Section 2.
Upper Window: This is where the program will be entered by the user.
Lower Window: This area will display messages regarding the programs assembly and also
where programs will be executed from, it contains the Messages and Terminal tabs.
Assemble & Download Buttons: These two are used for execution and assembly of a program.
6
Programming the FOX11-68HC11 Board
Messages tab: Assembling a program will result in one of two things, either a success or failure.
Either way this information will be displayed under this tab.
Terminal tab: In order to execute a program this area will be used. Output of a program and
information regarding the memory and data stored will all be handled under this window.
Assemble
Download
Upper Window
Lower Window: Messages and Terminal Area
Fig. 1.4 - Important aspects of AsmIDE environment
7
Programming the FOX11-68HC11 Board
The Test Program:
Now that the FOX11 board has been set up it is time to test it and ensure it is functional.
Included with the AsmIDE software is a test program that can be run .
First go to File> Open> and navigate to the C:\Ep2IDE\Ex_BUFFL\Test.asm
This will load the Test.asm program into the upper window and the next step is to assemble the
program. Clicking the “Assemble” button will ensure that the program is functional, under the
“Messages” tab there will be a message indicating a successful execution. Figure 1.5 shows the
results of a successful assembly.
Fig. 1.5 - Successful assembly
A successful assembly of the program means it is time to load and run the program. When the
program is assembled a second file is created in the same directory as the .asm file. This file is
given the .s19 file type and is the “executable” version of the program. A more thorough look at
assembling, executing, and debugging programs can be seen in Section 3.
8
Programming the FOX11-68HC11 Board
To load the program:
First, click on the “Terminal” tab located in the bottom window and then push the reset button
located on the FOX11 board. The reset button will ensure that the board is ready for a new
program and does not contain any “leftover” data from a previous program. It is recommended
that the user reset the board before any programs are run. When the reset is pressed the user will
see the following message appear in the Terminal window:
BUFFALO 3.47sc - Bit User Fast Friendly Aid to Logical Operation.
Put the cursor in the Terminal window by clicking anywhere within it and then press <Enter>.
At the prompt type: Load and press <Enter>
Next click on the “Download” button at the top and it will bring up a new window. Locate the
“Test.s19” file and double click it.
The following should appear:
…………………………………………
Done
>
Once the “done” is displayed, type in
 g D000
this will tell the program to begin at memory location D000. More on memory is discussed in
the next section.
The program should now execute and the FOX11 board should now light up and display a
message on the LCD screen. Pressing the PA0 switch will cause a song to be played as the
LED’s light up. If nothing happens, double check to ensure that everything is connected and that
9
Programming the FOX11-68HC11 Board
the board has power. Once the program is running the reset button can be pressed at any time to
stop execution of the program.
Now that the board has been set up and tested it is time to look at where and how the user
entered programs are stored.
10
Programming the FOX11-68HC11 Board
Section 2: Registers and Memory:
In order for the FOX11 board to store and manipulate data it is necessary for it to be able to store
and save values. This is done through the use of the registers and memory.
Types of Memory:
The FOX11 68HC11 board comes with three types of onboard memory. It contains 256 bytes of
static random access memory, 512 bytes of electrically erasable programmable read-only
memory, and 8 kilobytes of read only memory.
The random access memory (RAM) and the electrically erasable programmable read-only
memory(EEPROM) are where the FOX11 will store user entered programs and data.
The read only memory (ROM) is where the FOX11 stores information pertaining to specific
commands and instructions that will be entered by the user. This allows the board to “translate”
user entered commands such as LDAA #2 and understand what is meant by this command.
68HC11 Registers:
During program execution there are certain reserved memory areas within the FOX11 board that
are used to perform operations on user entered data and to manipulate and move data from one
location to another. These memory locations are called the “Registers” and are what will be used
throughout all the programs that will be done.
Accumulators A and B: These registers are 8 bit registers and are used extensively for
mathematical operations and for storage and manipulation of data. For example, the following
commands would load a value into accumulators A and B, increment the value, and then store
these values in another location.
LDAA #1
*This loads the value 1 into accumulator A
LDAB #2
*This loads the value 2 into accumulator B
11
Programming the FOX11-68HC11 Board
INCA
*This command increments the value in A so that it is now 2
INCB
*This command increments the value in B so that it is now 3
STAA $D100
*The value in A is now stored at memory location $D100
STAB $D200
*The value in B is now stored at memory location $D200
Accumulator D: This accumulator is a 16 bit register. It is made by combining accumulators A
and B and therefore allows the user to store a 16 bit value and perform the same operations on it
as can be done on A and B. When loading data into accumulator D the high byte is loaded into
accumulator A while the low byte is loaded into accumulator B. For example, performing the
following command: LDD #$1678 would load the value $16 into accumulator A and $78 into
accumulator B. A word of caution, if accumulator D is being used loading any data into A or B
will result in changing the value of accumulator D.
Index Registers X and Y: Also known as IX and IY, these registers are used mostly for storing
memory locations, this is useful when dealing with arrays. The following example would allow
a user to access every part of a predefined array using register X
LDX #array
*This loads the memory location of the first element of an array
LDAA 0,X
*This loads the first element of an array into accumulator A
INX
*This increments X so that it now has the next memory location of
the array
Stack Pointer: Also called the SP. This is a 16 bit register that is used in keeping track of
memory locations. This will be discussed further in Section 8.
12
Programming the FOX11-68HC11 Board
Program Counter: This 16 bit register keeps track of where the program is at in its execution.
As each line is executed the program counter is incremented to point to the next line of the
program.
Condition Code Register: CCR, when mathematical operations or comparisons are performed
this 8 bit register is used to keep track of the results. This will be discussed in more detail in
Section 7.
Addressing Modes:
Addressing modes are how the AsmIDE environment specifies what operands are to be used in
the execution of a program. When writing a program the user will sometimes have need to use a
certain memory address, at other times there will be a need to use a certain value contained in a
memory address. This is done through the use of the various addressing modes, below are the
types and examples of each addressing mode.
Immediate (IMM): This mode is used when there is a need to load a certain value directly into
memory. The following is an example of how immediate mode can be used to load the hex
value $12 into accumulator A.
 LDAA
#$12
*Accumulator A will now contain $12
Direct (DIR): Direct addressing mode is used to manipulate 8 bit memory addresses. This
means that only addresses $0000-$00FF may be addressed using Direct mode. To access
anything past those addresses Extended mode should be used (discussed next). The following is
an example of using direct mode to load accumulator A with the contents of memory address
$0011
13
Programming the FOX11-68HC11 Board
 LDAA
$11
*The 00 is unneeded with Direct mode, the assembler
Knows that the user is accessing $0011.
Note:In this example accumulator A now contains whatever value was located in
memory address $0011. It is important that the user keeps track of what values are
located in the addresses being loaded or the program may be sent “garbage” values.
-It is also important to note that when a 16 bit register is loaded through Direct mode it
will load the address specified plus one more. Example:
 LDD $11
*Will load accumulator D with the contents of
address $0011 AND $0012
Extended (EXT): Extended mode performs exactly like direct mode except that the entire
memory address is used. The following example will perform exactly as above but uses
extended addressing mode instead.
 LDAA
$0011
As with Direct mode, extended mode will load the data in the specified memory address plus one
when a 16 bit register is used.
Indexed (INDX, INDY): Indexed mode allows the user to access certain memory locations by
specifying the address and an offset value from that address. For Indexed mode registers X and
Y are used to specify the memory address along with an offset which will load the specified
memory address plus the offset. The following example will use register X and an array, set at
address $D000.
 LDX #$D000
*Sets X to point to memory address $D000
14
Programming the FOX11-68HC11 Board
 LDAA
0,X
*This loads accumulator A with the value at memory
Location $D000
 LDAB
5,X
*The 5 tells the assembler to load the value at D005
Inherent (INH): Inherent mode is when certain instructions can be executed through the use of
only one command. This may be things such as incrementing a value in a register or adding the
values stored in two registers together. These examples can be seen below:
 INCA
*Increments the value in accumulator A
 ABY or ABX
*Adds the contents of accumulator B to registers X or Y
 DEX or DEY
*Decrements the values located in registers X or Y
15
Programming the FOX11-68HC11 Board
Section 3: Assembling, Executing, and Debugging:
This section will discuss how the user creates, assembles, executes, and debugs programs using
the AsmIDE Development Environment.
Creating a program:
There are four main parts to every line of code that the user will input, they are the label, opcode, operand, and comment. These four fields are shown in Figure 3.1.
Label: A label allows the user to create unique variables or set up subroutines, the only
constrains are that a label cannot contain spaces or ascii characters.
Op-code: The op-code is the function or operation to be performed. This can be an instruction
such as LDAA which will load accumulator A
Operand: The operand is the variable or memory location that the op-code will operate on.
Comment: Comments are not required by the assembler but allows others a quick reference to
what is happening within the code.
16
Programming the FOX11-68HC11 Board
Fig. 3.1 – Four fields of a user program.
When creating any program in the AsmIDE environment the code is typed into the upper
window. The user needs to ensure that all the fields are in their separate columns, if this is not
done the assembler may have problems when assembling the code. Figure 3.2 shows a sample
program with all columns aligned. Now that the user has a program typed in, it is time to
assemble it.
17
Programming the FOX11-68HC11 Board
Fig. 3.2 - Sample program
Assembling a program:
Once a program has been entered it is time to save and assemble it. To save go to
File > Save As> And enter a name.
It is necessary to enter the .asm extension when saving the file so that the AsmIDE environment
can recognize the file. Once saved press the “Assemble” button and watch for any errors in the
“Messages” tab. Once assembled the program should appear differently. Labels and operands
should be colored a faint reddish brown while Op-codes become a bold blue color.
18
Programming the FOX11-68HC11 Board
A successful assembly results in a separate file being created in the same directory as the .asm
file. This file will be the same name but given the .s19 extension. This file will be used when
loading a program.
Debugging a program:
When a program is assembled it is checked for any errors. There may be any number of possible
problems that occur in a program. These problems are generally user created errors and are most
often the result of a misspelled command or label, sometimes they involve the misuse of a
command and the assembler does not know what to do. Fortunately, the AsmIDE development
software easily identifies lines in which there are problems with the code. Located along the left
hand side of the upper window are numbers that correspond to the line of code, when an error is
found the assembler will use this number and instruct the user that there are errors at this line.
Figure 3.3 shows the result of assembling code that is containing errors.
The code that was used in the sample program above was modified so that it would fail the
assembly.
The first change involved changing the label “loop” to “lop” on line 5.
The second change involved removing the “i” from “hi” on line 8.
Here is the result of the assembly:
Now assembling test.asm
AS11, an absolute assembler for Motorola MCU's, version 1.2e
test.asm:8: Error - Symbol h is undefined.
test.asm:9: Error - Symbol loop is undefined.
test.asm:9: Error - Branch out of Range
Executed: Sun Mar 16 13:18:51 2008
Total errors: 3, Total warnings: 0
Exitcode = 3
19
Programming the FOX11-68HC11 Board
The first error: test.asm:8: Error - Symbol h is undefined, shows that the assembler does not
understand what the symbol “h” means and it gives the line of this error.
The second and third errors are related, the first : test.asm:9: Error - Symbol loop is
undefined. Lets the user know that “loop” is an unknown command, because it was changed to
“lop”. The last error, test.asm:9: Error - Branch out of Range, means that the assembler does
not recognize where it is to loop to, because the label of the loop was changed.
Simply correcting the misspellings on the two lines will result in all three errors being cleared
and a successful assembly.
20
Programming the FOX11-68HC11 Board
Fig. 3.3 - Assembly with Errors
Loading and executing a program:
Once successfully assembled the program can be loaded and executed.
Click on the “Terminal” tab and place the cursor in the window and then press reset on the
FOX11 board.
Next, at the command prompt type:
 Load and then <Enter>
21
Programming the FOX11-68HC11 Board
Click the “Download” button at the top (the lightning bolt button). This will bring up a new
window, locate the test.s19 file and double click it or click once and then <Open>
The user should see:
 …………
 Done
Once the program is loaded there are many options available to the user.
If, for example, the user wishes to step line by line through the code the following command
sequence would be used:
 g D600
<enter>
Next, press the reset button on the FOX11 board
 t
The above two commands will do two things. First, it tells the development environment
to begin executing the code at instruction D600. The “t” stands for trace, and it allows
for the execution of each command line by line so that the inputs and outputs can be seen.
Pressing <enter> will execute the next line of the code. The trace command is useful for
debugging code when the desired results are not being found.
22
Programming the FOX11-68HC11 Board
Section 4: Instructions and Directives:
The following is a listing and examples of most of the instructions and directives that the user
will come across while programming the FOX11 board.
Directives:
Directives should be thought of as reserved words that the AsmIDE environment uses to perform
certain functions before a program is executed. These should always be placed at the beginning
of a program. It is also necessary that the user not try and access memory locations that have
already been set to a directive as errors can occur because of this.
BSZ: Block Storage of Zeros. This command allows the user to create a section of memory
and fill it with zeros. Useful for clearing out garbage values from memory so that inputs can be
seen. It can be given a label but that is optional.
Usage:
label
BSZ
Example:
empty BSZ
<length>
;Creates a block of 40bytes and fills with 0’s
<40>
DCB: Define Constant Block. This command is nearly identical to BSZ but it allows the user to
specify what to use to fill the empty blocks, the “filler” value. Again a label is optional.
Note: Not all development environments support this command, the FILL command may
be used instead.
Usage:
label
DCB <length>,<filler>
Example:
test
DCB 20,$00
;Creates a block of 20 bytes and fills with $00
END: This command specifies the end of a program. There are no other values to specify but a
label can be used.
Usage:
label
END
23
Programming the FOX11-68HC11 Board
Example:
;This is a “trap”, causing the program to
finish END finish
loop to END over and over, useful when a program fails to end when specified.
EQU: Equate. This allows the user to give a name to a variable that may be used repeatedly
instead of loading a certain value every time. A label IS required.
Usage:
label EQU <value>
Example:
touchdown
EQU $06
;Creates a variable called touchdown and
sets it to $06.
To use this value:
LDAA
#$05
CMPA
touchdown
FCB: Form Constant Byte. This will allow the user to create a listing of specified elements one
byte long. Useful for creating an array or matrix.
Usage:
label
FCB
Example:
array FCB
<value>
$35,$45,$55,$65
;Creates a list of items
($35,$45,$55,$65) in memory called array.
FCC: Form Constant Character. Like FCB but this command allows a string of characters to be
stored. The string should be enclosed in quotation marks.
Usage:
label
FCC
“<string>”
Example:
hi
FCC
“Hello”
;Stores the ascii characters for the word
“Hello” in memory.
FDB: Form Double Byte. This performs much like FCB except it stores two bytes of data for
each initialized compared to one for FCB.
Usage:
label
FCB
<value>
24
Programming the FOX11-68HC11 Board
Example:
array FCB
$35,$45,$55,$65
;Creates a list of items
($00,$35,$00,$45,$00,$55,$00,$65) in memory called array.
FILL: Performs the same function as DCB with virtually the same usage.
Usage:
label
FILL <filler>,<length>
Example:
test
DCB $00,20
;creates a list of items in memory 20
bytes long and fills it with $00
ORG: Sets the location of the counter. Any data loaded into memory after this command will
start at the value set and increment after each line.
Example:
ORG $8000
;sets memory location to $8000
NUM RMB 1
;This command is at memory
location $8000
LDAB #$45
;This command is at memory
location $8001
LDAA #46
;This command is at memory
location $8002
RMB: Reserve Memory Byte. Much like the other commands that reserve a space in memory
RMB reserves a specified amount of space, useful for creating a variable to be used in the
program.
Usage:
Example:
label
num
RMB <length>
RMB 1
;Creates a variable called
“num” in memory and reserves 1
byte for this value
25
Programming the FOX11-68HC11 Board
Instructions:
Instructions are commands used by the AsmIDE Development Environment that allow the user
to perform specific operations on values and memory locations, these are mainly mathematical
manipulations but some are used for navigating through the program.
Note: There are many more instructions available to the user, the instructions and
accompanying examples are presented for the users reference. All the instructions may be found
in the 68HC11 User Guide.
ADD: This performs the addition function on the specified register. It can be used to add two
registers together or it can be used to add a user entered value to a register.
Examples:
ADDA
#$20
;Adds $20 to accumulator A
ADDB
#$20
;Adds $20 to accumulator A
ABA
;Adds accumulator B to A, the value is stored in A
ABX
;Adds accumulator B to X, the value is stored in X
BRANCH: A branch is generally used in connection with a compare instruction but it can be
used without any conditions. Whether or not a branch occurs depends upon the values within the
Condition Code Register(Discussed in Section 7) and the branch that is being used. Below is a
short list of branches most likely to be used by the user.
26
Programming the FOX11-68HC11 Board
Examples:
BEQ
loop
;Branch if equal, when two values equal this branch
will occur,in this case it will jump to loop
BNE
loop
;Branch if not equal to zero, if the compare does not
result in a zero in the CCR, then it will branch to
loop.
BHI
loop
;Branch if higher, if the value in the register is
Higher than the value being compared to it, it will
branch to loop.
BGE loop
;Branch if greater than or equal to, the value in the
register is greater than or equal to the compared
value it will branch to loop.
CLEAR: This command is used if there is a need to set the register to zero. This is helpful if the
user needs to remove any garbage values from the register.
Examples:
CLRA
;Sets register A to zero
CLRB
;Sets register B to zero
COMPARE: Compare allows the user to compare the values stored in one register with another
user specified value. Using the Condition Code Register(Discussed in Section 7) a branch
operation can be performed(See Branch). It is important to note that depending on whether the
values being compared are higher, lower, or equal, different branch instructions are used. They
can be given labels if the user wishes to use them in a loop.
Examples:
LDAA
#$20
;Loads acc A with $20
CMPA
#$25
;Compares the value $20 in acc A with value $25
27
Programming the FOX11-68HC11 Board
LDX
CPX
#$20
#$25
;Performs the same compare as above except using
;accumulator X
DECIMAL ADJUST: This command is used in conjunction with a binary coded decimal
addition that results in an invalid value. When a carry is loaded into the Condition Code
Register(See Section 7) this command will automatically adjust the BCD to make it a valid
value.
Example:
DAA
;Performing this operation after a BCD addition
Results in a correct BCD result
DIVISION: There are two different operations available to the user, FDIV and IDIV.
FDIV: This performs a fractional division; it takes a 16 bit value stored in accumulator D
and divides it by the 16 bit value stored in register X. After this operation the remainder is saved
to accumulator D, the quotient is placed in register X.
Example:
LDD
#$1000
LDX
#$1000
IDIV
IDIV: This performs an integer division; it takes a 16 bit value stored in accumulator D
and divides it by the 16 bit value stored in register X. Like FDIV, the remainder is saved to
accumulator D while the quotient is placed in register X. Its usage is exactly like that of FDIV.
28
Programming the FOX11-68HC11 Board
EXHANGE REGISTER: Often times its necessary to swap the contents of two registers. The
exchange register commands allow the user to swap the contents of accumulator D with those
values in either register X or Y
Examples:
Assume accumulator D contains the value $D000 and registers X and Y
contain the value $D100
XGDX
;Register X now contains $D000, acc D contains
$D100
XGDY
; RegisterY now contains $D100, acc D contains
$D100
Increment/Decrement: The AsmIDE environment provides a quick and easy way to increment
and decrement values located in registers and stored as variables.
Examples:
INCA
;Increments the value contained in acc A by 1
INCB
;Increments the value contained in acc B by 1
INS
;Increments the stack pointer by 1
INX
;Increments the value contained in register X by 1
INY
;Increments the value contained in register Y by 1
DECA
;Decrements the value contained in acc A by 1
DECB
;Decrements the value contained in acc B by 1
DEX
;Decrements the value contained in register X by 1
DES
;Decrements the stack pointer by 1
-The following example creates a variable named Num and loads it with the value $01, then
using the INC command increments the value by 1
29
Programming the FOX11-68HC11 Board
Num
RMB
1
LDAA
$01
STAA
Num
INC
Num
LOAD: This command loads data into a register. It works for all registers(A,B,D,X,Y, SP)
Examples:
LDAA
#$13
;Loads the value $13 into acc. A
LDAB
$D000
;Loads the contents of address $D000 into acc. B
LDD
$D000
;Loads the contents of address
$D000 and $D001 into acc. D
LDX
#$2343
;Loads the value $2343 into register X
LDY
#$2343
;Loads the value $2343 into register Y
MULTIPLICATION: The command for this is MUL, it takes the contents of accumulator A and
multiplies it by the balue in accumulator B. The resulting output is saved to accumulator D.
Example:
LDAA
#$03
;Loads $03 into accumulator A
LDAB
#$03
;Loads $03 into accumulator B
MUL
;Multiplies $03x$03, $09 is stored in accumulator D
PUSH/PULL: The push pull commands are associated with the Stack(See Section 8), and are
used to push and pull values/memory addresses onto the stack. When data is pushed or pulled it
is stored at the memory location pointed to by the stack pointer.
Examples:
PSHA
;Pushes the contents of register A onto the stack
PSHB
;Pushes the contents of register B onto the stack
30
Programming the FOX11-68HC11 Board
PULA
;Pulls the top 8 bytes of the stack and stores the data
in accumulator A
PULB
;Pulls the top 8 bytes of the stack and stores the data
in accumulator A
STORE: Stores the values located in a register into a user specified memory address. Can be
used to store a value in a previously defined variable(such as those created using RMB)
Examples:
STAA
$D000
;Stores the contents of acc. A in address $D000
STAB
$D000
;Stores the contents of acc. B in address $D000
STD
$D000
;Stores the contents of acc. D in address $D000
And $D001
STX
$D000
;Stores the contents of acc. X in address $D000
And $D001
STY
$D000
;Stores the contents of acc. Y in address $D000
And $D001
SUB: Operates exactly as ADD but subtracts the values instead.
Examples:
SBA
;Subtracts accumulator B from A stores answer in A
SUBA
#$23
;Subtracts $23 from accumulator A
SUBB
#$23
;Subtracts $23 from accumulator B
31
Programming the FOX11-68HC11 Board
Section 5: Machine Code Sequences and Decoding Machine Language Instructions:
Machine code is the next step down from assembly language. This is code that the compiler uses
since it does not understand what, for example, LDAA really means. Machine code breaks this
down into a corresponding Hex value so that it can be understood by the compiler.
An example is the best way to show this process.
If the user writes the following commands:
 LDAA
#$34
 LDAB
#$20
The corresponding machine code for this is:
86
34
C6
20
The 86 is the corresponding machine code for the instruction LDAA, while C6 is the machine
code for instruction LDAB. The Hex values of $34 and $20 remain the same. Had a decimal or
other value been loaded it would have been necessary to convert it to a Hex value(as this is what
the compiler will see).
The machine codes and the corresponding instructions are shown below, this table comes from
the MC68HC11: An Introduction textbook.
01
86
96
C6
D6
CC
DC
8B
9B
CB
NOP
LDAA
LDAA
LDAB
LDAB
LDD
LDD
ADDA
ADDA
ADDB
IMM
DIR
IMM
DIR
IMM
DIR
IMM
DIR
IMM
32
Programming the FOX11-68HC11 Board
DB
C3
D3
97
D7
DD
ADDB
ADDD
ADDD
STAA
STAB
STD
DIR
IMM
DIR
DIR
DIR
DIR
As can be seen, the machine code used depends on the type of addressing(Section 2) being used
in the program. For example, had the examples above been done using direct addressing rather
than immediate, the codes would have been different:
LDAA
$34
LDAB
$20
The corresponding machine code is:
96
34
D6
20
Longer programs involve simply matching each instruction to its corresponding machine code.
33
Programming the FOX11-68HC11 Board
Section 6: Flowcharts:
Flowcharts are almost a necessity for programming the FOX11 board as it lets the user visualize
the logical flow of the program. This is done through the use of various symbols that show
points in the program where instructions are executed, where branches occur, and what the
program does during events. To show how a flowchart works example code is given below and
the corresponding flowchart follows in Figure 6. This is only done as an example, the user
should always create a flowchart before coding anything.
ORG $D000
Test
EQU #$05
loop
LDAA #$01
MPA Test
BEQ
loop
END
Fig. 6 - Example Flowchart
34
Programming the FOX11-68HC11 Board
In the above example it can be seen that this program will never loop, as $05 and $01 will never
be equal and thus the program will always exit. It is this kind of problem that a flowchart comes
in useful as faulty logic can quickly be seen.
Flowchart symbology:
There are a three symbols common to all flowcharts, these are:
Diamond: This is always associated with a branch or comparison of some kind, if statement A is
Yes then go to step B, if No, then repeat.
Rectangle: These are generally places in which some code is executed or variables initialized,
but there is no branching. Loading the value $05 into accumulator A would be done using this
symbol. A skewed rectangle would be used when loading variables into memory.
Oval: These are associated with beginning and ending the program, in Figure 6 it is “Start” and
“End”.
The overall complexity of a flowchart is completely dependent upon the task at hand, however it
is always better to break up multiple steps into separate blocks so that the logic behind the code
can easily be seen.
35
Programming the FOX11-68HC11 Board
Section 7: Mathematical Operations and the Flags of the Condition Code Register:
Condition Code Register:
The condition code registers track the outcome of mathematical operations that are performed
during a programs execution. It tracks whether there is an overflow during an addition, or it will
contain the result of a conditional operation such as a branch (Section 4).
When a program executes and performs a conditional operation it will store either a 1 or 0 in one
of eight condition code fields. These fields are as follows:
S: Stop Disable. Used in interrupts.
X: Interrupt Mask. Provides interrupt functionality
H: Half-Carry. Used during multi-precision operations
I: Interrupt Mask. Provides interrupt functionality.
N: Negative. If the result of an operation is a negative value this register will receive a 1
Z: Zero. If the result of an operation is a zero this register will receive a 1
O: Overflow. If a mathematical operation results in an overflow this register will receive a 1
C: Carry. A mathematical operation that results in a carry will load a 1 into this register
Multiprecision Arithmetic:
The condition code registers are often associated with multiprecision arithmetic. This is because
when an 8 bit processor performs arithmetic on numbers larger than one bit it requires the ability
to keep track of overflows and other conditions associated. The C register can be appended to
the end of the 8 bits allowing for track of 9 bit numbers and makes this possible.
36
Programming the FOX11-68HC11 Board
Section 8: The Stack and Indexable Data Structures:
The Stack:
The stack is a last in first out storage system. What this means is that as elements are pushed
onto the stack they are placed further down in the list. In order to get to an item placed in the
stack first(and therefore at the bottom), the user must do one of two things. Either all the
elements on top of the stack must be pulled off, or the user can use an offset and pull the element
out of the stack by using the address of the stack plus the offset value. In other words, if the
stack pointer is at address $8000, and the user knows that a value is 5 addresses down in the
stack. The following could be used to retrieve this value:
 TSX
;This loads the stack pointer into X
 LDAA 5,X
;This loads the 5th element in the stack into accumulator A
Because the stack exists in the SRAM memory it is possible to run out of storage room. Should
too many items be placed on the stack an “overflow” error occurs.
Conversely, it is possible for too many items to be pulled off the stack. In other words, there are
20 items on the stack and the user attempts to pull 25 items. This would result in an underflow
error where there is no information to be pulled.
One note on the stack and how the pointer is incremented/decremented. When looking at the
stack, the highest address is at the bottom of the stack. The lowest address is at the top. As
information is put onto the stack it is placed at the high address and then the stack pointer is
decremented. For example, if the stack pointer is at $8005 and an 8 bit value is pushed onto the
stack the stack pointer now moves to $8004. If it were a 16 bit value then the stack pointer
would move two places to point at $8003.
37
Programming the FOX11-68HC11 Board
The following are the commands used to place information on the stack(PUSH).
PSHA: This loads the contents of accumulator A onto the stack, the stack pointer(SP) is
decremented by 1.
PSHB: This loads the contents of accumulator B onto the stack, the SP is decremented by 1.
PSHX: This loads the contents of register X onto the stack, the SP is decremented by 2.
PSHY: This loads the contents of register Y onto the stack, the SP is decremented by 2.
The following commands are used to take information off the stack(PULL):
PULA: This takes the top 8 bytes of data off the stack and places it in accumulator A, the SP is
incremented by 1.
PULB: This takes the top 8 bytes of data off the stack and places it in accumulator B, the SP is
incremented by 1.
PULX: This takes the top 16 bytes of data off the stack and places it in register X, the SP is
incremented by 2.
PULY: This takes the top 16 bytes of data off the stack and places it in register Y, the SP is
incremented by 2.
Besides being able to push and pull information on the stack, there are other commands available
to the user, these are taken from MC68HC11: An Instroduction:
DES: Decrement the stack pointer by 1
INS: Increment the stack pointer by 1
LDS: This loads the contents of a memory location or immediate value into the SP
STS: Store the contents of the stack pointer in a memory location
TSX: Load the contents of the stack pointer plus 1 into register X
38
Programming the FOX11-68HC11 Board
TSY: Load the contents of the stack pointer plus 1 into register Y
TXS: Load the contents of index register X minus 1 onto SP
TYS: Load the contents of index register Y minus 1 onto SP
Indexable Data Structures:
Indexable data structures are simply lists of data that are easily indexed and accessed. These are
arrays(vectors) and matrices.
No matter which structure is being used they are initialized the same way, that is by using the
FCB or FDB instructions discussed in Section 4.
To create a vector:
Test
FCB
$01,$02, $03,$04
;Creates a list of values labeled Test
To create a matrix:
Mat_test
FCB
$01, $02,$03 ;This creates a 3x3 matrix holding elements $01-$09
FCB
$04,$05,$06
FCB
$07,$08,$09
Accessing the matrix is a simple task, assuming the user wanted to access the 5th element in the
array the following command would be used:
 LDX #Mat_test
;Loads the first address of the matrix into register X
 LDAA 5,X
;Loads the 5th element of the matrix into accumulator A
39
Programming the FOX11-68HC11 Board
Section 9: Subroutines:
Subroutines allow the user to create modular chunks of programs that can be accessed at
any point within a program. Programming languages such as C# operate upon the same
principles, the ideas behind subroutines is that a small main program can be created that are then
accessed in the main body. Once the subroutine performs a given function, a return call is made
that returns the program to its previous location within the main program. This also helps a
program to appear more streamlined and less cluttered and allows other programmers to quickly
understand the main components and logical flow behind the program.
Figure 9.1 shows a sample program containing a subroutine.
The main part of this sample program contains only five lines. The first sets the starting
memory address of the program. Lines 3 and 4 load accumulators A and B with the values $10
and $20. The command JSR is then used, this command tells the assembler to jump to the
subroutine labeled add_em.
Subroutine add_em is given a starting memory location by using the ORG $D100 command.
The first and only instruction performed by the subroutine is the ABA instruction, adding
accumulator B to accumulator A. Finally the RTS instruction is given. This returns to the main
program and starts over at the line in which the subroutine was originally called.
The following are the main instructions used in the creation and calling of a subroutine:
BSR: Branch to Subroutine. BRS is more restrictive than the JSR command, it requires that the
subroutine be contained in memory within -128 to 127 bytes from the memory location where
the call is being made.
Example:
BSR
add_em
;branch to subroutine add_em , provided the
40
Programming the FOX11-68HC11 Board
subroutine is within range.
JSR: Jump to Subroutine. This is a more robust version of BSR and will jump to any subroutine
as long as it is within 64 kilobytes of the memory location from where the call is being made.
Example:
JSR
add_em
;branch to subroutine add_em
RTS: Return from Subroutine. This returns to the previous memory address from where the
subroutine call was made.
Example:
RTS
;Automatically returns to main program
As can be seen, subroutines allow the user with the assembly language equivalent of object
oriented programming. Small subroutines can be created and used in multiple programs because
they are not as dependent on the main body of the program so they can easily be customized to
fit whatever needs the user has.
41
Programming the FOX11-68HC11 Board
Fig. 9-1 Sample Subroutine program
42
Programming the FOX11-68HC11 Board
Section 10: Buffalo Input/Output (I/O) Routines:
When memory was discussed in Section 2 it was mentioned that the FOX11 board contains built
in commands that are kept within the onboard read only memory(ROM). These commands are
essentially subroutines that are built in, providing the user a quick and easy way to perform many
common functions. These include functions that will read in commands from the keyboard,
output the contents of registers, or simply change characters stored in memory from lower case
to uppercase.
The following is a listing of the more common Buffalo monitor I/O routines.
DCHEK: Sets the Z condition code register if the value in accumulater A is a carriage return or
whitespace
INIT: Initializes the I/O device (i.e. keyboard)
INCHAR: This reads in an ASCII character into accumulator A, allows input from the keyboard
INPUT: Reads I/O device (i.e. keyboard)
OUTPUT: Writes to the I/O device(LCD screen, terminal window,etc)
OUTLHLF: Converts the left four bytes of accumulator A to an ascii character
OUTRHLF: Converts the right four bytes of accumulator A to an ascii character
OUTA: Outputs the ascii character contained in accumulator A
OUT1BYT: This takes the value in register X and outputs two ascii characters corresponding to
the values found within the register, it then points X to the next byte.
OUT1BSP: Performs the same action as OUT1BYT but also outputs a space after it outputs the
characters. Returns address in X that points to the next byte
OUT2BSP: Performs like OUT1BSP but outputs 4 characters.
OUTCRLF: Outputs carriage return
43
Programming the FOX11-68HC11 Board
OUTSTRG: This outputs an entire string pointed to by register X, a carriage return is where it
will stop outputting characters. Useful for outputting an entire string
OUTSTRGO: Performs exactly as OUTSTRG but skips carriage returns and line feeds
UPCASE: Outputs the upper case of whatever is in accumulator A
WCHEK: Tests accumulator A and sets the Z condition code register to 1 if the character is a
white space(space, comma, tab, etc.)
Along with the above I/O routines there are built in jump memory addresses that the user can use
so that rather than go through the trouble of writing a jump routing to an I/O routine, a memory
address can be used.
Example:
$FFA0 is equivalent to the command JMP UPCASE
So should the user wish to change the character in accumulator A to uppercase this command
would be used like this:
 JSR
$FFAO
This would automatically change the character in A to uppercase. The following is a listing of
the Buffalo Monitor I/O Routine Jump Table:
$FFA0
JMP UPCASE
$FFA3
JMP WCHEK
$FFA6
JMP DCHEK
$FFA9
JMP INIT
$FFAC
JMP INPUT
$FFAF
JMP OUTPUT
44
Programming the FOX11-68HC11 Board
$FFB2
JMP OUTLHLF
$FFB5
JMP OUTRHLF
$FFB8
JMP OUTA
$FFBB
JMP OUT1BYT
$FFBE
JMP OUT1BSP
$FFC1
JMP OUT2BSP
$FFC4
JMP OUTCRLF
$FFC7
JMP OUTSTRG
$FFCA
JMP OUTSTRGO
$FFCD
JMP INCHAR
The above commands allow the user easy ways in which to perform input and output routines
without having to create new subroutines.
45
Programming the FOX11-68HC11 Board
Section 11: Interrupts and Resets:
Interrupt:
An interrupt allows a way to stop execution of a program causing the CPU to pause whatever
action is being performed and perform a special action. This is useful for when information is
entered while the CPU is performing some other function, preventing data loss of the
information that was entered. It also allows for a way to exit a process if there is an error within
the process itself.
The general flow of events when an interrupt occurs is as follows:
 First, the program counter is stored so that once the interrupt has been dealt with the
process can start from where it left off.
 The CPUs’ actions are stored to memory. This ensures that if only a partial action was
completed the CPU will finish it once the program begins again.
 The reason for the interrupt is checked so that the CPU can take the necessary action.
 The program counter will jump to whatever process is being called in the interrupt, it will
then begin executing the process.
 Once the interrupt has been dealt with and the CPU is again free to work it loads the
information that was stored to memory and goes to the point that the program counter
was last pointing. The process that was interrupted is now started from where it left off.
Note: There are two types of possible interrupts that occur, one type can be ignored while the
second cannot. The first type that can be ignored is called a maskable interrupt, meaning the
CPU can finish whatever it was doing before dealing with the interrupt. The other that cannot be
ignored is called a nonmaskable interrupt. These are often things such as pressing a reset button,
or an error within the software that causes a problem the CPU cannot handle.
46
Programming the FOX11-68HC11 Board
Interrupt Vector:
When an interrupt occurs, there is a corresponding memory address that refers to where the CPU
should begin working. This memory address is the starting address of the routine that the CPU
will need to run in order to handle the interrupt. This is called the Interrupt Vector. Because
there are usually multiple interrupt vectors the memory addresses that correspond to different
vectors are stored in what is called an interrupt-vector table. This table allows the CPU to take
the memory address of the service routine, compare it to the table, and go to the corresponding
interrupt vector where the interrupt will be handled.
Writing/Handling the Interrupt Request:
When creating and dealing with an interrupt request there are three steps that need to be handled:
First, the vector interrupt table needs to be initialized. If a processor already has existing
interrupt vectors then this step can be ignored.
ORG
$D000
;$D000 would be whatever address the user has set the vector table
; to.
FDB
IRQ_ISR
;This is the address for the first interrupt
FDB
IRQ_ISR2
;This would be the second interrupt, there may be as many created
; as the user needs.
Next, the routines need to be created, these are essentially subroutines and perform much like a
subroutine except that the instructions are different.
IRQ_ISR
LDAA $45
;IRQ_ISR is telling the CPU that this is the name of
;this interrupt, after this the user can enter whatever
;commands are needed(LDAA was an example)
RTI
;This command returns from the interrupt to
47
Programming the FOX11-68HC11 Board
;wherever the CPU was at before the interrupt
;request.
Finally, the interrupts should be enabled, this is done by using the commands
CLI: This enables interrupts globally, usually done after the directives at the start of the
program(so that the first initializations of the program cannot be interrupted)
SEI: This disables interrupts, this would be used at points where it would be unwise to have an
interrupt because it could cause problems within the program.
The general outline of a program using interrupts would be as follows:
Initialize variables/Directives
ORG $D000
FDB
IRQ_ISR
CLI
;Enable interrupts
Main body of program
;This is the main program,
END
IRQ_ISR
;Interrupt subroutine begins here
Interrupt processes
;Perform interrupt services
SEI
;disable interrupts
RTI
;return back to main program
48
Programming the FOX11-68HC11 Board
References:
Huang, H.W., (2001). MC68HC11: An Introduction: Software and Hardware Interfacing. New
York: Delmar, Thomson Learning.
49