1 <!doctype linuxdoc system>
4 <title>ca65 Users Guide
5 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">
9 ca65 is a macro assembler for the 6502, 65C02 and 65816 CPUs. It is used as a
10 companion assembler for the cc65 crosscompiler, but it may also be used as a
14 <!-- Table of contents -->
17 <!-- Begin the document -->
21 ca65 is a replacement for the ra65 assembler that was part of the cc65 C
22 compiler, originally developed by John R. Dunning. I had some problems with
23 ra65 and the copyright does not permit some things which I wanted to be
24 possible, so I decided to write a completely new assembler/linker/archiver
25 suite for the cc65 compiler. ca65 is part of this suite.
27 Some parts of the assembler (code generation and some routines for symbol
28 table handling) are taken from an older crossassembler named a816 written
29 by me a long time ago.
31 Here's a list of the design criteria, that I considered important for the
36 <item> The assembler must support macros. Macros are not essential, but they
37 make some things easier, especially when you use the assembler in the
38 backend of a compiler.
39 <item> The assembler must support the newer 65C02 and 65816 CPUs. I have been
40 thinking about a 65816 backend for the C compiler, and even my old
41 a816 assembler had support for these CPUs, so this wasn't really a
43 <item> The assembler must produce relocatable code. This necessary for the
44 compiler support, and it is more convenient.
45 <item> Conditional assembly must be supported. This is a must for bigger
46 projects written in assembler (like Elite128).
47 <item> The assembler must support segments, and it must support more than
48 three segments (this is the count, most other assemblers support).
49 Having more than one code segments helps developing code for systems
50 with a divided ROM area (like the C64).
51 <item> The linker must be able to resolve arbitrary expressions. It should
52 be able to get things like
59 <item> True lexical nesting for symbols. This is very convenient for larger
61 <item> "Cheap" local symbols without lexical nesting for those quick, late
63 <item> I liked the idea of "options" as Anre Fachats .o65 format has it, so I
64 introduced the concept into the object file format use by the new cc65
66 <item> The assembler will be a one pass assembler. There was no real need for
67 this decision, but I've written several multipass assemblers, and it
68 started to get boring. A one pass assembler needs much more elaborated
69 data structures, and because of that it's much more fun:-)
70 <item> Non-GPLed code that may be used in any project without restrictions or
71 fear of "GPL infecting" other code.
78 The assembler accepts the following options:
81 ---------------------------------------------------------------------------
82 Usage: ca65 [options] file
84 -g Add debug info to object file
86 -i Ignore case of symbols
87 -l Create a listing if assembly was ok
88 -o name Name the output file
90 -t sys Set the target system
92 -D name[=value] Define a symbol
93 -I dir Set an include directory search path
94 -U Mark unresolved symbols as import
95 -V Print the assembler version
96 -W n Set warning level n
99 --auto-import Mark unresolved symbols as import
100 --cpu type Set cpu type
101 --debug-info Add debug info to object file
102 --feature name Set an emulation feature
103 --help Help (this text)
104 --ignore-case Ignore case of symbols
105 --include-dir dir Set an include directory search path
106 --listing Create a listing if assembly was ok
107 --pagelength n Set the page length for the listing
108 --smart Enable smart mode
109 --target sys Set the target system
110 --verbose Increase verbosity
111 --version Print the assembler version
112 ---------------------------------------------------------------------------
115 Here is a description of all the command line options:
119 <tag><tt>--cpu type</tt></tag>
121 Set the default for the CPU type. The option takes a parameter, which
124 6502, 65C02, 65816 and sunplus
126 The latter (sunplus) is not available in the freeware version, because the
127 instruction set of the sunplus CPU is "confidential".
130 <tag><tt>--feature name</tt></tag>
132 Enable an emulation feature. This is identical as using <tt/.FEATURE/
133 in the source with two exceptions: Feature names must be lower case, and
134 each feature must be specified by using an extra <tt/--feature/ option,
135 comma separated lists are not allowed.
137 See the discussion of the <tt/.FEATURE/ command for a list of emulation
141 <tag><tt>-g, --debug-info</tt></tag>
143 When this option (or the equivalent control command <tt/.DEBUGINFO/) is
144 used, the assembler will add a section to the object file that contains
145 all symbols (including local ones) together with the symbol values and
146 source file positions. The linker will put these additional symbols into
147 the VICE label file, so even local symbols can be seen in the VICE
151 <tag><tt>-h, --help</tt></tag>
153 Print the short option summary shown above.
156 <tag><tt>-i, --ignore-case</tt></tag>
158 This option makes the assembler case insensitive on identifiers and
159 labels. This option will override the default, but may itself be
160 overriden by the <tt/.CASE/ control command (see section 6).
163 <tag><tt>-l, --listing</tt></tag>
165 Generate an assembler listing. The listing file will always have the
166 name of the main input file with the extension replaced by ".lst". This
167 may change in future versions.
170 <tag><tt>-o name</tt></tag>
172 The default output name is the name of the input file with the extension
173 replaced by ".o". If you don't like that, you may give another name with
174 the -o option. The output file will be placed in the same directory as
175 the source file, or, if -o is given, the full path in this name is used.
178 <tag><tt>--pagelength n</tt></tag>
180 sets the length of a listing page in lines. See the <tt/.PAGELENGTH/
181 directive for more information.
184 <tag><tt>-s, --smart-mode</tt></tag>
186 In smart mode (enabled by -s or the <tt/.SMART/ pseudo instruction) the
187 assembler will track usage of the REP and SEP instructions in 65816 mode
188 and update the operand sizes accordingly. If the operand of such an
189 instruction cannot be evaluated by the assembler (for example, because
190 the operand is an imported symbol), a warning is issued.
192 Beware: Since the assembler cannot trace the execution flow this may
193 lead to false results in some cases. If in doubt, use the .ixx and .axx
194 instructions to tell the assembler about the current settings. Smart
195 mode is off by default.
198 <tag><tt>-t sys, --target sys</tt></tag>
200 Set the target system. This will enable translation of character strings
201 and character constants into the character set of the target platform.
202 The default for the target system is "none", which means that no translation
203 will take place. The assembler supports the same target systems as the
204 compiler, see there for a list.
207 <tag><tt>-v, --verbose</tt></tag>
209 Increase the assembler verbosity. Usually only needed for debugging
210 purposes. You may use this option more than one time for even more
214 <tag><tt>-D</tt></tag>
216 This option allows you to define symbols on the command line. Without a
217 value, the symbol is defined with the value zero. When giving a value,
218 you may use the '$' prefix for hexadecimal symbols. Please note
219 that for some operating systems, '$' has a special meaning, so
220 you may have to quote the expression.
223 <tag><tt>-I dir, --include-dir dir</tt></tag>
225 Name a directory which is searched for include files. The option may be
226 used more than once to specify more than one directory to search. The
227 current directory is always searched first before considering any
228 additional directores.
231 <tag><tt>-U, --auto-import</tt></tag>
233 Mark symbols that are not defined in the sources as imported symbols.
234 This should be used with care since it delays error messages about typos
235 and such until the linker is run. The compiler uses the equivalent of
236 this switch (<tt/.AUTOIMPORT/, see control command section below) to enable
237 auto imported symbols for the runtime library. However, the compiler is
238 supposed to generate code that runs through the assembler without
239 problems, something which is not always true for assembler programmers.
242 <tag><tt>-V, --version</tt></tag>
244 Print the version number of the assembler. If you send any suggestions
245 or bugfixes, please include the version number.
248 <tag><tt>-Wn</tt></tag>
250 Set the warning level for the assembler. Using -W2 the assembler will
251 even warn about such things like unused imported symbols. The default
252 warning level is 1, and it would probably be silly to set it to
259 <sect>Input format<p>
261 The assembler accepts the standard 6502/65816 assembler syntax. One line
262 may contain a label (which is identified by a colon), and, in addition to
263 the label, an assembler mnemonic, a macro, or a control command (see
264 section 6 for supported control commands). Alternatively, the line may
265 contain a symbol definition using the '=' token. Everything after a
266 semicolon is handled as a comment (that is, it is ignored).
268 Here are some examples for valid input lines:
271 Label: ; A label and a comment
272 lda #$20 ; A 6502 instruction plus comment
273 L1: ldx #$20 ; Same with label
274 L2: .byte "Hello world" ; Label plus control command
275 mymac $20 ; Macro expansion
276 MySym = 3*L1 ; Symbol definition
277 MaSym = Label ; Another symbol
280 The assembler accepts all valid 6502 mnemonics when in 6502 mode (the
281 default). The assembler accepts all valid 65SC02 mnemonics when in 65SC02
282 mode (after a <tt/.PC02/ command is found). The assembler accepts all valid
283 65816 mnemonics with a few exceptions after a .P816 command is found.
284 These exceptions are listed below.
286 In 65816 mode several aliases are accepted in addition to the official
290 BGE is an alias for BCS
291 BLT is an alias for BCC
292 CPA is an alias for CMP
293 DEA is an alias for DEC A
294 INA is an alias for INC A
295 SWA is an alias for XBA
296 TAD is an alias for TCD
297 TAS is an alias for TCS
298 TDA is an alias for TDC
299 TSA is an alias for TSC
302 Evaluation of banked expressions in 65816 mode differs slightly from the
305 Instead of accepting a 24 bit address (something that is difficult for
306 the assembler to determine and would have required one more special
307 .import command), the bank and the absolute address in that bank are
311 jsl 3.$1234 ; Call subroutine at $1234 in bank 3
314 For literal values, the assembler accepts the widely used number formats:
315 A preceeding '$' denotes a hex value, a preceeding '%' denotes a
316 binary value, and a bare number is interpeted as a decimal. There are
317 currently no octal values and no floats.
324 All expressions are evaluated with (at least) 32 bit precision. An
325 expression may contain constant values and any combination of internal and
326 external symbols. Expressions that cannot be evaluated at assembly time
327 are stored inside the object file for evaluation by the linker.
328 Expressions referencing imported symbols must always be evaluated by the
331 Sometimes, the assembler must know about the size of the value that is the
332 result of an expression. This is usually the case, if a decision has to be
333 made, to generate a zero page or an absolute memory references. In this
334 case, the assembler has to make some assumptions about the result of an
338 <item> If the result of an expression is constant, the actual value is
339 checked to see if it's a byte sized expression or not.
340 <item> If the expression is explicitly casted to a byte sized expression by
341 one of the '>'/'<' operators, it is a byte expression.
342 <item> If this is not the case, and the expression contains a symbol,
343 explicitly declared as zero page symbol (by one of the .importzp or
344 .exportzp instructions), then the whole expression is assumed to be
346 <item> If the expression contains symbols that are not defined, and these
347 symbols are local symbols, the enclosing scopes are searched for a
348 symbol with the same name. If one exists and this symbol is defined,
349 it's attributes are used to determine the result size.
350 <item> In all other cases the expression is assumed to be word sized.
353 Note: If the assembler is not able to evaluate the expression at assembly
354 time, the linker will evaluate it and check for range errors as soon as
358 <bf>Boolean expressions:</bf>
360 In the context of a boolean expression, any non zero value is evaluated as
361 true, any other value to false. The result of a boolean expression is 1 if
362 it's true, and zero if it's false. There are boolean operators with extrem
363 low precedence with version 2.x (where x > 0). The <tt/.AND/ and <tt/.OR/
364 operators are shortcut operators. That is, if the result of the expression is
365 already known, after evaluating the left hand side, the right hand side is
369 Available operators sorted by precedence:
372 Op Description Precedence
373 -------------------------------------------------------------------
374 .CONCAT Builtin function 0
375 .LEFT Builtin function 0
376 .MID Builtin function 0
377 .RIGHT Builtin function 0
378 .STRING Builtin function 0
380 * Builtin pseudo variable (r/o) 1
381 .BLANK Builtin function 1
382 .CONST Builtin function 1
383 .CPU Builtin pseudo variable (r/o) 1
384 .DEFINED Builtin function 1
385 .MATCH Builtin function 1
386 .TCOUNT Builtin function 1
387 .XMATCH Builtin function 1
388 .PARAMCOUNT Builtin pseudo variable (r/o) 1
389 .REFERENCED Builtin function 1
390 :: Global namespace override 1
393 ~ Unary bitwise not 1
394 .BITNOT Unary bitwise not 1
395 < Low byte operator 1
396 > High byte operator 1
400 .MOD Modulo operation 2
402 .BITAND Bitwise and 2
404 .BITXOR Bitwise xor 2
405 << Shift left operator 2
406 .SHL Shift left operator 2
407 >> Shift right operator 2
408 .SHR Shift right operator 2
415 = Compare operation (equal) 4
416 <> Compare operation (not equal) 4
417 < Compare operation (less) 4
418 > Compare operation (greater) 4
419 <= Compare operation (less or equal) 4
420 >= Compare operation (greater or equal) 4
422 && Boolean and 5
434 To force a specific order of evaluation, braces may be used as usual.
436 Some of the pseudo variables mentioned above need some more explanation:
439 * This symbol is replaced by the value of the program
440 counter at start of the current instruction. Note, that
441 '*' yields a rvalue, that means, you cannot assign to it.
442 Use <tt/.ORG/ to set the program counter in sections with
449 <sect>Symbols and labels
452 The assembler allows you to use symbols instead of naked values to make
453 the source more readable. There are a lot of different ways to define and
454 use symbols and labels, giving a lot of flexibility.
459 <tag/Numeric constants/
461 Numeric constants are defined using the equal sign. After doing
467 may use the symbol "two" in every place where a number is expected,
468 and it is evaluated to the value 2 in this context. An example would be
475 <tag/Standard labels/
477 A label is defined by writing the name of the label at the start of
478 the line (before any instruction mnemonic, macro or pseudo
479 directive), followed by a colon. This will declare a symbol with the
480 given name and the value of the current program counter.
483 <tag/Local labels and symbols/
485 Using the <tt/.PROC/ directive, it is possible to create regions of code
486 where the names of labels and symbols are local to this region. They
487 are not know outside and cannot be accessed from there. Such regions
488 may be nested like PROCEDUREs in Pascal.
490 See the description of the <tt/.PROC/ directive for more information.
492 <tag/Cheap local labels/
494 Cheap local labels are defined like standard labels, but the name of
495 the label must begin with a special symbol (usually '@', but this can
496 be changed by the <tt/.LOCALCHAR/ directive).
498 Cheap local labels are visible only between two no cheap labels. As
499 soon as a standard symbol is encountered (this may also be a local
500 symbol if inside a region defined with the .PROC directive), the
501 cheap local symbol goes out of scope.
503 You may use cheap local labels as an easy way to reuse common label
504 names like "Loop". Here is an example:
507 Clear: lda #$00 ; Global label
509 @Loop: sta Mem,y ; Local label
513 Sub: ... ; New global label
514 bne @Loop ; ERROR: Unknown identifier!
519 If you really want to write messy code, there are also unnamed
520 labels. These labels do not have a name (you guessed that already,
521 didn't you?). A colon is used to mark the absence of the name.
523 Unnamed labels may be accessed by using the colon plus several minus
524 or plus characters as a label designator. Using the '-' characters
525 will create a back reference (use the n'th label backwards), using
526 '+' will create a forward reference (use the n'th label in forward
527 direction). An example will help to understand this:
549 As you can see from the example, unnamed labels will make even short
550 sections of code hard to understand, because you have to count labels
551 to find branch targets (this is the reason why I for my part do
552 prefer the "cheap" local labels). Nevertheless, unnamed labels are
553 convenient in some situations, so it's your decision.
555 <tag/Using macros to define labels and constants/
557 While there are drawbacks with this approach, it may be handy in some
558 situations. Using <tt/.DEFINE/, it is possible to define symbols or
559 constants that may be used elsewhere. Since the macro facility works
560 on a very low level, there is no scoping. On the other side, you may
561 also define string constants this way (this is not possible with the
568 .DEFINE version "SOS V2.3"
570 four = two * two ; Ok
573 .PROC ; Start local scope
574 two = 3 ; Will give "2 = 3" - invalid!
581 If <tt/.DEBUGINFO/ is enabled (or -g is given on the command line), global,
582 local and cheap local labels are written to the object file and will be
583 available in the symbol file via the linker. Unnamed labels are not
584 written to the object file, because they don't have a name which would
585 allow to access them.
589 <sect>Control commands
592 Here's a list of all control commands and a description, what they do:
596 <tag><tt><idx>.A16</idx></tt></tag>
598 Valid only in 65816 mode. Switch the accumulator to 16 bit.
600 Note: This command will not emit any code, it will tell the assembler to
601 create 16 bit operands for immediate accumulator adressing mode.
603 See also: <tt/.SMART/
606 <tag><tt><idx>.A8</idx></tt></tag>
608 Valid only in 65816 mode. Switch the accumulator to 8 bit.
610 Note: This command will not emit any code, it will tell the assembler to
611 create 8 bit operands for immediate accu adressing mode.
613 See also: <tt/.SMART/
616 <tag><tt><idx>.ADDR</idx></tt></tag>
618 Define word sized data. In 6502 mode, this is an alias for <tt/.WORD/ and
619 may be used for better readability if the data words are address values.
620 In 65816 mode, the address is forced to be 16 bit wide to fit into the
621 current segment. See also <tt/.FARADDR/. The command must be followed by a
622 sequence of (not necessarily constant) expressions.
627 .addr $0D00, $AF13, _Clear
631 <tag><tt><idx>.ALIGN</idx></tt></tag>
633 Align data to a given boundary. The command expects a constant integer
634 argument that must be a power of two, plus an optional second argument
635 in byte range. If there is a second argument, it is used as fill value,
636 otherwise the value defined in the linker configuration file is used
637 (the default for this value is zero).
639 Since alignment depends on the base address of the module, you must
640 give the same (or a greater) alignment for the segment when linking.
641 The linker will give you a warning, if you don't do that.
650 <tag><tt><idx>.ASCIIZ</idx></tt></tag>
652 Define a string with a trailing zero.
657 Msg: .asciiz "Hello world"
660 This will put the string "Hello world" followed by a binary zero into
661 the current segment. There may be more strings separated by commas, but
662 the binary zero is only appended once (after the last one).
665 <tag><tt><idx>.AUTOIMPORT</idx></tt></tag>
667 Is followd by a plus or a minus character. When switched on (using a
668 +), undefined symbols are automatically marked as import instead of
669 giving errors. When switched off (which is the default so this does not
670 make much sense), this does not happen and an error message is
671 displayed. The state of the autoimport flag is evaluated when the
672 complete source was translated, before outputing actual code, so it is
673 <em/not/ possible to switch this feature on or off for separate sections
674 of code. The last setting is used for all symbols.
676 You should probably not use this switch because it delays error
677 messages about undefined symbols until the link stage. The cc65
678 compiler (which is supposed to produce correct assembler code in all
679 circumstances, something which is not true for most assembler
680 programmers) will insert this command to avoid importing each and every
681 routine from the runtime library.
686 .autoimport + ; Switch on auto import
690 <tag><tt><idx>.BLANK</idx></tt></tag>
692 Builtin function. The function evaluates its argument in braces and
693 yields "false" if the argument is non blank (there is an argument), and
694 "true" if there is no argument. As an example, the <tt/.IFBLANK/ statement
702 <tag><tt><idx>.BSS</idx></tt></tag>
704 Switch to the BSS segment. The name of the BSS segment is always "BSS",
705 so this is a shortcut for
711 See also the <tt/.SEGMENT/ command.
714 <tag><tt><idx>.BYTE</idx></tt></tag>
716 Define byte sized data. Must be followed by a sequence of (byte ranged)
717 expressions or strings.
722 .byte "Hello world", $0D, $00
726 <tag><tt><idx>.CASE</idx></tt></tag>
728 Switch on or off case sensitivity on identifiers. The default is off
729 (that is, identifiers are case sensitive), but may be changed by the
730 -i switch on the command line.
731 The command must be followed by a '+' or '-' character to switch the
732 option on or off respectively.
737 .case - ; Identifiers are not case sensitive
741 <tag><tt><idx>.CODE</idx></tt></tag>
743 Switch to the CODE segment. The name of the CODE segment is always
744 "CODE", so this is a shortcut for
750 See also the <tt/.SEGMENT/ command.
753 <tag><tt><idx>.CONCAT</idx></tt></tag>
755 Builtin function. The function allows to concatenate a list of string
756 constants separated by commas. The result is a string constant that
757 is the concatentation of all arguments. This function is most useful
758 in macros and when used together with the <tt/.STRING/ builtin function.
759 The function may be used in any case where a string constant is
765 .include .concat ("myheader", ".", "inc)
768 This is the same as the command
771 .include "myheader.inc"
775 <tag><tt><idx>.CONST</idx></tt></tag>
777 Builtin function. The function evaluates its argument in braces and
778 yields "true" if the argument is a constant expression (that is, an
779 expression that yields a constant value at assembly time) and "false"
780 otherwise. As an example, the .IFCONST statement may be replaced by
787 <tag><tt><idx>.CPU</idx></tt></tag>
789 Reading this pseudo variable will give a constant integer value that
790 tells which instruction set is currently enabled. Possible values are:
799 It may be used to replace the .IFPxx pseudo instructions or to construct
800 even more complex expressions.
805 .if (.cpu = 0) .or (.cpu = 1)
817 <tag><tt><idx>.DATA</idx></tt></tag>
819 Switch to the DATA segment. The name of the DATA segment is always
820 "DATA", so this is a shortcut for
826 See also the <tt/.SEGMENT/ command.
829 <tag><tt><idx>.DBYT</idx></tt></tag>
831 Define word sized data with the hi and lo bytes swapped (use <tt/.WORD/ to
832 create word sized data in native 65XX format). Must be followed by a
833 sequence of (word ranged) expressions.
841 This will emit the bytes
847 into the current segment in that order.
850 <tag><tt><idx>.DEBUGINFO</idx></tt></tag>
852 Switch on or off debug info generation. The default is off (that is,
853 the object file will not contain debug infos), but may be changed by the
854 -g switch on the command line.
855 The command must be followed by a '+' or '-' character to switch the
856 option on or off respectively.
861 .debuginfo + ; Generate debug info
865 <tag><tt><idx>.DEFINE</idx></tt></tag>
867 Start a define style macro definition. The command is followed by an
868 identifier (the macro name) and optionally by a list of formal arguments
870 See separate section about macros.
873 <tag><tt><idx>.DEF,</idx> <idx>.DEFINED</idx></tt></tag>
875 Builtin function. The function expects an identifier as argument in
876 braces. The argument is evaluated, and the function yields "true" if the
877 identifier is a symbol that is already defined somewhere in the source
878 file up to the current position. Otherwise the function yields false. As
879 an example, the <tt/.IFDEF/ statement may be replaced by
886 <tag><tt><idx>.DWORD</idx></tt></tag>
888 Define dword sized data (4 bytes) Must be followed by a sequence of
894 .dword $12344512, $12FA489
898 <tag><tt><idx>.ELSE</idx></tt></tag>
900 Conditional assembly: Reverse the current condition.
903 <tag><tt><idx>.ELSEIF</idx></tt></tag>
905 Conditional assembly: Reverse current condition and test a new one.
908 <tag><tt><idx>.END</idx></tt></tag>
910 Forced end of assembly. Assembly stops at this point, even if the command
911 is read from an include file.
914 <tag><tt><idx>.ENDIF</idx></tt></tag>
916 Conditional assembly: Close a <tt/.IF.../ or <tt/.ELSE/ branch.
919 <tag><tt><idx>.ENDMAC,</idx> <idx>.ENDMACRO</idx></tt></tag>
921 End of macro definition (see separate section).
924 <tag><tt><idx>.ENDPROC</idx></tt></tag>
926 End of local lexical level (see <tt/.PROC/).
929 <tag><tt><idx>.ENDREP,</idx> <idx>.ENDREPEAT</idx></tt></tag>
931 End a <tt/.REPEAT/ block. See the <tt/.REPEAT/ command.
934 <tag><tt><idx>.ERROR</idx></tt></tag>
936 Force an assembly error. The assembler will output an error message
937 preceeded by "User error" and will <em/not/ produce an object file.
939 This command may be used to check for initial conditions that must be
940 set before assembling a source file.
950 .error "Must define foo or bar!"
954 See also the <tt/.WARNING/ and <tt/.OUT/ directives.
957 <tag><tt><idx>.EXITMAC,</idx> <idx>.EXITMACRO</idx></tt></tag>
959 Abort a macro expansion immidiately. This command is often useful in
960 recursive macros. See separate chapter about macros.
963 <tag><tt><idx>.EXPORT</idx></tt></tag>
965 Make symbols accessible from other modules. Must be followed by a comma
966 separated list of symbols to export.
975 <tag><tt><idx>.EXPORTZP</idx></tt></tag>
977 Make symbols accessible from other modules. Must be followed by a comma
978 separated list of symbols to export. The exported symbols are explicitly
979 marked as zero page symols.
988 <tag><tt><idx>.FARADDR</idx></tt></tag>
990 Define far (24 bit) address data. The command must be followed by a
991 sequence of (not necessarily constant) expressions.
996 .faraddr DrawCircle, DrawRectangle, DrawHexagon
1000 <tag><tt><idx>.FEATURE</idx></tt></tag>
1002 This directive may be used to enable one or more compatibility features
1003 of the assembler. While the use of <tt/.FEATURE/ should be avoided when
1004 possible, it may be useful when porting sources written for other
1005 assemblers. There is no way to switch a feature off, once you have
1006 enabled it, so using
1012 will enable the feature until end of assembly is reached.
1014 The following features are available:
1018 <tag><tt><idx>dollar_is_pc</idx></tt></tag>
1020 The dollar sign may be used as an alias for the star (`*'), which
1021 gives the value of the current PC in expressions.
1022 Note: Assignment to the pseudo variable is not allowed.
1024 <tag><tt><idx>labels_without_colons</idx></tt></tag>
1026 Allow labels without a trailing colon. These labels are only accepted,
1027 if they start at the beginning of a line (no leading white space).
1029 <tag><tt><idx>loose_string_term</idx></tt></tag>
1031 Accept single quotes as well as double quotes as terminators for string
1034 <tag><tt><idx>at_in_identifiers</idx></tt></tag>
1036 Accept the at character (`@') as a valid character in identifiers. The
1037 at character is not allowed to start an identifier, even with this
1040 <tag><tt><idx>dollar_in_identifiers</idx></tt></tag>
1042 Accept the dollar sign (`$') as a valid character in identifiers. The
1043 at character is not allowed to start an identifier, even with this
1046 <tag><tt><idx>pc_assignment</idx></tt></tag>
1048 Allow assignments to the PC symbol (`*' or `$' if dollar_is_pc is
1049 enabled). Such an assignment is handled identical to the <tt/.ORG/
1050 command (which is usually not needed, so just removing the lines with
1051 the assignments may also be an option when porting code written for older
1057 <tag><tt><idx>.FILEOPT,</idx> <idx>.FOPT</idx></tt></tag>
1059 Insert an option string into the object file. There are two forms of
1060 this command, one specifies the option by a keyword, the second
1061 specifies it as a number. Since usage of the second one needs knowledge
1062 of the internal encoding, its use is not recommended and I will only
1063 describe the first form here.
1065 The command is followed by one of the keywords
1073 a comma and a string. The option is written into the object file
1074 together with the string value. This is currently unidirectional and
1075 there is no way to actually use these options once they are in the
1081 .fileopt comment, "Code stolen from my brother"
1082 .fileopt compiler, "BASIC 2.0"
1083 .fopt author, "J. R. User"
1087 <tag><tt><idx>.GLOBAL</idx></tt></tag>
1089 Declare symbols as global. Must be followed by a comma separated list
1090 of symbols to declare. Symbols from the list, that are defined somewhere
1091 in the source, are exported, all others are imported. An additional
1092 explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same symbol is
1102 <tag><tt><idx>.GLOBALZP</idx></tt></tag>
1104 Declare symbols as global. Must be followed by a comma separated list
1105 of symbols to declare. Symbols from the list, that are defined
1106 somewhere in the source, are exported, all others are imported. An
1107 additional explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same
1108 symbol is explicitly allowed. The symbols in the list are explicitly
1109 marked as zero page symols.
1118 <tag><tt><idx>.I16</idx></tt></tag>
1120 Valid only in 65816 mode. Switch the index registers to 16 bit.
1122 Note: This command will not emit any code, it will tell the assembler to
1123 create 16 bit operands for immediate operands.
1125 See also the <tt/.SMART/ command.
1128 <tag><tt><idx>.I8</idx></tt></tag>
1130 Valid only in 65816 mode. Switch the index registers to 8 bit.
1132 Note: This command will not emit any code, it will tell the assembler to
1133 create 8 bit operands for immediate operands.
1135 See also the <tt/.SMART/ command.
1138 <tag><tt><idx>.IF</idx></tt></tag>
1140 Conditional assembly: Evalute an expression and switch assembler output
1141 on or off depending on the expression. The expression must be a constant
1142 expression, that is, all operands must be defined.
1144 A expression value of zero evaluates to FALSE, any other value evaluates
1148 <tag><tt><idx>.IFBLANK</idx></tt></tag>
1150 Conditional assembly: Check if there are any remaining tokens in this
1151 line, and evaluate to FALSE if this is the case, and to TRUE otherwise.
1152 If the condition is not true, further lines are not assembled until
1153 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1155 This command is often used to check if a macro parameter was given.
1156 Since an empty macro parameter will evaluate to nothing, the condition
1157 will evaluate to FALSE if an empty parameter was given.
1171 See also: <tt/.BLANK/
1174 <tag><tt><idx>.IFCONST</idx></tt></tag>
1176 Conditional assembly: Evaluate an expression and switch assembler output
1177 on or off depending on the constness of the expression.
1179 A const expression evaluates to to TRUE, a non const expression (one
1180 containing an imported or currently undefined symbol) evaluates to
1183 See also: <tt/.CONST/
1186 <tag><tt><idx>.IFDEF</idx></tt></tag>
1188 Conditional assembly: Check if a symbol is defined. Must be followed by
1189 a symbol name. The condition is true if the the given symbol is already
1190 defined, and false otherwise.
1192 See also: <tt/.DEFINED/
1195 <tag><tt><idx>.IFNBLANK</idx></tt></tag>
1197 Conditional assembly: Check if there are any remaining tokens in this
1198 line, and evaluate to TRUE if this is the case, and to FALSE otherwise.
1199 If the condition is not true, further lines are not assembled until
1200 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1202 This command is often used to check if a macro parameter was given.
1203 Since an empty macro parameter will evaluate to nothing, the condition
1204 will evaluate to FALSE if an empty parameter was given.
1217 See also: <tt/.BLANK/
1220 <tag><tt><idx>.IFNDEF</idx></tt></tag>
1222 Conditional assembly: Check if a symbol is defined. Must be followed by
1223 a symbol name. The condition is true if the the given symbol is not
1224 defined, and false otherwise.
1226 See also: <tt/.DEFINED/
1229 <tag><tt><idx>.IFNREF</idx></tt></tag>
1231 Conditional assembly: Check if a symbol is referenced. Must be followed
1232 by a symbol name. The condition is true if if the the given symbol was
1233 not referenced before, and false otherwise.
1235 See also: <tt/.REFERENCED/
1238 <tag><tt><idx>.IFP02</idx></tt></tag>
1240 Conditional assembly: Check if the assembler is currently in 6502 mode
1241 (see <tt/.P02/ command).
1244 <tag><tt><idx>.IFP816</idx></tt></tag>
1246 Conditional assembly: Check if the assembler is currently in 65816 mode
1247 (see <tt/.P816/ command).
1250 <tag><tt><idx>.IFPC02</idx></tt></tag>
1252 Conditional assembly: Check if the assembler is currently in 65C02 mode
1253 (see <tt/.PC02/ command).
1256 <tag><tt><idx>.IFREF</idx></tt></tag>
1258 Conditional assembly: Check if a symbol is referenced. Must be followed
1259 by a symbol name. The condition is true if if the the given symbol was
1260 referenced before, and false otherwise.
1262 This command may be used to build subroutine libraries in include files
1263 (you may use separate object modules for this purpose too).
1268 .ifref ToHex ; If someone used this subroutine
1269 ToHex: tay ; Define subroutine
1275 See also: <tt/.REFERENCED/
1278 <tag><tt><idx>.IMPORT</idx></tt></tag>
1280 Import a symbol from another module. The command is followed by a comma
1281 separated list of symbols to import.
1290 <tag><tt><idx>.IMPORTZP</idx></tt></tag>
1292 Import a symbol from another module. The command is followed by a comma
1293 separated list of symbols to import. The symbols are explicitly imported
1294 as zero page symbols (that is, symbols with values in byte range).
1303 <tag><tt><idx>.INCBIN</idx></tt></tag>
1305 Include a file as binary data. The command expects a string argument
1306 that is the name of a file to include literally in the current segment.
1311 .incbin "sprites.dat"
1315 <tag><tt><idx>.INCLUDE</idx></tt></tag>
1317 Include another file. Include files may be nested up to a depth of 16.
1326 <tag><tt><idx>.LEFT</idx></tt></tag>
1328 Builtin function. Extracts the left part of a given token list.
1333 .LEFT (<int expr>, <token list>)
1336 The first integer expression gives the number of tokens to extract from
1337 the token list. The second argument is the token list itself.
1341 To check in a macro if the given argument has a '#' as first token
1342 (immidiate addressing mode), use something like this:
1347 .if (.match (.left (1, arg), #))
1349 ; ldax called with immidiate operand
1357 See also the <tt/.MID/ and <tt/.RIGHT/ builtin functions.
1360 <tag><tt><idx>.LINECONT</idx></tt></tag>
1362 Switch on or off line continuations using the backslash character
1363 before a newline. The option is off by default.
1364 Note: Line continuations do not work in a comment. A backslash at the
1365 end of a comment is treated as part of the comment and does not trigger
1367 The command must be followed by a '+' or '-' character to switch the
1368 option on or off respectively.
1373 .linecont + ; Allow line continuations
1376 #$20 ; This is legal now
1380 <tag><tt><idx>.LIST</idx></tt></tag>
1382 Enable output to the listing. The command must be followed by a boolean
1383 switch ("on", "off", "+" or "-") and will enable or disable listing
1385 The option has no effect if the listing is not enabled by the command line
1386 switch -l. If -l is used, an internal counter is set to 1. Lines are output
1387 to the listing file, if the counter is greater than zero, and suppressed if
1388 the counter is zero. Each use of <tt/.LIST/ will increment or decrement the
1394 .list on ; Enable listing output
1398 <tag><tt><idx>.LISTBYTES</idx></tt></tag>
1400 Set, how many bytes are shown in the listing for one source line. The
1401 default is 12, so the listing will show only the first 12 bytes for any
1402 source line that generates more than 12 bytes of code or data.
1403 The directive needs an argument, which is either "unlimited", or an
1404 integer constant in the range 4..255.
1409 .listbytes unlimited ; List all bytes
1410 .listbytes 12 ; List the first 12 bytes
1411 .incbin "data.bin" ; Include large binary file
1415 <tag><tt><idx>.LOCAL</idx></tt></tag>
1417 This command may only be used inside a macro definition. It declares a
1418 list of identifiers as local to the macro expansion.
1420 A problem when using macros are labels: Since they don't change their
1421 name, you get a "duplicate symbol" error if the macro is expanded the
1422 second time. Labels declared with <tt/.LOCAL/ have their name mapped to
1423 an internal unique name (<tt/___ABCD__/) with each macro invocation.
1425 Some other assemblers start a new lexical block inside a macro
1426 expansion. This has some drawbacks however, since that will not allow
1427 <em/any/ symbol to be visible outside a macro, a feature that is sometimes
1428 useful. The <tt/.LOCAL/ command is in my eyes a better way to address
1431 You get an error when using <tt/.LOCAL/ outside a macro.
1434 <tag><tt><idx>.LOCALCHAR</idx></tt></tag>
1436 Defines the character that start "cheap" local labels. You may use one
1437 of '@' and '?' as start character. The default is '@'.
1439 Cheap local labels are labels that are visible only between two non
1440 cheap labels. This way you can reuse identifiers like "<tt/loop/" without
1441 using explicit lexical nesting.
1448 Clear: lda #$00 ; Global label
1449 ?Loop: sta Mem,y ; Local label
1453 Sub: ... ; New global label
1454 bne ?Loop ; ERROR: Unknown identifier!
1458 <tag><tt><idx>.MACPACK</idx></tt></tag>
1460 Insert a predefined macro package. The command is followed by an
1461 identifier specifying the macro package to insert. Available macro
1464 generic Defines generic macros like add and sub.
1465 longbranch Defines conditional long jump macros.
1467 Including a macro package twice, or including a macro package that
1468 redefines already existing macros will lead to an error.
1473 .macpack longbranch ; Include macro package
1475 cmp #$20 ; Set condition codes
1476 jne Label ; Jump long on condition
1479 See separate section about macros packages.
1482 <tag><tt><idx>.MAC,</idx> <idx>.MACRO</idx></tt></tag>
1484 Start a classic macro definition. The command is followed by an identifier
1485 (the macro name) and optionally by a comma separated list of identifiers
1486 that are macro parameters.
1487 See separate section about macros.
1490 <tag><tt><idx>.MATCH</idx></tt></tag>
1492 Builtin function. Matches two token lists against each other. This is
1493 most useful within macros, since macros are not stored as strings, but
1499 .MATCH(<token list #1>, <token list #2>)
1502 Both token list may contain arbitrary tokens with the exception of the
1503 terminator token (comma resp. right parenthesis) and
1510 Often a macro parameter is used for any of the token lists.
1512 Please note that the function does only compare tokens, not token
1513 attributes. So any number is equal to any other number, regardless of
1514 the actual value. The same is true for strings. If you need to compare
1515 tokens <em/and/ token attributes, use the <tt/.XMATCH/ function.
1519 Assume the macro <tt/ASR/, that will shift right the accumulator by one, while
1520 honoring the sign bit. The builtin processor instructions will allow an
1521 optional "A" for accu addressing for instructions like <tt/ROL/ and <tt/ROR/.
1522 We will use the <tt/.MATCH/ function to check for this and print and error
1528 .if (.not .blank(arg)) .and (.not .match (arg, a))
1529 .error "Syntax error"
1532 cmp #$80 ; Bit 7 into carry
1533 lsr a ; Shit carry into bit 7
1538 The macro will only accept no arguments, or one argument that must be the
1539 reserved keyword "A".
1542 <tag><tt><idx>.MID</idx></tt></tag>
1544 Builtin function. Takes a starting index, a count and a token list as
1545 arguments. Will return part of the token list.
1550 .MID (<int expr>, <int expr>, <token list>)
1553 The first integer expression gives the starting token in the list (the
1554 first token has index 0). The second integer expression gives the number
1555 of tokens to extract from the token list. The third argument is the
1560 To check in a macro if the given argument has a '<tt/#/' as first token
1561 (immidiate addressing mode), use something like this:
1566 .if (.match (.mid (0, 1, arg), #))
1568 ; ldax called with immidiate operand
1576 See also the <tt/.LEFT/ and <tt/.RIGHT/ builtin functions.
1579 <tag><tt><idx>.ORG</idx></tt></tag>
1581 Start a section of absolute code. The command is followed by a constant
1582 expression that gives the new PC counter location for which the code is
1583 assembled. Use <tt/.RELOC/ to switch back to relocatable code.
1585 You may not switch segments while inside a section of absolute code.
1590 .org $7FF ; Emit code starting at $7FF
1594 <tag><tt><idx>.OUT</idx></tt></tag>
1596 Output a string to the console without producing an error. This command
1597 is similiar to <tt/.ERROR/, however, it does not force an assembler error
1598 that prevents the creation of an object file.
1603 .out "This code was written by the codebuster(tm)"
1606 See also the <tt/.WARNING/ and <tt/.ERROR/ directives.
1609 <tag><tt><idx>.P02</idx></tt></tag>
1611 Enable the 6502 instruction set, disable 65C02 and 65816 instructions.
1612 This is the default if not overridden by the <tt/--cpu/ command line
1616 <tag><tt><idx>.P816</idx></tt></tag>
1618 Enable the 65816 instruction set. This is a superset of the 65C02 and
1619 6502 instruction sets.
1622 <tag><tt><idx>.PAGELEN,</idx> <idx>.PAGELENGTH</idx></tt></tag>
1624 Set the page length for the listing. Must be followed by an integer
1625 constant. The value may be "unlimited", or in the range 32 to 127. The
1626 statement has no effect if no listing is generated. The default value
1627 is -1 but may be overridden by the <tt/--pagelength/ command line option.
1628 Beware: Since the listing is generated after assembly is complete, you
1629 cannot use multiple line lengths with one source. Instead, the value
1630 set with the last <tt/.PAGELENGTH/ is used.
1635 .pagelength 66 ; Use 66 lines per listing page
1637 .pagelength unlimited ; Unlimited page length
1641 <tag><tt><idx>.PARAMCOUNT</idx></tt></tag>
1643 This builtin pseudo variable is only available in macros. It is replaced
1644 by the actual number of parameters that were given in the macro
1650 .macro foo arg1, arg2, arg3
1651 .if .paramcount <> 3
1652 .error "Too few parameters for macro foo"
1659 <tag><tt><idx>.PC02</idx></tt></tag>
1661 Enable the 65C02 instructions set. This instruction set includes all
1665 <tag><tt><idx>.PROC</idx></tt></tag>
1667 Start a nested lexical level. All new symbols from now on are in the
1668 local lexical level and are not accessible from outside. Symbols defined
1669 outside this local level may be accessed as long as their names are not
1670 used for new symbols inside the level. Symbols names in other lexical
1671 levels do not clash, so you may use the same names for identifiers. The
1672 lexical level ends when the <tt/.ENDPROC/ command is read. Lexical levels
1673 may be nested up to a depth of 16.
1675 The command may be followed by an identifier, in this case the
1676 identifier is declared in the outer level as a label having the value of
1677 the program counter at the start of the lexical level.
1679 Note: Macro names are always in the global level and in a separate name
1680 space. There is no special reason for this, it's just that I've never
1681 had any need for local macro definitions.
1686 .proc Clear ; Define Clear subroutine, start new level
1688 L1: sta Mem,y ; L1 is local and does not cause a
1689 ; duplicate symbol error if used in other
1692 bne L1 ; Reference local symbol
1694 .endproc ; Leave lexical level
1698 <tag><tt><idx>.REF,</idx> <idx>.REFERENCED</idx></tt></tag>
1700 Builtin function. The function expects an identifier as argument in
1701 braces. The argument is evaluated, and the function yields "true" if the
1702 identifier is a symbol that has already been referenced somewhere in the
1703 source file up to the current position. Otherwise the function yields
1704 false. As an example, the <tt/.IFREF/ statement may be replaced by
1711 <tag><tt><idx>.REPEAT</idx></tt></tag>
1713 Repeat all commands between <tt/.REPEAT/ and <tt/.ENDREPEAT/ a constant
1714 number of times. The command is followed by a constant expression that tells
1715 how many times the commands in the body should get repeated. Optionally, a
1716 comma and an identifier may be specified. If this identifier is found in
1717 the body of the repeat statement, it is replaced by the current repeat
1718 count (starting with zero for the first time the body is repeated).
1720 <tt/.REPEAT/ statements may be nested. If you use the same repeat count
1721 identifier for a nested <tt/.REPEAT/ statement, the one from the inner
1722 level will be used, not the one from the outer level.
1726 The following macro will emit a string that is "encrypted" in that all
1727 characters of the string are XORed by the value $55.
1731 .repeat strlen(Arg), I
1732 .byte strat(Arg, I) .xor $55
1738 <tag><tt><idx>.RELOC</idx></tt></tag>
1740 Switch back to relocatable mode. See the <tt/.ORG/ command.
1743 <tag><tt><idx>.RES</idx></tt></tag>
1745 Reserve storage. The command is followed by one or two constant
1746 expressions. The first one is mandatory and defines, how many bytes of
1747 storage should be defined. The second, optional expression must by a
1748 constant byte value that will be used as value of the data. If there
1749 is no fill value given, the linker will use the value defined in the
1750 linker configuration file (default: zero).
1755 ; Reserve 12 bytes of memory with value $AA
1760 <tag><tt><idx>.RIGHT</idx></tt></tag>
1762 Builtin function. Extracts the right part of a given token list.
1767 .RIGHT (<int expr>, <token list>)
1770 The first integer expression gives the number of tokens to extract from
1771 the token list. The second argument is the token list itself.
1773 See also the <tt/.LEFT/ and <tt/.MID/ builtin functions.
1776 <tag><tt><idx>.RODATA</idx></tt></tag>
1778 Switch to the RODATA segment. The name of the RODATA segment is always
1779 "RODATA", so this is a shortcut for
1785 The RODATA segment is a segment that is used by the compiler for
1786 readonly data like string constants. See also the <tt/.SEGMENT/ command.
1789 <tag><tt><idx>.SEGMENT</idx></tt></tag>
1791 Switch to another segment. Code and data is always emitted into a
1792 segment, that is, a named section of data. The default segment is
1793 "CODE". There may be up to 254 different segments per object file
1794 (and up to 65534 per executable). There are shortcut commands for
1795 the most common segments ("CODE", "DATA" and "BSS").
1797 The command is followed by a string containing the segment name (there
1798 are some constraints for the name - as a rule of thumb use only those
1799 segment names that would also be valid identifiers). There may also be
1800 an optional attribute separated by a comma. Valid attributes are
1801 "<tt/zeropage/" and "<tt/absolute/".
1803 When specifying a segment for the first time, "absolute" is the
1804 default. For all other uses, the attribute specified the first time
1807 "absolute" means that this is a segment with absolute addressing. That
1808 is, the segment will reside somewhere in core memory outside the zero
1809 page. "zeropage" means the opposite: The segment will be placed in the
1810 zero page and direct (short) addressing is possible for data in this
1813 Beware: Only labels in a segment with the zeropage attribute are marked
1814 as reachable by short addressing. The `*' (PC counter) operator will
1815 work as in other segments and will create absolute variable values.
1820 .segment "ROM2" ; Switch to ROM2 segment
1821 .segment "ZP2", zeropage ; New direct segment
1822 .segment "ZP2" ; Ok, will use last attribute
1823 .segment "ZP2", absolute ; Error, redecl mismatch
1827 <tag><tt><idx>.SMART</idx></tt></tag>
1829 Switch on or off smart mode. The command must be followed by a '+' or
1830 '-' character to switch the option on or off respectively. The default
1831 is off (that is, the assembler doesn't try to be smart), but this
1832 default may be changed by the -s switch on the command line.
1834 In smart mode the assembler will track usage of the <tt/REP/ and <tt/SEP/
1835 instructions in 65816 mode and update the operand sizes accordingly. If
1836 the operand of such an instruction cannot be evaluated by the assembler
1837 (for example, because the operand is an imported symbol), a warning is
1838 issued. Beware: Since the assembler cannot trace the execution flow this
1839 may lead to false results in some cases. If in doubt, use the <tt/.Inn/ and
1840 <tt/.Ann/ instructions to tell the assembler about the current settings.
1846 .smart - ; Stop being smart
1850 <tag><tt><idx>.STRAT</idx></tt></tag>
1852 Builtin function. The function accepts a string and an index as
1853 arguments and returns the value of the character at the given position
1854 as an integer value. The index is zero based.
1860 ; Check if the argument string starts with '#'
1861 .if (.strat (Arg, 0) = '#')
1868 <tag><tt><idx>.STRING</idx></tt></tag>
1870 Builtin function. The function accepts an argument in braces and
1871 converts this argument into a string constant. The argument may be an
1872 identifier, or a constant numeric value.
1873 Since you can use a string in the first place, the use of the function
1874 may not be obvious. However, it is useful in macros, or more complex
1880 ; Emulate other assemblers:
1882 .segment .string(name)
1887 <tag><tt><idx>.STRLEN</idx></tt></tag>
1889 Builtin function. The function accepts a string argument in braces and
1890 eveluates to the length of the string.
1894 The following macro encodes a string as a pascal style string with
1895 a leading length byte.
1899 .byte .strlen(Arg), Arg
1904 <tag><tt><idx>.TCOUNT</idx></tt></tag>
1906 Builtin function. The function accepts a token list in braces. The
1907 function result is the number of tokens given as argument.
1911 The <tt/ldax/ macro accepts the '#' token to denote immidiate addressing (as
1912 with the normal 6502 instructions). To translate it into two separate 8 bit
1913 load instructions, the '#' token has to get stripped from the argument:
1917 .if (.match (.mid (0, 1, arg), #))
1918 ; ldax called with immidiate operand
1919 lda #<(.right (.tcount (arg)-1, arg))
1920 ldx #>(.right (.tcount (arg)-1, arg))
1928 <tag><tt><idx>.WARNING</idx></tt></tag>
1930 Force an assembly warning. The assembler will output a warning message
1931 preceeded by "User warning". This warning will always be output, even
1932 if other warnings are disabled with the <tt/-W0/ command line option.
1934 This command may be used to output possible problems when assembling
1943 .warning "Forward jump in jne, cannot optimize!"
1953 See also the <tt/.ERROR/ and <tt/.OUT/ directives.
1956 <tag><tt><idx>.WORD</idx></tt></tag>
1958 Define word sized data. Must be followed by a sequence of (word ranged,
1959 but not necessarily constant) expressions.
1964 .word $0D00, $AF13, _Clear
1968 <tag><tt><idx>.ZEROPAGE</idx></tt></tag>
1970 Switch to the ZEROPAGE segment and mark it as direct (zeropage) segment.
1971 The name of the ZEROPAGE segment is always "ZEROPAGE", so this is a
1975 .segment "ZEROPAGE", zeropage
1978 Because of the "zeropage" attribute, labels declared in this segment are
1979 addressed using direct addressing mode if possible. You <em/must/ instruct
1980 the linker to place this segment somewhere in the address range 0..$FF
1981 otherwise you will get errors.
1990 Macros may be thought of as "parametrized super instructions". Macros are
1991 sequences of tokens that have a name. If that name is used in the source
1992 file, the macro is "expanded", that is, it is replaced by the tokens that
1993 were specified when the macro was defined.
1995 In it's simplest form, a macro does not have parameters. Here's an
1999 .macro asr ; Arithmetic shift right
2000 cmp #$80 ; Put bit 7 into carry
2001 ror ; Rotate right with carry
2005 The macro above consists of two real instructions, that are inserted into
2006 the code, whenever the macro is expanded. Macro expansion is simply done
2007 by using the name, like this:
2015 When using macro parameters, macros can be even more useful:
2029 When calling the macro, you may give a parameter, and each occurence of
2030 the name "addr" in the macro definition will be replaced by the given
2049 A macro may have more than one parameter, in this case, the parameters
2050 are separated by commas. You are free to give less parameters than the
2051 macro actually takes in the definition. You may also leave intermediate
2052 parameters empty. Empty parameters are replaced by empty space (that is,
2053 they are removed when the macro is exanded). If you have a look at our
2054 macro definition above, you will see, that replacing the "addr" parameter
2055 by nothing will lead to wrong code in most lines. To help you, writing
2056 macros with a variable parameter list, there are some control commands:
2058 <tt/.IFBLANK/ tests the rest of the line and returns true, if there are any
2059 tokens on the remainder of the line. Since empty parameters are replaced by
2060 nothing, this may be used to test if a given parameter is empty.
2061 <tt/.IFNBLANK/ tests the opposite.
2063 Look at this example:
2066 .macro ldaxy a, x, y
2079 This macro may be called as follows:
2082 ldaxy 1, 2, 3 ; Load all three registers
2084 ldaxy 1, , 3 ; Load only a and y
2086 ldaxy , , 3 ; Load y only
2089 There's another helper command for determining, which macro parameters are
2090 valid: <tt/.PARAMCOUNT/. This command is replaced by the parameter count
2091 given, <em/including/ intermediate empty macro parameters:
2094 ldaxy 1 ; .PARAMCOUNT = 1
2095 ldaxy 1,,3 ; .PARAMCOUNT = 3
2096 ldaxy 1,2 ; .PARAMCOUNT = 2
2097 ldaxy 1, ; .PARAMCOUNT = 2
2098 ldaxy 1,2,3 ; .PARAMCOUNT = 3
2101 Macros may be used recursively:
2104 .macro push r1, r2, r3
2113 There's also a special macro to help writing recursive macros:
2114 <tt/.EXITMACRO/. This command will stop macro expansion immidiately:
2117 .macro push r1, r2, r3, r4, r5, r6, r7
2119 ; First parameter is empty
2125 push r2, r3, r4, r5, r6, r7
2129 When expanding this macro, the expansion will push all given parameters
2130 until an empty one is encountered. The macro may be called like this:
2133 push $20, $21, $32 ; Push 3 ZP locations
2134 push $21 ; Push one ZP location
2137 Now, with recursive macros, <tt/.IFBLANK/ and <tt/.PARAMCOUNT/, what else do
2138 you need? Have a look at the inc16 macro above. Here is it again:
2152 If you have a closer look at the code, you will notice, that it could be
2153 written more efficiently, like this:
2167 But imagine what happens, if you use this macro twice? Since the label
2168 "Skip" has the same name both times, you get a "duplicate symbol" error.
2169 Without a way to circumvent this problem, macros are not as useful, as
2170 they could be. One solution is, to start a new lexical block inside the
2187 Now the label is local to the block and not visible outside. However,
2188 sometimes you want a label inside the macro to be visible outside. To make
2189 that possible, there's a new command that's only usable inside a macro
2190 definition: <tt/.LOCAL/. <tt/.LOCAL/ declares one or more symbols as local to
2191 the macro expansion. The names of local variables are replaced by a unique
2192 name in each separate macro expansion. So we could also solve the problem
2193 above by using <tt/.LOCAL/:
2197 .local Skip ; Make Skip a local symbol
2204 Skip: ; Not visible outside
2208 Starting with version 2.5 of the assembler, there is a second macro type
2209 available: C style macros using the <tt/.DEFINE/ directive. These macros are
2210 similar to the classic macro type speified above, but behaviour is sometimes
2215 <item> Macros defined with <tt/.DEFINE/ may not span more than a line. You
2216 may use line continuation (see <tt/.LINECONT/) to spread the
2217 definition over more than one line for increased readability, but the
2218 macro itself does not contain an end-of-line token.
2220 <item> Macros defined with <tt/.DEFINE/ share the name space with classic
2221 macros, but they are detected and replaced at the scanner level. While
2222 classic macros may be used in every place, where a mnemonic or other
2223 directive is allowed, <tt/.DEFINE/ style macros are allowed anywhere
2224 in a line. So they are more versatile in some situations.
2226 <item> <tt/.DEFINE/ style macros may take parameters. While classic macros
2227 may have empty parameters, this is not true for <tt/.DEFINE/ style
2228 macros. For this macro type, the number of actual parameters must
2229 match exactly the number of formal parameters.
2231 To make this possible, formal parameters are enclosed in braces when
2232 defining the macro. If there are no parameters, the empty braces may
2235 <item> Since <tt/.DEFINE/ style macros may not contain end-of-line tokens,
2236 there are things that cannot be done. They may not contain several
2237 processor instructions for example. So, while some things may be done
2238 with both macro types, each type has special usages. The types
2239 complement each other.
2243 Let's look at a few examples to make the advantages and disadvantages
2246 To emulate assemblers that use "<tt/EQU/" instead of "<tt/=/" you may use the
2247 following <tt/.DEFINE/:
2252 foo EQU $1234 ; This is accepted now
2255 You may use the directive to define string constants used elsewhere:
2258 ; Define the version number
2259 .define VERSION "12.3a"
2265 Macros with parameters may also be useful:
2268 .define DEBUG(message) .out message
2270 DEBUG "Assembling include file #3"
2273 Note that, while formal parameters have to be placed in braces, this is
2274 not true for the actual parameters. Beware: Since the assembler cannot
2275 detect the end of one parameter, only the first token is used. If you
2276 don't like that, use classic macros instead:
2284 (This is an example where a problem can be solved with both macro types).
2288 <sect>Macro packages
2291 Using the <tt/.MACPACK/ directive, predefined macro packages may be included
2292 with just one command. Available macro packages are:
2296 <tag><tt><idx>generic</idx></tt></tag>
2298 This macro package defines macros that are useful in almost any program.
2299 Currently, two macros are defined:
2314 <tag><tt><idx>longbranch</idx></tt></tag>
2316 This macro package defines long conditional jumps. They are named like the
2317 short counterpart but with the 'b' replaced by a 'j'. Here is a sample
2318 definition for the "<tt/jeq/" macro, the other macros are built using the
2323 .if .def(Target) .and ((*+2)-(Target) <= 127)
2332 All macros expand to a short branch, if the label is already defined (back
2333 jump) and is reachable with a short jump. Otherwise the macro expands to a
2334 conditional branch with the branch condition inverted, followed by an
2335 absolute jump to the actual branch target.
2337 The package defines the following macros:
2340 jeq, jne, jmi, jpl, jcs, jcc, jvs, jvc
2346 <sect>Bugs/Feedback<p>
2348 If you have problems using the assembler, if you find any bugs, or if
2349 you're doing something interesting with the assembler, I would be glad to
2350 hear from you. Feel free to contact me by email
2351 (<htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">).
2357 ca65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
2358 Bassewitz. For usage of the binaries and/or sources the following
2359 conditions do apply:
2361 This software is provided 'as-is', without any expressed or implied
2362 warranty. In no event will the authors be held liable for any damages
2363 arising from the use of this software.
2365 Permission is granted to anyone to use this software for any purpose,
2366 including commercial applications, and to alter it and redistribute it
2367 freely, subject to the following restrictions:
2370 <item> The origin of this software must not be misrepresented; you must not
2371 claim that you wrote the original software. If you use this software
2372 in a product, an acknowledgment in the product documentation would be
2373 appreciated but is not required.
2374 <item> Altered source versions must be plainly marked as such, and must not
2375 be misrepresented as being the original software.
2376 <item> This notice may not be removed or altered from any source