These mostly follow the MOS Technology 6500 Microprocessor Familiy Programming Manual, except for the Accumulator mode.
RTS
LSR
LDA #$06
LDA $7C
LDA $7C,X
LDA $7C,Y
LDA $D020
LDA $D000,X
LDA $D000,Y
LDA ($80, X)
LDA ($80), Y
JMP ($A000)
BNE loop
Most arguments are just a number or label. The formats for these are below.
$41
(Prefixed with $)65
(No markings)0101
(Prefixed with
zero)%01000001
(Prefixed with
%)'A
(Prefixed with
single quote)Normal labels are simply referred to by name. Temporary
labels may be referenced with strings of - or + signs (the
label -
refers to the immediate previous temporary
label, --
the one before that, etc., while +
refers to the next temporary label), and the special label
^
refers to the program counter at the start of the
current instruction or pragma.
Normal labels are defined by prefixing a line with
the label name and then a colon (e.g., label:
).
Temporary labels are defined by prefixing a line with an
asterisk (e.g., *
).
Strings are enclosed in double quotation marks. Backslashed
characters (including backslashes and double quotes) are
treated literally, so the string "The man said, \"The \\
character is the backslash.\""
produces the ASCII sequence
for The man said, "The \ character is the
backslash."
Strings are only used as arguments to assembler pragmas -
usually for filenames (e.g., .include
) but also for
string data (e.g. .ascii
).
Arguments consist of three components: The prefix, the core, and the offset. All but the core are optional.
There are two valid prefixes. The prefix '>
'
causes the value to be the high byte of the eventual
result. This is equivalent to dividing the value by 256.
Likewise, the prefix '<
' returns the low
byte.
The core of the argument is a simple number or label.
The offset is a number that is added or subtracted to the core.
Examples:
$D000
<-- evaluates to $D000$D000+32
<-- evaluates to $D020$D000+$20
<-- also evaluates to $D020<$D000+32
<-- evaluates to $20>$D000+32
<-- evaluates to $D0, not
$F0!>$D000-275
<-- evaluates to $CEIn order to properly compute the locations of labels and the like, P65 must keep track of where assembled code will actually be sitting in memory, and it strives to do this in a way that is independent both of the target file and of the target machine.
The primary technique P65 uses is "program counter tracking." As it assembles the code, it keeps track of a virtual program counter, and uses that to determine where the labels should go. (It's a little more complicated than this, thanks to some properties of the instruction set architecture, but that's beyond the scope of this document. See the implementation notes if you're morbidly curious.)
In the absence of an .org
pragma, it assumes a
starting PC of zero. .org
is a simple pragma, setting
the PC to the value that .org
specifies. In the
simplest case, one .org
pragma appears at the
beginning of the code and sets the location for the rest of the
code, which is one contiguous block.
However, this isn't always practical. Often one wishes to have a region of memory reserved for data without actually mapping that memory to the file. On some systems (typically cartridge-based systems where ROM and RAM are seperate, and the target file only specifies the ROM image) this is mandatory. In order to access these variables symbolically, it's necessary to put the values into the label lookup table.
It is possible, but inconvenient, to do this with
.alias
, assigning a specific memory location to each
variable. This requires careful coordination through your code,
and makes creating reusable libraries all but impossible.
A better approach is to reserve a section at the beginning
or end of your program, put an .org
directive in, then
use the .space
directive to divide up the data area.
This is still a bit inconvenient, though, because all variables
must be assigned all at once. What we'd really like is to keep
multiple PC counters, one for data and one for code.
The .text
and .data
directives do this.
Each has its own PC that starts at zero, and you can switch
between the two at any point without corrupting the other's
counter. In this way each function can have a .data
section (filled with .space
commands) and a
.text
section (that contains the actual code). This
lets our library routines be almost completely self-contained -
we can have one source file that could be .included
by
multiple projects without getting in anything's way.
However, any given program may have its own ideas about
where data and code go, and it's good to ensure with a
.checkpc
at the end of your code that you haven't
accidentally overwritten code with data or vice versa. If your
.data
segment did start at zero, it's probably
wise to make sure you aren't smashing the stack, too (which is
sitting in the region from $0100 to $01FF).
If you write code with no segment-defining statements in it,
the default segment is text
.
The text and data segments are usually sufficient, but for
the cases where it is not, P65 allows for user-defined
segments. To switch to an arbitrary statement, just issue the
command .segment name
. The name follows the
same naming rules as labels.
Say, for example, that we have access to the RAM at the low end of the address space, but want to reserve the zero page for truly critical variables, and use the rest of RAM for everything else. Let's also assume that this is a 6510 chip, and locations $00 and $01 are reserved for the I/O port. We could start our program off with:
.data .org $200 .segment zp .org $2 .text .org $800
And, to be safe, we would probably want to end our code with checks to make sure we aren't overwriting anything:
.data .checkpc $800 .segment zp .checkpc $100
Note that the .text
and .data
commands are
actually implemented in terms of .segment
, so if you
wish to have a uniform approach, you can say .segment
text
and .segment data
instead.
Assembler pragmas are all instructions to the assembler that are not actual instructions. Currently implemented pragmas are:
.advance
address: Forces the
program counter to be address. Unlike the
.org
pragma, .advance
outputs zeroes until
the program counter reaches a specified address. Attempting
to .advance
to a point behind the current program
counter is an assemble-time error..alias
label value: The
.alias pragma assigns an arbitrary value to a label. This
value may be an arbitrary argument, but cannot reference any
label that has not already been defined (this prevents
recursive label dependencies)..ascii
string: This indicates
ASCII data. One string may be specified per .ascii
directive..byte
arg [ , arg, ... ]:
Specifies a series of arguments which are evaluated. The
final results of these arguments must be one byte in size.
Seperate constants are seperated by comments..checkpc
address: Ensures that the
program counter is less than or equal to the address
specified, and emits an assemble-time error if it is not.
This produces no code in the final binary - it is there to
ensure that linking a large amount of data together does not
overstep memory boundaries..code
: Equivalent to .segment
text
..data
: Equivalent to .segment
data
..incbin
filename: Inserts the
contents of the file specified as binary data. Use it to
include graphics information, precompiled code, or other
non-assembler data..include
filename: Includes the
entirety of the file specified at that point in the program.
Like .link
, but uses the current address as the base
address. Use this in your sources to put them all
together..link
filename address: Assembles
the file specified as if it began at the address specified.
This is generally for use in "top-level" files, where there
is not necessarily a one-to-one correspondence between file
position and memory position. This is equivalent to an
.org
directive followed by an .include
.
With the introduction of the .org
pragma this one is
less useful (and in most cases, any .org
statement
you use will actually be at the top of the .include
d
file)..org
address: Sets the program
counter to the address specified. This does not emit any
code in and of itself, nor does it overwrite anything that
previously existed. If you wish to jump ahead in memory,
use .advance
..segment
segmentname: Sets the
current segment to the name specified. If the segment has not
yet been referenced, its program counter is initialized to
zero..space
label size: This
pragma is used to organize global variables. It defines the
label specified to be at the current location of the program
counter, and then advances the program counter size
steps ahead. No actual code is produced. This is equivalent
to label: .org ^+size
..text
: Equivalent to .segment
text
..word
arg [ , arg, ... ]:
Like .byte
, but values are all treated as two-byte
values and stored low-end first (as is the 6502's wont). Use
this to create jump tables (an unadorned label will evaluate
to that label's location) or otherwise store 16-bit data.
This directive is also called .address
for
historical reasons, but use of .address
is
deprecated.