xasm - 6502 cross-assembler
xasm is a cross-assembler for the 6502 family of processors.
SOURCE_FILE is the name of the source file
(you may omit the default .asx extension).
Using - as SOURCE_FILE makes xasm read from standard input.
When invoked without any options, xasm assembles SOURCE_FILE
and writes the result to an object file named SOURCE_FILE
with the extension changed to .obx.
- -c
-
The listing should include conditionally skipped lines.
- -d LABEL=VALUE
-
Defines a label. LABEL should be a valid label name. VALUE may be any expression (potentially referencing labels defined in source files). You may use several -d options to define many labels on the command line.
- -i
-
The listing file should exclude included files.
- -l [LISTING_FILE]
-
Generates a listing file. Using - as LISTING_FILE makes xasm write to standard output. If LISTING_FILE is omitted, the listing filename is SOURCE_FILE with the extension changed to
.lst. - -M
-
Prints a
Makefilerule. The first line of the rule lists OBJECT_FILE as the target of the rule and all source files (including the ones specified withiclandins) as dependencies. The second line contains the command line with OBJECT_FILE replaced by the make macro$@and SOURCE_FILE replaced by the macro$<. Dollar signs in the command line are doubled. Your make or shell may require further escaping. - -o OBJECT_FILE
-
Sets the output file name. Using - as OBJECT_FILE makes xasm write to standard output. The default is SOURCE_FILE with the extension changed to
.obx, or standard output if standard input was specified as the source. - -p
-
Prints absolute paths in the listing and in error messages.
- -q
-
Quiet mode. Prevents xasm from printing its banner and compilation summary.
- -t [LABEL_FILE]
-
Generates a label table. Using - as LABEL_FILE makes xasm write to standard output. If LABEL_FILE is omitted, the table is appended at the end of the listing.
- -u
-
Issues warnings for unreferenced labels.
Alternatively, you may use DOS-style options, for example:
xasm /i /d:DEBUG=1 /l:listing.lst source.asxThese are deprecated because they are incompatible with MSYS2.
Source files should be plain ASCII files. LF, CR, CR/LF and Atari ($9b) line terminators are supported. Labels and instructions are case-insensitive.
xasm is backward compatible with Quick Assembler.
To compile QA sources with xasm, simply replace ATASCII characters
in string literals with the corresponding integers.
Also update all OPT directives, but often you can omit them.
A label is a symbol that represents a signed 32-bit integer.
You define a label by putting its name at the beginning of a line
(with no spaces before it).
The label will be assigned the current value of the origin counter
(that is, the address of the compiled instruction),
unless you use it with the EQU directive to assign the specified value.
Any label name starting with a ? (question mark) is a local label.
It is implicitly prefixed with the name of the most recently defined
global label (that is, a label without any ? in its name),
and remains visible until another global label is defined.
It is still possible to access a local label from anywhere in the source
by specifying its full name.
Local labels provide a way to reuse common, short label names while keeping
them unique.
Example:
foo ldy #0
?loop lda data,y ; full label name is FOO?LOOP
beq ?ret
jsr sendByte
iny:bne ?loop
?ret rts
bar lda baz
beq foo?ret ; ok
bne ?loop ; ERROR: Undeclared label: BAR?LOOPInstructions and directives must be preceded with some whitespace. Without leading whitespace they are treated as label names. For example:
nopis a 6502 instruction, whereas
nop(without leading space) defines a label called nop.
Whole-line comments must start with a semicolon, an asterisk or a pipe, optionally preceded by a label definition and spaces. Here are examples of whole-line comments:
; this is a comment
* so it is
label | and this tooLines with instructions (and selected directives) may be repeated. To assemble a single line several times, precede the repeat count with a colon, for example:
:4 asl @
mask_lookup :32 dta $80,$40,$20,$10,$08,$04,$02,$01In lines with instructions or directives, a comment starts immediately after the instruction/directive has been successfully parsed. That is, in such lines xasm does not require any special character to start a comment.
lda foo ; this is a comment
sta bar this too
tax #0 tax has no operand, therefore #0 starts this commentYou may put two or more instructions in one line, so they share their operand. For example:
eor:sta foois equivalent to
eor foo
sta fooNote that
lda:tax:tay #0is allowed because #0 is treated as a comment for tax and tay.
Expressions are numbers combined with operators and brackets. You should use square brackets, because parentheses are reserved for 6502 indirect addressing.
A number is:
-
a 32-bit decimal integer, e.g.
12345 -
a 32-bit hexadecimal integer, e.g.
$abcd -
a 32-bit binary integer, e.g.
%10100101 -
an ASCII character, e.g.
'a'or"a" -
the current value of the origin counter:
* -
a hardware register (see below), e.g.
^4e
Abbreviations of Atari hardware registers are provided
to save two characters ($d40e vs ^4e)
and to facilitate porting software between Atari 8-bit computers
and the Atari 5200 console.
These are very similar machines; one of the biggest differences
is the location of hardware registers.
| Syntax | Chip | Value | Value in Atari 5200 mode (opt g+) |
|---|---|---|---|
|
GTIA |
|
|
|
GTIA |
|
|
|
POKEY |
|
|
|
PIA |
|
error (there’s no PIA chip) |
|
ANTIC |
|
|
Atari 5200 mode is enabled with opt g+ or opt 'hardware=atari5200'.
The abbreviations can also be rejected altogether with opt 'hardware=none',
which is useful if the code targets neither of these machines.
The opcode syntax represents the opcode byte of the instruction inside braces.
The operand of the instruction is discarded and is needed only to recognize
the addressing mode. The instruction should begin right after the left brace
and the right brace should immediately follow the operand or the instruction.
You can skip the operand if the addressing mode is fixed.
Examples: {lda #}, {jsr}, {bne}, {jmp ()}, {sta a:,x}.
You can use the line repeat counter (#) in the repeated lines.
It counts the iterations, starting from zero. Examples:
:3 dta # ; generates three bytes: 0, 1, 2.
line_lo :192 dta l(screen+40*#)
line_hi :192 dta h(screen+40*#)
dl :59 dta $4f,a(screen+40*#),0,$4f,a(screen+40*#),0def(label) is 1 if the label is defined and 0 if it is not.
It never reports an undeclared label.
A label counts as defined only if it is declared on the command line
with -d or on an earlier source line
that was not skipped by a false condition.
A label declared later in the source is not defined,
consistently in both passes of the assembly.
Labels with a value of zero are defined.
def(?local) tests a local label
of the most recent global label.
def(label) is most useful with IFT.
The default value of a label that can be overridden on the command line:
ift !def(speed)
speed equ 3
eifInclude guard, so that lib.asx can be included several times:
ift !def(lib_included)
lib_included equ 1
; ...
eifThe following binary operators are supported:
-
+Addition -
-Subtraction -
*Multiplication -
/Division -
%Remainder -
&Bitwise AND -
|Bitwise OR -
^Bitwise XOR -
<<Arithmetic shift left -
>>Arithmetic shift right -
==Equal -
=Equal (same as==) -
!=Not equal -
<>Not equal (same as!=) -
<Less than -
>Greater than -
<=Less than or equal -
>=Greater than or equal -
&&Logical AND -
||Logical OR
The following unary operators are supported:
-
+Plus (no operation) -
-Minus (changes the sign) -
~Bitwise NOT (complements all bits) -
!Logical NOT (changes true to false and vice versa) -
<Low (extracts the low byte) -
>High (extracts the high byte)
Although the operators are like those used in C, C++, C# and Java, their precedence is different:
-
first:
[](brackets) -
+ - ~ < >(unary) -
* / % & << >>(binary) -
+ - | ^(binary) -
= == <> != < > <= >=(binary) -
!(unary) -
&&(binary) -
last:
||(binary)
The comparison and logical operators assume that zero is false and any non-zero value is true. They return 1 for true.
Expressions are calculated in signed 32-bit arithmetic. An overflow is signaled with an "Arithmetic overflow" error.
- EQU - assign the value of an expression to a label
-
Examples:
five equ 5 here equ *
- OPT - set assembler options
-
The operand is either a sequence of single-letter switches or a quoted list of
name=valuesettings. Six switches are available:-
F- fill the space between noncontiguous memory areas with$FFbytes -
G- Atari 5200 mode for hardware register abbreviations (^xx) -
H- generate Atari executable headers -
L- write the listing -
O- write the object file -
U- warn of unused labelsYou can turn any of these on or off. The default (before the first
OPT) isopt f-g-h+l+o+u+. For compatibility with MADS,opt ?+is accepted and ignored. Examples:opt l- listing off opt l+o- listing on, object file off opt f+g+h- useful for Atari 5200 cartridges - raw output, 5200 hw regs opt ?+ MADS compatibility, no effect
Alternatively, the operand is a string in single or double quotes, holding a comma-separated list of settings, each written as
name=value:opt 'listing=off' opt 'output=rom,hardware=atari5200' opt "WARN_UNUSED_LABELS=Off"
The following settings are available:
-
hardware=atari800-^xxare the hardware registers of Atari 8-bit computers (the default, same asopt g-) -
hardware=atari5200-^xxare the hardware registers of the Atari 5200 (same asopt g+) -
hardware=none-^xxis rejected -
listing=on/listing=off- same asopt l+/opt l- -
warn_unused_labels=on/warn_unused_labels=off- same asopt u+/opt u- -
output=ataridos- write an Atari DOS executable file (the default, same asopt f-h+o+) -
output=raw- write the bytes with no headers (same asopt f-h-o+) -
output=rom- as above, but fill the space between noncontiguous memory areas with$FFbytes, which is what cartridge images need (same asopt f+h-o+) -
output=off- write no object file (same asopt f-h-o-)
Names and values are case-insensitive. No spaces are allowed inside the string.
outputmay be set only once in the source, and only before the first byte of the object file is emitted. Onceoutputis set, theF,HandOswitches are rejected. -
- ORG - set the origin counter
-
If Atari executable headers are enabled (
opt h+), you can include an operand prefix:-
a:starts a new block even if it’s superfluous because the new address equals the current address. -
f:is the same asa:, but additionally generates a double$FFprefix before the new header. This prefix is automatically generated at the beginning of the file (no need to includef:in the firstORG).
Examples:
org $600 org f:$700 table org *+100
In the last example,
tablepoints to 100 bytes of uninitialized data (the label is assigned*before theORGdirective is executed).xasm supports code that is relocated at run time. Let’s say you want your code to be located on page zero for best performance. You can’t safely load it directly into this place, so you load it at a different address and then move it in your program.
org r:changes the address used for code generation but not the address used for generating Atari executable headers. Example:org $8000 ldx #code_length-1 mva:rpl code_loaded,x z:code_zpage,x- jmp code_zpage code_loaded org r:$30 code_zpage jmp * ; ... or something more sensible code_length equ *-code_zpage
Note that both
*and label definitions use the counter used for code generation. There is no direct access to the other counter. You can only calculate it:where_am_i equ *-code_zpage+code_loaded
-
- .DS - define storage
-
Advances the origin counter by the given number of bytes. It’s normally equivalent to
ORG *+xwherexis the.DSoperand. It behaves differently in the scope oforg r:where it advances both the code-generation counter and the load counter.Example:
ptr .ds 2
- ALN - align the origin counter
-
Advances the origin counter to the specified boundary. It’s equivalent to
.DS -*&[b-1]wherebis theALNoperand.Example:
aln $400 font ins 'panther.fnt'
- DTA - define data
-
-
integers
-
bytes:
b(200)or simply200 -
16-bit words:
a(10000) -
low bytes of words:
l(511)(byte 255) -
high bytes of words:
h(511)(byte 1) -
24-bit integers:
e($123456) -
32-bit integers:
f($12345678)
You may enter many expressions in parentheses and combine different types of data on a single line, separating things with commas.
You may also build a sine lookup table. The syntax is:
sin(center,amp,period,first,last)
where:
-
centeris an integer which is added to every sine value -
ampis the sine amplitude -
periodis the number of values per sine period -
first,lastdefine the range of sine arguments. They are optional. The defaults are0,period-1.
Example:
dta a(sin(0,1000,256,0,63))
defines a table of 64 words representing a quarter of a sine with an amplitude of 1000.
-
-
real numbers:
r(-1.23456e12)Real numbers are stored in the 6-byte Atari Floating-Point format.
-
text strings
-
ASCII strings:
c'Text'orc"Text" -
ANTIC strings:
d'Text'ord"Text"
A character string consists of any number of characters surrounded by quotation marks. You can include the quotation marks in the string by doubling them. If the string is suffixed with a
*character, bit 7 in every byte gets inverted.Examples of
DTA:dta b(1,2),3,a(1000,-1),l(12345,sin(0,127,256)) dta d"ANTIC"*,c'It''s a string',$9b
-
-
- ICL - include another source file
-
Specifies another file to be included in the assembly as if the contents of the referenced file appeared in place of the
ICLstatement. The included file may contain otherICLstatements. The.asxextension is added if none is given. Examples:icl 'macros.asx' icl 'lib/fileio'
NoteFor Windows/macOS/Linux portability, use relative paths and slashes as path separators. - END - end this source file
-
May be used if the source file ends with something which shouldn’t be read by xasm (e.g. your notes).
- INS - insert binary file contents
-
Copies every byte of the specified file into the object file and updates the origin counter, as if these bytes were specified in a
DTA. You may specify a range of the file to insert. The syntax is:ins 'file'[,offset[,length]]
The first byte in a file has an offset of zero. If the offset is negative, it counts from the end of the file. Examples:
ins 'picture.raw' ins 'file',-256 ; insert the last 256 bytes of the file ins 'file',10,10 ; insert bytes 10..19 of the file
- RUN - set the Atari executable run address
-
run main
is equivalent to:
org $2e0 dta a(main)
- INI - set the Atari executable init address
-
Example:
ini showloadingpic
- ERT - abort the assembly with an error if an expression is true
-
Examples:
ert *>$c000 ert len1>$ff||len2>$ff
- IFT - assemble if expression is true
- ELI - else if
- ELS - else
- EIF - end if
-
With these directives, you can construct fragments which are assembled only when a condition is met. Conditional constructions can be nested. Example:
noscr equ 1 widescr equ 1 ift noscr lda #0 eli widescr lda #$23 els lda #$22 eif sta $22f
To test whether a label is defined, use
def(label), e.g.ift def(debug).NoteAlternatively, the above example can be written using the repeat line feature: noscr equ 1 widescr equ 1 :noscr lda #0 :!noscr&&widescr lda #$23 :!noscr&&!widescr lda #$22 sta $22f
Pseudo commands are built-in macros. There are no user-defined macros in xasm.
- ADD - add without carry
-
If you have ever programmed a 6502, you must have noticed that you had to use a
CLCbeforeADCfor every simple addition.xasm can do it for you.
ADDreplaces two instructions:CLCandADC. - SUB - subtract
-
It is
SECfollowed bySBC. - RCC, RCS, REQ, RMI, RNE, RPL, RVC, RVS - conditional repeat
-
These are branches to the previous instruction. They take no operand, because the branch target is the address of the previously assembled instruction or pseudo command. Example:
ldx #0 mva:rne $500,x $600,x+
The above code copies a 256-byte memory block from $500 to $600. Here is the same code written with standard 6502 commands only:
ldx #0 copy_loop lda $500,x sta $600,x inx bne copy_loop - SCC, SCS, SEQ, SMI, SNE, SPL, SVC, SVS - conditional skip
-
These are branches over the next instruction. No operand is required, because the target is the address of the instruction following the next instruction. Example:
lda #40 add:sta ptr scc:inc ptr+1
In the above example, the 16-bit variable
ptris incremented by 40. - JCC, JCS, JEQ, JMI, JNE, JPL, JVC, JVS - conditional jump
-
These are long branches. While standard branches (such as
BNE) have a range of -128..+127 bytes, these jumps have a range of 64 KB. For example:jne dest
is equivalent to:
seq:jmp dest
- INW - increment word
-
Increments a 16-bit word in memory. Example:
inw dest
is equivalent to:
inc dest sne:inc dest+1
- MVA, MVX, MVY - move a byte using the accumulator, X or Y
-
Each of these pseudo commands requires two operands and substitutes two commands:
mva source dest = lda source : sta dest mvx source dest = ldx source : stx dest mvy source dest = ldy source : sty dest
- MWA, MWX, MWY - move a word using the accumulator, X or Y
-
These pseudo commands require two operands and are combinations of two
MVA/MVX/MWY: one to move the low byte, and the other to move the high byte. You can’t use indirect or pseudo addressing modes withMWA/MWX/MWY. The destination must be an absolute or zero-page address, optionally indexed. When the source is also an absolute or zero-page address, anmwa source destexpands to:mva source dest mva source+1 dest+1
When the source is an immediate value, an
mwa #immed destexpands to:mva <immed dest mva >immed dest+1
When
<immedequals>immedandimmedis not forward-referenced, xasm skips the secondLDA, generating the following code:mva <immed dest sta dest+1
If possible,
MWXandMWYuse increment/decrement instructions. For example,mwx #1 destexpands to:ldx #1 stx dest dex stx dest+1
Addressing modes are entered according to the standard 6502 convention.
An exception is the accumulator mode marked with the @ character
for compatibility with Quick Assembler.
Also for Quick Assembler compatibility, there are two extra immediate
addressing modes: < and >, which use the low/high byte of a 16-bit word constant.
Unlike Quick Assembler, you can alternatively use
the more common syntax: #< and #>.
Note the difference:
lda >$ff+5 ; loads 1 (>$104)
lda #>$ff+5 ; loads 5 (0+5)You can explicitly specify absolute (a:) and zero-page (z:) addressing modes.
Examples:
nop
asl @
lda >$1234 assembles to lda #$12
lda $100,x
lda 0 zero-page (8-bit address)
lda a:0 absolute (16-bit address)
jmp ($0a)
lda ($80),yThere are pseudo addressing modes, which are similar to pseudo commands.
You may use them just like standard addressing modes in all 6502 commands
and pseudo commands, except for MWA, MWX and MWY:
cmd a,x+ = cmd a,x : inx
cmd a,x- = cmd a,x : dex
cmd a,y+ = cmd a,y : iny
cmd a,y- = cmd a,y : dey
cmd (z),y+ = cmd (z),y : iny
cmd (z),y- = cmd (z),y : dey
cmd (z,0) = ldx #0 : cmd (z,x)
cmd (z),0 = ldy #0 : cmd (z),y
cmd (z),0+ = ldy #0 : cmd (z),y : iny
cmd (z),0- = ldy #0 : cmd (z),y : dey-
built with LDC - supports macOS 15.4+
-
def(label)tests whether a label is defined (requested by Sebastian Igielski) -
.DSdirective for defining uninitialized storage (requested by Sebastian Igielski) -
ALNdirective for aligning the origin counter to the specified boundary (requested by Sebastian Igielski) -
DTA e(int24bit),f(int32bit)modes (compatible with Fast Assembler and MADS) -
avoid misinterpreting certain non-ASCII characters as an Atari $9b EOL, by detecting EOL in the first line of each source file (reported by Sebastian Igielski)
-
fixed corrupted executable headers with
ORGround-trip through an area with no emitted output (reported by Sebastian Igielski) -
fixed corrupted executable headers with
OPT O-(reported by Tomasz Biela) -
OPTalternatively accepts a quoted list ofname=valuesettings, for exampleopt 'output=rom,hardware=atari5200', including the newoutputformat selection andhardware=none(by Adrian Matoga) -
xasm can now be used as a library in any D program (by Adrian Matoga)
-
fixed writing the /t label table: it no longer closes standard output when the listing goes to
-, and it no longer overwrites the listing file when both /l and /t name the same file (by Adrian Matoga) -
fixed parsing of opcode values: an operand such as the one in
{jsr $1234}is no longer misread, and a line ending with{lda <no longer crashes (by Adrian Matoga)
-
the source can optionally be read from standard input, and the object can be written to standard output instead of files (by Adrian Matoga)
-
any number of instructions in one line (by Adrian Matoga)
-
local labels (contributed by Adrian Matoga)
-
fixed emitted bytes not shown in the listing after
OPT L+(by Adrian Matoga) -
emit shorter code for moving a negative immediate value with
MWX/MWY -
Windows binary signed
-
macOS binary signed, notarized, installed in
/usr/local/bin
-
remove partial object file on error (requested by Bill Kendrick)
-
report label name in the unused label warning (requested by Peter Dell)
-
support
a:/f:modifiers forRUN/INI -
improved performance with network drives (reported by Marek Pavlik)
-
OS X, Ubuntu and Fedora distributions
-
INScan be repeated (suggested by Marek Pavlik), and you can take its "opcode" -
OPT U-disables /u unused label warnings (suggested by Marek Pavlik) -
if the file to be included cannot be opened, report an error in the
ICLline (suggested by Peter Dell) -
removed duplicate filenames for /M
-
implemented /p outside Windows
-
source code updated from D1 to D2
-
project moved to GitHub
-
fixed the "Branch out of range" error message, which was overstated by 256 bytes for backward branches
-
command-line options are now case-insensitive
-
on Windows, error messages are printed in red, warnings in yellow
-
fixed a bug in
OPT H-mode -
made xasm compilable with the latest D compiler v1.010 (there were incompatible changes in the D language and library)
-
rewritten from the x86 assembly language to the D programming language - the Linux version is now available and DOS is no longer supported
-
no limits on line length, number of
ICLs,ORGs,IFTsand labels -
Unix-style command-line options are supported
-
the /e option is removed
-
the label table is now sorted alphabetically
-
no more "Arithmetic overflow" and "Division by zero" errors for correct use of forward-referenced labels (bug found by Marcin Lewandowski)
-
an error was reported in the following correct code:
ift 0 foo equ 1 ift foo eif eif
(bug found by Adrian Matoga)
-
errors for nonexistent
INC @andDEC @ -
negative numbers fixed in the listing
-
long file names are supported under Windows
-
label values are now 32-bit, not just 17-bit
-
command-line options /n and /s are no longer supported
-
fatal I/O errors no longer print the annoying "Abort, Retry, Ignore" message
-
version 2.5.1 broke Unix EOLs - fixed
-
version 2.5.1 omitted all blank/comment/label lines, unless /c was used
-
fixed assembling sources with Atari EOLs
-
blank/comment/label lines in false conditionals are now correctly omitted in the listing
-
fixed another bug similar to the previous one, for example:
ift 0 :label nop eif
it reported a "Label not defined before" error for the repeat count
-
OPT F+causesORGto fill the space between the old and the new location with$FFs -
OPT G+enables Atari 5200 mode for hardware register abbreviations
-
fixed a bug related to label definitions in conditionally skipped code, e.g.
ift 0 label eif
it reported a "No ORG specified" error for the label definition
-
fixed incorrect unary operator precedence
-
fixed wrong label value after a skip pseudo command
-
the assembler is an .EXE (.COM caused problems with DJGPP make due to a bug in the DJGPP runtime)
-
the assembler executable is no longer compressed
-
improved command-line parsing: options may be used before the source file name, a tab is a valid separator, and a slash may be used as a directory separator
-
error and warning messages are written to stderr, not stdout
-
added
==(equals) operator, equivalent to=, but familiar to C/C++/Java programmers -
added
/poption: print full paths in the listing and in error messages -
MWXandMWYmay useINX/DEXandINY/DEY, respectively, for generating shorter code
-
fixed double skip (e.g.
SCC:SNE) -
fixed real numbers with two-digit exponent
-
trailing spaces are trimmed from listing lines
-
label definitions allowed in blank, comment and repeated lines
-
fixed invalid opcodes of absolute
CPXandCPY -
fixed: addressing mode not checked for branch commands
-
fixed
ICLin last line -
fixed
OPT H-H+ -
fixed first
ORG * -
no need to set the origin counter until it’s needed
-
allow Unix, Macintosh and Atari EOLs
-
value of true changed to 1
-
command-line option to set environment variables on error
-
command-line option to assemble only if the source is newer than the object file
-
fixed: object filename was truncated
-
fixed forward references in
EQUandDTA -
fixed hex numbers
-
.OBXis now the default extension for the object file -
options (command-line switches and
OPT) -
listing
-
label table
-
conditional assembly
-
user errors (
ERT) -
warnings
-
6 new pseudo commands (memory-to-memory move)
-
8 pseudo addressing modes
-
indirect conditional jumps
-
Atari floating-point numbers
-
object file headers optimization
-
improved expressions - 19 operators and brackets, 32-bit arithmetic
-
improved signed numbers
-
improved
INS: inserting a specified part of a file
Piotr Fusik <fox@scene.pl>
Website: https://github.com/pfusik/xasm