Hello World for HC11 Mobile Robots

Everybody's heard of the ``Hello World'' program - so far as I know, it first turned up in the original Bell Labs report on C; it's a trivial little program that just prints the phrase Hello World to the screen, and quits. Since then, it's become popular to write a ``hello world'' for whatever language you want to describe to people. The purpose is to let people get a very, very quick insight into the basic syntax and flavor of a language.

In the case of the HC11 miniboard, actually printing hello world would be a daunting task - step 1 would be writing to the serial port driver (that actually maybe an assignment toward the end of the semester). Probably the closest thing to the spirit of a Hello World is the following HC11 assembler program:


        org   $f800      * generate code starting at $f800 (start of EEPROM)
fred    ldaa  $1003      * read the sensors
        staa  $1004      * write them to the motors (unchanged!)
        bra   fred       * go 'round and do it again

        org   $fffe      * generate code for RESET interrupt
        fdb   fred       * start program at ``fred'' (that's $f800)

There's actually a whole lot of information packed into those six lines of code; let's cover what we can. First, some notes on the assembler; we'll talk about how the code works later.

Code Notes

Compiler directives vs. instructions.
An assembler can recognize both instructions to be translated for the HC11, and also directives telling the assembler itself what to do. Unfortunately, there is no syntactic difference between them (some assemblers do things like start assembler directives with a period; ours doesn't), so you just have to remember. In this case, org is an assembler directive; it tells the assembler where to put the code it's creating. This is the sort of thing you have to worry about in assembler that you don't need to worry about in a high level language. Everything else in the example is either an instruction, or data.


Labels
Assembly language gives you no help whatever for writing structured code. So, the only flow of control you've got is with gotos and conditional gotos. In our assembler, anything that starts in column 1 is a label for a goto or an information variable that will be used as a directive to the assembler itself; anything that starts in any other column is an instruction, data, or an assembler directive.

The assembler remembers the address where labels happen (in this case, the address is $f800 since we'd just told the assembler to generate code there, and we hadn't put any new code or data in since). So now, whenever it sees fred, it'll put in the number $f800.

As it happens, this is going to be where we start executing, so it would have made more sense to call it start or begin or main or something. But it's easy to start thinking the name of the start label is magic somehow (like main() in C), so I want to emphasize that we could call it anything we want.

Putting in data
The line that says fdb fred tells the assembler to put ``fred'' at the current address (which is $fffe). The fdb directive says to ``form a double byte,'' or a 16 bit quantity. So it puts $f800 (remember, that's the value of fred) into addresses $fffe and $ffff. This is going to end up telling the HC11 where the program starts, as we'll see in a minute.


No exit()!
In a workstation environment, the exit() statements says to stop executing the current program, and turn control back over to the command interpreter. In our environment, there is no command interpreter; this program runs until the power is turned off.

How the Code is Executed

Startup
When you turn on the miniboard, or you press the reset button on the board, the CPU ``wakes up'' and has to figure out where to begin executing. It does this by looking at addresses $fffe and $ffff for a 16 bit number; it then loads this number into the PC and starts executing. Why $fffe and $ffff? Because that's where Motorola decided to have it look; actually, that's a reasonable place, if you think about it - not in RAM, and a place that can be reserved for the purpose regardless of which member of the HC11 family you're working with.


What the Code Does
There are just three lines of executable code: one that loads the digital sensors into the A accumulator, one that stores the A accumulator out into the motor drivers, and one that says ``go do it again.''

How It Works

Reading the Digital Input

Cleaning Up The Program

The code shown on this web page will work, but suffers from having a lot of in-line constants and (deliberately) poorly chosen labels. Here is the complete program, using symbols properly.