Here is an example Micro Autocode program,
and here is the assembler code produced by the
A Micro Autocode instruction consists of one line of text. There is no
way of putting more than one instruction on a line, and no way of
splitting an instruction over more than one line. One line, one
Comments are begun at the first semicolon in a line, and extend to the
end of the line, just like in assembler:
i[0x11]=255 ; set port D to all output
You can include assembler anywhere in a program by enclosing it in
braces (each on their own line):
To avoid bloating Autocode unnecessarily, this is the only way to:
switch the interrupt flag on or off, sleep or kick the watchdog.
cli ; disable interrupts
A Micro Autocode program consists of a prologue followed by an
arbitrary number of chapters. There are two types of instruction that
may be used in the prologue:
The vector instruction
This instruction defines which chapters handle the reset, interrupt and
[-->start, -->interrupt1, -->interrupt2, -->interrupt3]
where "start", is a chapter name, and "interrupt1", "interrupt2" and
service names. You will get a warning if you don't give the right
number of vectors. If you are not compiling for the AT90s1200 then
change the "vectors" option to suit.
The declaration instruction
This instruction assigns a meaningful name to a storage location.
Unlike other programming languages you have to specify the exact
location of each variable that you are going to use.
this --> r
Here we see that "this" is stored in register r20, "abit" is stored in
bit 3 of register r1, "DDRD" is equivilant to I/O location 11(hex), and
"State" is stored in EEPROM location 3.
abit --> r.3
DDRD --> i[0x11]
State --> n
Here are the double length declarations:
count --> s
So "count" is stored in registers r2 and r3, and "id" is stored in
EEPROM locations 4 and 5.
id --> m
You do not need to declare a name for every location; you can use
i[...] , n[...] and m[...] in any instruction where a name would go.
(Using r[...] and s[...] in instructions is allowed, but not
recommended.) And when you are doing so, the index can be an expression.
Identifiers cannot contain numbers, only A-Z, a-z and the underscore.
When you are compiling for controllers that support reading program
memory you also have p[...] (single length) and q[...] (double length)
So the variable types are:
|| single length registers and RAM.
|| double length registers and RAM.
|| single length EEPROM.
|| double length EEPROM.
|| single length I/O.
|| single length program memory.
|| double length program memory.
The rest of the program consists of a succession of chapters. There are
two types of chapters: chapters and services. A service is identical to
a chapter, but is automatically closed with a RETI instruction, so they
are used for interrupt service routines.
This declares a chapter called "start".
chapter wait, 1
This declares a chapter called "wait", which will use register set 1.
MicroAutocode needs to allocate temporary registers from time to time;
for example to hold partial results in expressions and loop bounds.
Temporary registers are allocated from r29 downwards. Since there is no
stack to store variables on, each subroutine and each
interrupt routine must use a different set of temporary registers.
chapter with the same register set will use the same set of temporary
registers. You should allocate register sets to chapters so that
temporary registers do not get overwritten. If you have a main chapter
that calls a sub-chapter then they must have different register sets.
you have two sub-chapters that do not call each other then they can
the same register set. Similarly interrupt service chapters must have a
different register set to any chapters that might be active when an
Defines an interrupt service chapter called "interrupt" which uses
register set 2.
Within a chapter the following instructions are allowed:
state = 1 - state
the expressions on the right-hand-side can use the following operators:
But where is the multiply? It doesn't need an operator. You just
write the two operands next to each other:
product = 2single
Just as if you were writing a mathematical formula.
There are no parentheses. This is a limitation of the original
Autocode, and it limits the number of temporary registers the compiler
has to allocate.
Multiplication and division are only supported with at least one of the
operands being a number, and that number being a power of two. This
reduces the code size. At some point multiplication by any number will
be supported, but division by any number and operations between two
variables will never be supported.
counter = 10(1)20
This defines what most modern languages call a "for" loop. "counter" is
initialised to 10, and then incremented by 1 each time through the loop
until it reaches 20.
Each of the three values can be arbitrary expressions.
The test is for equality, so the counter has to actually hit the end
value exactly. If it doesn't the loop will just keep going until it
does. This allows forward and backward loops at no extra cost, but it
does mean that you have to be that little bit careful. The loop will be
executed for the initial value, and for each value upto but not
including the end value.
You can not depend on when or how often the values are calculated.
(Because I intend to change it.)
The loop will be executed repeatedly until the condition becomes false.
This is a new instruction in Micro Autocode, it was not included in the
The conditional operators are:
Note the form of "not-equal", which is closer to the original glyph
than the != I could have used but didn't.
Jumps and Labels
Labels are defined by putting them on their own line followed by a
Jumps take two forms, unconditional, and conditional:
You can only jump within the same chapter.
--> AnotherLabel, x < 4
Up, Down and Across
Not an insane crossword puzzle, these are the instructions that let you
call or jump to other chapters:
Calls the chapter "AnotherChapter" as a sub-chapter.
Returns from a sub-chapter to whichever chapter called it. (Yes it's a
Jumps to the chapter "YetAnotherChapter". You cannot return with "up",
haven't used up any valuable stack locations, and you've modularised
It's not usually good practise to jump into the middle of a
chapter, but sometimes you have to squeeze every last byte out of your
code, and since the original language provided the functionality so do
The first of these calls the chapter "AnotherChapter" as a sub-chapter,
entering it at its label "AnotherLable". The second jumps to the label
"YetAnotherLabel", which is within the chapter "YetAnotherChapter".
More on bits
The bit operator "." can be specified in the declarations or in later
instructions. Bit numbers must be numbers, they cannot be expressions.
abit --> r.1
Dont expect this to produce tight code, AVRs are not good at this sort
of thing. However tight code is produced by instructions of the form:
this --> r
abit = this.1
All controllers have different capabilities and requirements. The
.option directive allows you to specify those differences. Put them at
the beginning of the file, before anything that they might affect. An
option has a name and a value. It is specified as follows:
Note the dot at the beginning and the quote characters around the name.
The following options are defined for the AVR series:
|Loading program memory is not
supported. The use of variables p[...] and q[...] will result in errors.
|Loading program memory is
supported, but only to R0.
|Loading program memory to any
register is supported.
|Auto-incremented indirect mode
(Z+) is not supported. Extra code will be used to increment Z when
|Auto-incremented indirect mode
(Z+) is supported.
|Both r30 and r31 are used in (Z)
|Only r30 is used in (Z) indirect
mode. Reading program memory still uses both r30 and r31.
|The given number of vectors are
And that's it. the entire language.
Invoking the compiler
The compiler only understands two flags at the moment so this is dead
autocode [-d] [-o outputfile] [inputfile]
If the input file or the output file are not specified then it reads
from standard in, and writes to standard out. The -d flag is for
debugging the parser; you don't need it.
The output of the compiler is AVR assembler suitable for the GNU gas
Last Edited - R Urwin - 18 September 2004