CS 273: Assembly Language Syntax

Probably the easiest way to describe the syntax of the assembly language is to give an example:


* HC11 example program
        org $f800    * tell the assembler to start generating code at $f800

fred
        ldaa  #$12   * load $12 into accumulator A
george  inca         * add one to A
        staa  $24    * store A out to address $24

        bra   george * infinite loop

        end   fred   * tell assembler to stop here

OK, so what do we see here?

  1. Every line of assembly code corresponds to either one instruction, or is an "assembler directive". An instruction, of course translates into one instruction for the HC11 to execute; an assembler directive controls the operation of the assembler itself. This example shows both (as we'll see in a moment).
  2. Each line that isn't a comment has an optional label, an op code or directive, any operands needed by the op code or directive, and an optional comment. A line can contain a comment all by itself, in which case it starts with an asterisk in column 1.

Let's look at some of the lines of example program in more detail:

* HC11 example program
A comment. The * must be in the first column.
        org $f800    * tell the assembler to start generating code at $f800
An assembler directive, telling the assembler to generate code starting at $f800. This is the beginning of the on-chip EEPROM.
fred
A label on a line by itself
        ldaa  #$12   * load $12 into accumulator A
An instruction. The op code comes first, and specifies both the instruction and the accumulator it operates on. This instruction takes on operand; we're telling it we want to use immediate addressing (that's what the # means) and to load $12 into the A accumulator.
george  inca         * add one to A
A labelled instruction. The label is george, while the instruction is inca. This instruction takes no operands.
        staa  $24    * store A out to address $24
This intstruction stores the contents of the A accumulator to address $24 — this is called direct addressing. Note: this is the single most confusing part of the syntax of assembly language programming: in high level languages, whether we use direct or immediate addressing is determined by context. In assembly code, we have to say which we mean! An astonishing number of bugs are caused by doing a load with direct addressing when immediate was desired.
        bra   george * infinite loop
A branch always instruction. Assembly language has no high-level constructs like for loops: we have to implement them by hand.
        end   fred   * tell assembler to stop here
Another assembler directive. This one tells the assembler that we're all done, and also tells it where to start the program.

Assembling an Instruction

Last time, we looked at an instruction, and saw how to decode and execute it. This time, let's look at how to assemble an instruction. We'll use the staa $24 as an example.

  1. Look up the addressing mode in the Motorola Freeware 8-Bit Cross Assemblers User Manual. Actually, you'll get these memorized so quickly that you won't have to look them up... A plain ol' operand, like we've got here, is Direct Addressing.
  2. Look up the instruction and addressing mode in the Reference Guide or the Reference Manual. staa is on page 19 of the Reference Guide, and Page 579 of the Reference Manual. In both cases, you can see that the op code is 97, and the op code is one byte of address. So the instruction assembles to 97 24.

Last modified: Mon Mar 15 11:09:35 MDT 2010

Valid HTML 4.01 Transitional