1 <!doctype linuxdoc system>
4 <title>ca65 Users Guide
5 <author>Ullrich von Bassewitz, <tt/uz@musoftware.de/
7 <abstract>ca65 is a macro assembler for the 6502, 65C02 and 65816 CPUs.
10 <!-- Table of contents -->
13 <!-- Begin the document -->
17 ca65 is a replacement for the ra65 assembler that was part of the cc65 C
18 compiler developed by John R. Dunning. I had some problems with ra65 and
19 the copyright does not permit some things which I wanted to be possible,
20 so I decided to write a completely new assembler/linker/archiver suite for
21 the cc65 compiler. ca65 is part of this suite.
23 Some parts of the assembler (code generation and some routines for symbol
24 table handling) are taken from an older crossassembler named a816 written
25 by me a long time ago.
27 Here's a list of the design criteria, that were important for the
32 <item> The assembler must support macros. Macros are not essential, but they
33 make some things easier, especially when you use the assembler in the
34 backend of a compiler.
35 <item> The assembler must support the newer 65C02 and 65816 CPUs. I have been
36 thinking about a 65816 backend for the C compiler, and even my old
37 a816 assembler had support for these CPUs, so this wasn't really a
39 <item> The assembler must produce relocatable code. This necessary for the
40 compiler support, and it is more convenient.
41 <item> Conditional assembly must be supported. This is a must for bigger
42 projects written in assembler (like Elite128).
43 <item> The assembler must support segments, and it must support more than
44 three segments (this is the count, most other assemblers support).
45 Having more than one code segments helps developing code for systems
46 with a divided ROM area (like the C64).
47 <item> The linker must be able to resolve arbitrary expressions. Years ago I
48 spent half a day to convince Borlands Turbo Assembler to let me use
49 the size of a structure I had created. So I decided that this is a
50 must. The linker should be able to get things like
57 <item> True lexical nesting for symbols. This is very convenient for larger
59 <item> "Cheap" local symbols without lexical nesting for those quick, late
61 <item> I liked the idea of "options" as Anre Fachats .o65 format has it, so I
62 introduced the concept into the object file format use by the new cc65
64 <item> The assembler will be a one pass assembler. There was no real need for
65 this decision, but I've written several multipass assemblers, and it
66 started to get boring. A one pass assembler needs much more elaborated
67 data structures, and because of that it's much more fun:-)
68 <item> Non-GPLed code that may be used in any project without restrictions or
69 fear of "GPL infecting" other code.
75 The assembler accepts the following options:
78 ---------------------------------------------------------------------------
79 Usage: ca65 [options] file
81 -g Add debug info to object file
83 -i Ignore case of symbols
84 -l Create a listing if assembly was ok
85 -o name Name the output file
88 -D name[=value] Define a symbol
89 -I dir Set an include directory search path
90 -U Mark unresolved symbols as import
91 -V Print the assembler version
92 -W n Set warning level n
95 --auto-import Mark unresolved symbols as import
96 --cpu type Set cpu type
97 --debug-info Add debug info to object file
98 --help Help (this text)
99 --ignore-case Ignore case of symbols
100 --include-dir dir Set an include directory search path
101 --listing Create a listing if assembly was ok
102 --pagelength n Set the page length for the listing
103 --smart Enable smart mode
104 --verbose Increase verbosity
105 --version Print the assembler version
106 ---------------------------------------------------------------------------
109 Here is a description of all the command line options:
113 <tag><tt>--cpu type</tt></tag>
115 Set the default for the CPU type. The option takes a parameter, which
118 6502, 65C02, 65816 and sunplus
120 (the latter is not available in the freeware version).
123 <tag><tt>-g, --debug-info</tt></tag>
125 When this option (or the equivalent control command <tt/.DEBUGINFO/) is
126 used, the assembler will add a section to the object file that contains
127 all symbols (including local ones) together with the symbol values and
128 source file positions. The linker will put these additional symbols into
129 the VICE label file, so even local symbols can be seen in the VICE
133 <tag><tt>-h, --help</tt></tag>
135 Print the short option summary shown above.
138 <tag><tt>-i, --ignore-case</tt></tag>
140 This option makes the assembler case insensitive on identifiers and
141 labels. This option will override the default, but may itself be
142 overriden by the <tt/.CASE/ control command (see section 6).
145 <tag><tt>-l, --listing</tt></tag>
147 Generate an assembler listing. The listing file will always have the
148 name of the main input file with the extension replaced by ".lst". This
149 may change in future versions.
152 <tag><tt>-o name</tt></tag>
154 The default output name is the name of the input file with the extension
155 replaced by ".o". If you don't like that, you may give another name with
156 the -o option. The output file will be placed in the same directory as
157 the source file, or, if -o is given, the full path in this name is used.
160 <tag><tt>--pagelength n</tt></tag>
162 sets the length of a listing page in lines. See the <tt/.PAGELENGTH/
163 directive for more information.
166 <tag><tt>-s, --smart-mode</tt></tag>
168 In smart mode (enabled by -s or the <tt/.SMART/ pseudo instruction) the
169 assembler will track usage of the REP and SEP instructions in 65816 mode
170 and update the operand sizes accordingly. If the operand of such an
171 instruction cannot be evaluated by the assembler (for example, because
172 the operand is an imported symbol), a warning is issued.
174 Beware: Since the assembler cannot trace the execution flow this may
175 lead to false results in some cases. If in doubt, use the .ixx and .axx
176 instructions to tell the assembler about the current settings. Smart
177 mode is off by default.
180 <tag><tt>-v, --verbose</tt></tag>
182 Increase the assembler verbosity. Usually only needed for debugging
183 purposes. You may use this option more than one time for even more
187 <tag><tt>-D</tt></tag>
189 This option allows you to define symbols on the command line. Without a
190 value, the symbol is defined with the value zero. When giving a value,
191 you may use the '$' prefix for hexadecimal symbols. Please note
192 that for some operating systems, '$' has a special meaning, so
193 you may have to quote the expression.
196 <tag><tt>-I dir, --include-dir dir</tt></tag>
198 Name a directory which is searched for include files. The option may be
199 used more than once to specify more than one directory to search. The
200 current directory is always searched first before considering any
201 additional directores.
204 <tag><tt>-U, --auto-import</tt></tag>
206 Mark symbols that are not defined in the sources as imported symbols.
207 This should be used with care since it delays error messages about typos
208 and such until the linker is run. The compiler uses the equivalent of
209 this switch (<tt/.AUTOIMPORT/, see control command section below) to enable
210 auto imported symbols for the runtime library. However, the compiler is
211 supposed to generate code that runs through the assembler without
212 problems, something which is not always true for assembler programmers.
215 <tag><tt>-V, --version</tt></tag>
217 Print the version number of the assembler. If you send any suggestions
218 or bugfixes, please include the version number.
221 <tag><tt>-Wn</tt></tag>
223 Set the warning level for the assembler. Using -W2 the assembler will
224 even warn about such things like unused imported symbols. The default
225 warning level is 1, and it would probably be silly to set it to
232 <sect>Input format<p>
234 The assembler accepts the standard 6502/65816 assembler syntax. One line
235 may contain a label (which is identified by a colon), and, in addition to
236 the label, an assembler mnemonic, a macro, or a control command (see
237 section 6 for supported control commands). Alternatively, the line may
238 contain a symbol definition using the '=' token. Everything after a
239 semicolon is handled as a comment (that is, it is ignored).
241 Here are some examples for valid input lines:
244 Label: ; A label and a comment
245 lda #$20 ; A 6502 instruction plus comment
246 L1: ldx #$20 ; Same with label
247 L2: .byte "Hello world" ; Label plus control command
248 mymac $20 ; Macro expansion
249 MySym = 3*L1 ; Symbol definition
250 MaSym = Label ; Another symbol
253 The assembler accepts all valid 6502 mnemonics when in 6502 mode (the
254 default). The assembler accepts all valid 65SC02 mnemonics when in 65SC02
255 mode (after a <tt/.PC02/ command is found). The assembler accepts all valid
256 65816 mnemonics with a few exceptions after a .P816 command is found.
257 These exceptions are listed below.
259 In 65816 mode several aliases are accepted in addition to the official
263 BGE is an alias for BCS
264 BLT is an alias for BCC
265 CPA is an alias for CMP
266 DEA is an alias for DEC A
267 INA is an alias for INC A
268 SWA is an alias for XBA
269 TAD is an alias for TCD
270 TAS is an alias for TCS
271 TDA is an alias for TDC
272 TSA is an alias for TSC
275 Evaluation of banked expressions in 65816 mode differs slightly from the
278 Instead of accepting a 24 bit address (something that is difficult for
279 the assembler to determine and would have required one more special
280 .import command), the bank and the absolute address in that bank are
284 jsl 3.$1234 ; Call subroutine at $1234 in bank 3
287 For literal values, the assembler accepts the widely used number formats:
288 A preceeding '$' denotes a hex value, a preceeding '%' denotes a
289 binary value, and a bare number is interpeted as a decimal. There are
290 currently no octal values and no floats.
297 All expressions are evaluated with (at least) 32 bit precision. An
298 expression may contain constant values and any combination of internal and
299 external symbols. Expressions that cannot be evaluated at assembly time
300 are stored inside the object file for evaluation by the linker.
301 Expressions referencing imported symbols must always be evaluated by the
304 Sometimes, the assembler must know about the size of the value that is the
305 result of an expression. This is usually the case, if a decision has to be
306 made, to generate a zero page or an absolute memory references. In this
307 case, the assembler has to make some assumptions about the result of an
311 <item> If the result of an expression is constant, the actual value is
312 checked to see if it's a byte sized expression or not.
313 <item> If the expression is explicitly casted to a byte sized expression by
314 one of the '>'/'<' operators, it is a byte expression.
315 <item> If this is not the case, and the expression contains a symbol,
316 explicitly declared as zero page symbol (by one of the .importzp or
317 .exportzp instructions), then the whole expression is assumed to be
319 <item> If the expression contains symbols that are not defined, and these
320 symbols are local symbols, the enclosing scopes are searched for a
321 symbol with the same name. If one exists and this symbol is defined,
322 it's attributes are used to determine the result size.
323 <item> In all other cases the expression is assumed to be word sized.
326 Note: If the assembler is not able to evaluate the expression at assembly
327 time, the linker will evaluate it and check for range errors as soon as
331 <bf>Boolean expressions:</bf>
333 In the context of a boolean expression, any non zero value is evaluated as
334 true, any other value to false. The result of a boolean expression is 1 if
335 it's true, and zero if it's false. There are boolean operators with extrem
336 low precedence with version 2.x (where x > 0). The <tt/.AND/ and <tt/.OR/
337 operators are shortcut operators. That is, if the result of the expression is
338 already known, after evaluating the left hand side, the right hand side is
342 Available operators sorted by precedence:
345 Op Description Precedence
346 -------------------------------------------------------------------
347 .CONCAT Builtin function 0
348 .LEFT Builtin function 0
349 .MID Builtin function 0
350 .RIGHT Builtin function 0
351 .STRING Builtin function 0
353 * Builtin pseudo variable (r/o) 1
354 .BLANK Builtin function 1
355 .CONST Builtin function 1
356 .CPU Builtin pseudo variable (r/o) 1
357 .DEFINED Builtin function 1
358 .MATCH Builtin function 1
359 .TCOUNT Builtin function 1
360 .XMATCH Builtin function 1
361 .PARAMCOUNT Builtin pseudo variable (r/o) 1
362 .REFERENCED Builtin function 1
363 :: Global namespace override 1
366 ~ Unary bitwise not 1
367 .BITNOT Unary bitwise not 1
368 < Low byte operator 1
369 > High byte operator 1
373 .MOD Modulo operation 2
375 .BITAND Bitwise and 2
377 .BITXOR Bitwise xor 2
378 << Shift left operator 2
379 .SHL Shift left operator 2
380 >> Shift right operator 2
381 .SHR Shift right operator 2
388 = Compare operation (equal) 4
389 <> Compare operation (not equal) 4
390 < Compare operation (less) 4
391 > Compare operation (greater) 4
392 <= Compare operation (less or equal) 4
393 >= Compare operation (greater or equal) 4
395 && Boolean and 5
407 To force a specific order of evaluation, braces may be used as usual.
409 Some of the pseudo variables mentioned above need some more explanation:
411 * This symbol is replaced by the value of the program
412 counter at start of the current instruction. Note, that
413 '*' yields a rvalue, that means, you cannot assign to it.
414 Use <tt/.ORG/ to set the program counter in sections with
419 <sect>Symbols and labels
422 The assembler allows you to use symbols instead of naked values to make
423 the source more readable. There are a lot of different ways to define and
424 use symbols and labels, giving a lot of flexibility.
429 <tag/Numeric constants/
431 Numeric constants are defined using the equal sign. After doing
437 may use the symbol "two" in every place where a number is expected,
438 and it is evaluated to the value 2 in this context. An example would be
445 <tag/Standard labels/
447 A label is defined by writing the name of the label at the start of
448 the line (before any instruction mnemonic, macro or pseudo
449 directive), followed by a colon. This will declare a symbol with the
450 given name and the value of the current program counter.
453 <tag/Local labels and symbols/
455 Using the <tt/.PROC/ directive, it is possible to create regions of code
456 where the names of labels and symbols are local to this region. They
457 are not know outside and cannot be accessed from there. Such regions
458 may be nested like PROCEDUREs in Pascal.
460 See the description of the <tt/.PROC/ directive for more information.
462 <tag/Cheap local labels/
464 Cheap local labels are defined like standard labels, but the name of
465 the label must begin with a special symbol (usually '@', but this can
466 be changed by the <tt/.LOCALCHAR/ directive).
468 Cheap local labels are visible only between two no cheap labels. As
469 soon as a standard symbol is encountered (this may also be a local
470 symbol if inside a region defined with the .PROC directive), the
471 cheap local symbol goes out of scope.
473 You may use cheap local labels as an easy way to reuse common label
474 names like "Loop". Here is an example:
477 Clear: lda #$00 ; Global label
479 @Loop: sta Mem,y ; Local label
483 Sub: ... ; New global label
484 bne @Loop ; ERROR: Unknown identifier!
489 If you really want to write messy code, there are also unnamed
490 labels. These labels do not have a name (you guessed that already,
491 didn't you?). A colon is used to mark the absence of the name.
493 Unnamed labels may be accessed by using the colon plus several minus
494 or plus characters as a label designator. Using the '-' characters
495 will create a back reference (use the n'th label backwards), using
496 '+' will create a forward reference (use the n'th label in forward
497 direction). An example will help to understand this:
519 As you can see from the example, unnamed labels will make even short
520 sections of code hard to understand, because you have to count labels
521 to find branch targets (this is the reason why I for my part do
522 prefer the "cheap" local labels). Nevertheless, unnamed labels are
523 convenient in some situations, so it's your decision.
525 <tag/Using macros to define labels and constants/
527 While there are drawbacks with this approach, it may be handy in some
528 situations. Using <tt/.DEFINE/, it is possible to define symbols or
529 constants that may be used elsewhere. Since the macro facility works
530 on a very low level, there is no scoping. On the other side, you may
531 also define string constants this way (this is not possible with the
538 .DEFINE version "SOS V2.3"
540 four = two * two ; Ok
543 .PROC ; Start local scope
544 two = 3 ; Will give "2 = 3" - invalid!
551 If <tt/.DEBUGINFO/ is enabled (or -g is given on the command line), global,
552 local and cheap local labels are written to the object file and will be
553 available in the symbol file via the linker. Unnamed labels are not
554 written to the object file, because they don't have a name which would
555 allow to access them.
559 <sect>Control commands
562 Here's a list of all control commands and a description, what they do:
566 <tag><tt><idx>.A16</idx></tt></tag>
568 Valid only in 65816 mode. Switch the accumulator to 16 bit.
570 Note: This command will not emit any code, it will tell the assembler to
571 create 16 bit operands for immediate accumulator adressing mode.
573 See also: <tt/.SMART/
576 <tag><tt><idx>.A8</idx></tt></tag>
578 Valid only in 65816 mode. Switch the accumulator to 8 bit.
580 Note: This command will not emit any code, it will tell the assembler to
581 create 8 bit operands for immediate accu adressing mode.
583 See also: <tt/.SMART/
586 <tag><tt><idx>.ADDR</idx></tt></tag>
588 Define word sized data. In 6502 mode, this is an alias for <tt/.WORD/ and
589 may be used for better readability if the data words are address values.
590 In 65816 mode, the address is forced to be 16 bit wide to fit into the
591 current segment. See also <tt/.FARADDR/. The command must be followed by a
592 sequence of (not necessarily constant) expressions.
597 .addr $0D00, $AF13, _Clear
601 <tag><tt><idx>.ALIGN</idx></tt></tag>
603 Align data to a given boundary. The command expects a constant integer
604 argument that must be a power of two, plus an optional second argument
605 in byte range. If there is a second argument, it is used as fill value,
606 otherwise the value defined in the linker configuration file is used
607 (the default for this value is zero).
609 Since alignment depends on the base address of the module, you must
610 give the same (or a greater) alignment for the segment when linking.
611 The linker will give you a warning, if you don't do that.
620 <tag><tt><idx>.ASCIIZ</idx></tt></tag>
622 Define a string with a trailing zero.
627 Msg: .asciiz "Hello world"
630 This will put the string "Hello world" followed by a binary zero into
631 the current segment. There may be more strings separated by commas, but
632 the binary zero is only appended once (after the last one).
635 <tag><tt><idx>.AUTOIMPORT</idx></tt></tag>
637 Is followd by a plus or a minus character. When switched on (using a
638 +), undefined symbols are automatically marked as import instead of
639 giving errors. When switched off (which is the default so this does not
640 make much sense), this does not happen and an error message is
641 displayed. The state of the autoimport flag is evaluated when the
642 complete source was translated, before outputing actual code, so it is
643 <em/not/ possible to switch this feature on or off for separate sections
644 of code. The last setting is used for all symbols.
646 You should probably not use this switch because it delays error
647 messages about undefined symbols until the link stage. The cc65
648 compiler (which is supposed to produce correct assembler code in all
649 circumstances, something which is not true for most assembler
650 programmers) will insert this command to avoid importing each and every
651 routine from the runtime library.
656 .autoimport + ; Switch on auto import
660 <tag><tt><idx>.BLANK</idx></tt></tag>
662 Builtin function. The function evaluates its argument in braces and
663 yields "false" if the argument is non blank (there is an argument), and
664 "true" if there is no argument. As an example, the <tt/.IFBLANK/ statement
672 <tag><tt><idx>.BSS</idx></tt></tag>
674 Switch to the BSS segment. The name of the BSS segment is always "BSS",
675 so this is a shortcut for
681 See also the <tt/.SEGMENT/ command.
684 <tag><tt><idx>.BYTE</idx></tt></tag>
686 Define byte sized data. Must be followed by a sequence of (byte ranged)
687 expressions or strings.
692 .byte "Hello world", $0D, $00
696 <tag><tt><idx>.CASE</idx></tt></tag>
698 Switch on or off case sensitivity on identifiers. The default is off
699 (that is, identifiers are case sensitive), but may be changed by the
700 -i switch on the command line.
701 The command must be followed by a '+' or '-' character to switch the
702 option on or off respectively.
707 .case - ; Identifiers are not case sensitive
711 <tag><tt><idx>.CODE</idx></tt></tag>
713 Switch to the CODE segment. The name of the CODE segment is always
714 "CODE", so this is a shortcut for
720 See also the <tt/.SEGMENT/ command.
723 <tag><tt><idx>.CONCAT</idx></tt></tag>
725 Builtin function. The function allows to concatenate a list of string
726 constants separated by commas. The result is a string constant that
727 is the concatentation of all arguments. This function is most useful
728 in macros and when used together with the <tt/.STRING/ builtin function.
729 The function may be used in any case where a string constant is
735 .include .concat ("myheader", ".", "inc)
738 This is the same as the command
741 .include "myheader.inc"
745 <tag><tt><idx>.CONST</idx></tt></tag>
747 Builtin function. The function evaluates its argument in braces and
748 yields "true" if the argument is a constant expression (that is, an
749 expression that yields a constant value at assembly time) and "false"
750 otherwise. As an example, the .IFCONST statement may be replaced by
757 <tag><tt><idx>.CPU</idx></tt></tag>
759 Reading this pseudo variable will give a constant integer value that
760 tells which instruction set is currently enabled. Possible values are:
769 It may be used to replace the .IFPxx pseudo instructions or to construct
770 even more complex expressions.
775 .if (.cpu = 0) .or (.cpu = 1)
787 <tag><tt><idx>.DATA</idx></tt></tag>
789 Switch to the DATA segment. The name of the DATA segment is always
790 "DATA", so this is a shortcut for
796 See also the <tt/.SEGMENT/ command.
799 <tag><tt><idx>.DBYT</idx></tt></tag>
801 Define word sized data with the hi and lo bytes swapped (use <tt/.WORD/ to
802 create word sized data in native 65XX format). Must be followed by a
803 sequence of (word ranged) expressions.
811 This will emit the bytes
817 into the current segment in that order.
820 <tag><tt><idx>.DEBUGINFO</idx></tt></tag>
822 Switch on or off debug info generation. The default is off (that is,
823 the object file will not contain debug infos), but may be changed by the
824 -g switch on the command line.
825 The command must be followed by a '+' or '-' character to switch the
826 option on or off respectively.
831 .debuginfo + ; Generate debug info
835 <tag><tt><idx>.DEFINE</idx></tt></tag>
837 Start a define style macro definition. The command is followed by an
838 identifier (the macro name) and optionally by a list of formal arguments
840 See separate section about macros.
843 <tag><tt><idx>.DEF,</idx> <idx>.DEFINED</idx></tt></tag>
845 Builtin function. The function expects an identifier as argument in
846 braces. The argument is evaluated, and the function yields "true" if the
847 identifier is a symbol that is already defined somewhere in the source
848 file up to the current position. Otherwise the function yields false. As
849 an example, the <tt/.IFDEF/ statement may be replaced by
856 <tag><tt><idx>.DWORD</idx></tt></tag>
858 Define dword sized data (4 bytes) Must be followed by a sequence of
864 .dword $12344512, $12FA489
868 <tag><tt><idx>.ELSE</idx></tt></tag>
870 Conditional assembly: Reverse the current condition.
873 <tag><tt><idx>.ELSEIF</idx></tt></tag>
875 Conditional assembly: Reverse current condition and test a new one.
878 <tag><tt><idx>.END</idx></tt></tag>
880 Forced end of assembly. Assembly stops at this point, even if the command
881 is read from an include file.
884 <tag><tt><idx>.ENDIF</idx></tt></tag>
886 Conditional assembly: Close a <tt/.IF.../ or <tt/.ELSE/ branch.
889 <tag><tt><idx>.ENDMAC,</idx> <idx>.ENDMACRO</idx></tt></tag>
891 End of macro definition (see separate section).
894 <tag><tt><idx>.ENDPROC</idx></tt></tag>
896 End of local lexical level (see <tt/.PROC/).
899 <tag><tt><idx>.ENDREP,</idx> <idx>.ENDREPEAT</idx></tt></tag>
901 End a <tt/.REPEAT/ block. See the <tt/.REPEAT/ command.
904 <tag><tt><idx>.ERROR</idx></tt></tag>
906 Force an assembly error. The assembler will output an error message
907 preceeded by "User error" and will <em/not/ produce an object file.
909 This command may be used to check for initial conditions that must be
910 set before assembling a source file.
920 .error "Must define foo or bar!"
924 See also the <tt/.WARNING/ and <tt/.OUT/ directives.
927 <tag><tt><idx>.EXITMAC,</idx> <idx>.EXITMACRO</idx></tt></tag>
929 Abort a macro expansion immidiately. This command is often useful in
930 recursive macros. See separate chapter about macros.
933 <tag><tt><idx>.EXPORT</idx></tt></tag>
935 Make symbols accessible from other modules. Must be followed by a comma
936 separated list of symbols to export.
945 <tag><tt><idx>.EXPORTZP</idx></tt></tag>
947 Make symbols accessible from other modules. Must be followed by a comma
948 separated list of symbols to export. The exported symbols are explicitly
949 marked as zero page symols.
958 <tag><tt><idx>.FARADDR</idx></tt></tag>
960 Define far (24 bit) address data. The command must be followed by a
961 sequence of (not necessarily constant) expressions.
966 .faraddr DrawCircle, DrawRectangle, DrawHexagon
970 <tag><tt><idx>.FEATURE</idx></tt></tag>
972 This directive may be used to enable one or more compatibility features
973 of the assembler. While the use of <tt/.FEATURE/ should be avoided when
974 possible, it may be useful when porting sources written for other
975 assemblers. There is no way to switch a feature off, once you have
982 will enable the feature until end of assembly is reached.
984 The following features are available:
988 <tag><tt<idx>>dollar_is_pc</idx></tt></tag>
990 The dollar sign may be used as an alias for the star (`*'), which
991 gives the value of the current PC in expressions.
992 Note: Assignment to the pseudo variable is not allowed.
994 <tag><tt<idx>>labels_without_colons</idx></tt></tag>
996 Allow labels without a trailing colon. These labels are only accepted,
997 if they start at the beginning of a line (no leading white space).
999 <tag><tt<idx>>loose_string_term</idx></tt></tag>
1001 Accept single quotes as well as double quotes as terminators for string
1004 <tag><tt<idx>>at_in_identifiers</idx></tt></tag>
1006 Accept the at character (`@') as a valid character in identifiers. The
1007 at character is not allowed to start an identifier, even with this
1010 <tag><tt<idx>>dollar_in_identifiers</idx></tt></tag>
1012 Accept the dollar sign (`$') as a valid character in identifiers. The
1013 at character is not allowed to start an identifier, even with this
1019 <tag><tt><idx>.FILEOPT,</idx> <idx>.FOPT</idx></tt></tag>
1021 Insert an option string into the object file. There are two forms of
1022 this command, one specifies the option by a keyword, the second
1023 specifies it as a number. Since usage of the second one needs knowledge
1024 of the internal encoding, its use is not recommended and I will only
1025 describe the first form here.
1027 The command is followed by one of the keywords
1035 a comma and a string. The option is written into the object file
1036 together with the string value. This is currently unidirectional and
1037 there is no way to actually use these options once they are in the
1043 .fileopt comment, "Code stolen from my brother"
1044 .fileopt compiler, "BASIC 2.0"
1045 .fopt author, "J. R. User"
1049 <tag><tt><idx>.GLOBAL</idx></tt></tag>
1051 Declare symbols as global. Must be followed by a comma separated list
1052 of symbols to declare. Symbols from the list, that are defined somewhere
1053 in the source, are exported, all others are imported. An additional
1054 explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same symbol is
1064 <tag><tt><idx>.GLOBALZP</idx></tt></tag>
1066 Declare symbols as global. Must be followed by a comma separated list
1067 of symbols to declare. Symbols from the list, that are defined
1068 somewhere in the source, are exported, all others are imported. An
1069 additional explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same
1070 symbol is explicitly allowed. The symbols in the list are explicitly
1071 marked as zero page symols.
1080 <tag><tt><idx>.I16</idx></tt></tag>
1082 Valid only in 65816 mode. Switch the index registers to 16 bit.
1084 Note: This command will not emit any code, it will tell the assembler to
1085 create 16 bit operands for immediate operands.
1087 See also the <tt/.SMART/ command.
1090 <tag><tt><idx>.I8</idx></tt></tag>
1092 Valid only in 65816 mode. Switch the index registers to 8 bit.
1094 Note: This command will not emit any code, it will tell the assembler to
1095 create 8 bit operands for immediate operands.
1097 See also the <tt/.SMART/ command.
1100 <tag><tt><idx>.IF</idx></tt></tag>
1102 Conditional assembly: Evalute an expression and switch assembler output
1103 on or off depending on the expression. The expression must be a constant
1104 expression, that is, all operands must be defined.
1106 A expression value of zero evaluates to FALSE, any other value evaluates
1110 <tag><tt><idx>.IFBLANK</idx></tt></tag>
1112 Conditional assembly: Check if there are any remaining tokens in this
1113 line, and evaluate to FALSE if this is the case, and to TRUE otherwise.
1114 If the condition is not true, further lines are not assembled until
1115 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1117 This command is often used to check if a macro parameter was given.
1118 Since an empty macro parameter will evaluate to nothing, the condition
1119 will evaluate to FALSE if an empty parameter was given.
1133 See also: <tt/.BLANK/
1136 <tag><tt><idx>.IFCONST</idx></tt></tag>
1138 Conditional assembly: Evaluate an expression and switch assembler output
1139 on or off depending on the constness of the expression.
1141 A const expression evaluates to to TRUE, a non const expression (one
1142 containing an imported or currently undefined symbol) evaluates to
1145 See also: <tt/.CONST/
1148 <tag><tt><idx>.IFDEF</idx></tt></tag>
1150 Conditional assembly: Check if a symbol is defined. Must be followed by
1151 a symbol name. The condition is true if the the given symbol is already
1152 defined, and false otherwise.
1154 See also: <tt/.DEFINED/
1157 <tag><tt><idx>.IFNBLANK</idx></tt></tag>
1159 Conditional assembly: Check if there are any remaining tokens in this
1160 line, and evaluate to TRUE if this is the case, and to FALSE otherwise.
1161 If the condition is not true, further lines are not assembled until
1162 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1164 This command is often used to check if a macro parameter was given.
1165 Since an empty macro parameter will evaluate to nothing, the condition
1166 will evaluate to FALSE if an empty parameter was given.
1179 See also: <tt/.BLANK/
1182 <tag><tt><idx>.IFNDEF</idx></tt></tag>
1184 Conditional assembly: Check if a symbol is defined. Must be followed by
1185 a symbol name. The condition is true if the the given symbol is not
1186 defined, and false otherwise.
1188 See also: <tt/.DEFINED/
1191 <tag><tt><idx>.IFNREF</idx></tt></tag>
1193 Conditional assembly: Check if a symbol is referenced. Must be followed
1194 by a symbol name. The condition is true if if the the given symbol was
1195 not referenced before, and false otherwise.
1197 See also: <tt/.REFERENCED/
1200 <tag><tt><idx>.IFP02</idx></tt></tag>
1202 Conditional assembly: Check if the assembler is currently in 6502 mode
1203 (see <tt/.P02/ command).
1206 <tag><tt><idx>.IFP816</idx></tt></tag>
1208 Conditional assembly: Check if the assembler is currently in 65816 mode
1209 (see <tt/.P816/ command).
1212 <tag><tt><idx>.IFPC02</idx></tt></tag>
1214 Conditional assembly: Check if the assembler is currently in 65C02 mode
1215 (see <tt/.PC02/ command).
1218 <tag><tt><idx>.IFREF</idx></tt></tag>
1220 Conditional assembly: Check if a symbol is referenced. Must be followed
1221 by a symbol name. The condition is true if if the the given symbol was
1222 referenced before, and false otherwise.
1224 This command may be used to build subroutine libraries in include files
1225 (you may use separate object modules for this purpose too).
1230 .ifref ToHex ; If someone used this subroutine
1231 ToHex: tay ; Define subroutine
1237 See also: <tt/.REFERENCED/
1240 <tag><tt><idx>.IMPORT</idx></tt></tag>
1242 Import a symbol from another module. The command is followed by a comma
1243 separated list of symbols to import.
1252 <tag><tt><idx>.IMPORTZP</idx></tt></tag>
1254 Import a symbol from another module. The command is followed by a comma
1255 separated list of symbols to import. The symbols are explicitly imported
1256 as zero page symbols (that is, symbols with values in byte range).
1265 <tag><tt><idx>.INCBIN</idx></tt></tag>
1267 Include a file as binary data. The command expects a string argument
1268 that is the name of a file to include literally in the current segment.
1273 .incbin "sprites.dat"
1277 <tag><tt><idx>.INCLUDE</idx></tt></tag>
1279 Include another file. Include files may be nested up to a depth of 16.
1288 <tag><tt><idx>.LEFT</idx></tt></tag>
1290 Builtin function. Extracts the left part of a given token list.
1295 .LEFT (<int expr>, <token list>)
1298 The first integer expression gives the number of tokens to extract from
1299 the token list. The second argument is the token list itself.
1303 To check in a macro if the given argument has a '#' as first token
1304 (immidiate addressing mode), use something like this:
1309 .if (.match (.left (1, arg), #))
1311 ; ldax called with immidiate operand
1319 See also the <tt/.MID/ and <tt/.RIGHT/ builtin functions.
1322 <tag><tt><idx>.LINECONT</idx></tt></tag>
1324 Switch on or off line continuations using the backslash character
1325 before a newline. The option is off by default.
1326 Note: Line continuations do not work in a comment. A backslash at the
1327 end of a comment is treated as part of the comment and does not trigger
1329 The command must be followed by a '+' or '-' character to switch the
1330 option on or off respectively.
1335 .linecont + ; Allow line continuations
1338 #$20 ; This is legal now
1342 <tag><tt><idx>.LIST</idx></tt></tag>
1344 Enable output to the listing. The command must be followed by a boolean
1345 switch ("on", "off", "+" or "-") and will enable or disable listing
1347 The option has no effect if the listing is not enabled by the command line
1348 switch -l. If -l is used, an internal counter is set to 1. Lines are output
1349 to the listing file, if the counter is greater than zero, and suppressed if
1350 the counter is zero. Each use of <tt/.LIST/ will increment or decrement the
1356 .list on ; Enable listing output
1360 <tag><tt><idx>.LISTBYTES</idx></tt></tag>
1362 Set, how many bytes are shown in the listing for one source line. The
1363 default is 12, so the listing will show only the first 12 bytes for any
1364 source line that generates more than 12 bytes of code or data.
1365 The directive needs an argument, which is either "unlimited", or an
1366 integer constant in the range 4..255.
1371 .listbytes unlimited ; List all bytes
1372 .listbytes 12 ; List the first 12 bytes
1373 .incbin "data.bin" ; Include large binary file
1377 <tag><tt><idx>.LOCAL</idx></tt></tag>
1379 This command may only be used inside a macro definition. It declares a
1380 list of identifiers as local to the macro expansion.
1382 A problem when using macros are labels: Since they don't change their
1383 name, you get a "duplicate symbol" error if the macro is expanded the
1384 second time. Labels declared with <tt/.LOCAL/ have their name mapped to
1385 an internal unique name (<tt/___ABCD__/) with each macro invocation.
1387 Some other assemblers start a new lexical block inside a macro
1388 expansion. This has some drawbacks however, since that will not allow
1389 <em/any/ symbol to be visible outside a macro, a feature that is sometimes
1390 useful. The <tt/.LOCAL/ command is in my eyes a better way to address
1393 You get an error when using <tt/.LOCAL/ outside a macro.
1396 <tag><tt><idx>.LOCALCHAR</idx></tt></tag>
1398 Defines the character that start "cheap" local labels. You may use one
1399 of '@' and '?' as start character. The default is '@'.
1401 Cheap local labels are labels that are visible only between two non
1402 cheap labels. This way you can reuse identifiers like "<tt/loop/" without
1403 using explicit lexical nesting.
1410 Clear: lda #$00 ; Global label
1411 ?Loop: sta Mem,y ; Local label
1415 Sub: ... ; New global label
1416 bne ?Loop ; ERROR: Unknown identifier!
1420 <tag><tt><idx>.MACPACK</idx></tt></tag>
1422 Insert a predefined macro package. The command is followed by an
1423 identifier specifying the macro package to insert. Available macro
1426 generic Defines generic macros like add and sub.
1427 longbranch Defines conditional long jump macros.
1429 Including a macro package twice, or including a macro package that
1430 redefines already existing macros will lead to an error.
1435 .macpack longbranch ; Include macro package
1437 cmp #$20 ; Set condition codes
1438 jne Label ; Jump long on condition
1441 See separate section about macros packages.
1444 <tag><tt><idx>.MAC,</idx> <idx>.MACRO</idx></tt></tag>
1446 Start a classic macro definition. The command is followed by an identifier
1447 (the macro name) and optionally by a comma separated list of identifiers
1448 that are macro parameters.
1449 See separate section about macros.
1452 <tag><tt><idx>.MATCH</idx></tt></tag>
1454 Builtin function. Matches two token lists against each other. This is
1455 most useful within macros, since macros are not stored as strings, but
1461 .MATCH(<token list #1>, <token list #2>)
1464 Both token list may contain arbitrary tokens with the exception of the
1465 terminator token (comma resp. right parenthesis) and
1472 Often a macro parameter is used for any of the token lists.
1474 Please note that the function does only compare tokens, not token
1475 attributes. So any number is equal to any other number, regardless of
1476 the actual value. The same is true for strings. If you need to compare
1477 tokens <em/and/ token attributes, use the <tt/.XMATCH/ function.
1481 Assume the macro <tt/ASR/, that will shift right the accumulator by one, while
1482 honoring the sign bit. The builtin processor instructions will allow an
1483 optional "A" for accu addressing for instructions like <tt/ROL/ and <tt/ROR/.
1484 We will use the <tt/.MATCH/ function to check for this and print and error
1490 .if (.not .blank(arg)) .and (.not .match (arg, a))
1491 .error "Syntax error"
1494 cmp #$80 ; Bit 7 into carry
1495 lsr a ; Shit carry into bit 7
1500 The macro will only accept no arguments, or one argument that must be the
1501 reserved keyword "A".
1504 <tag><tt><idx>.MID</idx></tt></tag>
1506 Builtin function. Takes a starting index, a count and a token list as
1507 arguments. Will return part of the token list.
1512 .MID (<int expr>, <int expr>, <token list>)
1515 The first integer expression gives the starting token in the list (the
1516 first token has index 0). The second integer expression gives the number
1517 of tokens to extract from the token list. The third argument is the
1522 To check in a macro if the given argument has a '<tt/#/' as first token
1523 (immidiate addressing mode), use something like this:
1528 .if (.match (.mid (0, 1, arg), #))
1530 ; ldax called with immidiate operand
1538 See also the <tt/.LEFT/ and <tt/.RIGHT/ builtin functions.
1541 <tag><tt><idx>.ORG</idx></tt></tag>
1543 Start a section of absolute code. The command is followed by a constant
1544 expression that gives the new PC counter location for which the code is
1545 assembled. Use <tt/.RELOC/ to switch back to relocatable code.
1547 You may not switch segments while inside a section of absolute code.
1552 .org $7FF ; Emit code starting at $7FF
1556 <tag><tt><idx>.OUT</idx></tt></tag>
1558 Output a string to the console without producing an error. This command
1559 is similiar to <tt/.ERROR/, however, it does not force an assembler error
1560 that prevents the creation of an object file.
1565 .out "This code was written by the codebuster(tm)"
1568 See also the <tt/.WARNING/ and <tt/.ERROR/ directives.
1571 <tag><tt><idx>.P02</idx></tt></tag>
1573 Enable the 6502 instruction set, disable 65C02 and 65816 instructions.
1574 This is the default if not overridden by the <tt/--cpu/ command line
1578 <tag><tt><idx>.P816</idx></tt></tag>
1580 Enable the 65816 instruction set. This is a superset of the 65C02 and
1581 6502 instruction sets.
1584 <tag><tt><idx>.PAGELEN,</idx> <idx>.PAGELENGTH</idx></tt></tag>
1586 Set the page length for the listing. Must be followed by an integer
1587 constant. The value may be "unlimited", or in the range 32 to 127. The
1588 statement has no effect if no listing is generated. The default value
1589 is -1 but may be overridden by the <tt/--pagelength/ command line option.
1590 Beware: Since the listing is generated after assembly is complete, you
1591 cannot use multiple line lengths with one source. Instead, the value
1592 set with the last <tt/.PAGELENGTH/ is used.
1597 .pagelength 66 ; Use 66 lines per listing page
1599 .pagelength unlimited ; Unlimited page length
1603 <tag><tt><idx>.PARAMCOUNT</idx></tt></tag>
1605 This builtin pseudo variable is only available in macros. It is replaced
1606 by the actual number of parameters that were given in the macro
1612 .macro foo arg1, arg2, arg3
1613 .if .paramcount <> 3
1614 .error "Too few parameters for macro foo"
1621 <tag><tt><idx>.PC02</idx></tt></tag>
1623 Enable the 65C02 instructions set. This instruction set includes all
1627 <tag><tt><idx>.PROC</idx></tt></tag>
1629 Start a nested lexical level. All new symbols from now on are in the
1630 local lexical level and are not accessible from outside. Symbols defined
1631 outside this local level may be accessed as long as their names are not
1632 used for new symbols inside the level. Symbols names in other lexical
1633 levels do not clash, so you may use the same names for identifiers. The
1634 lexical level ends when the <tt/.ENDPROC/ command is read. Lexical levels
1635 may be nested up to a depth of 16.
1637 The command may be followed by an identifier, in this case the
1638 identifier is declared in the outer level as a label having the value of
1639 the program counter at the start of the lexical level.
1641 Note: Macro names are always in the global level and in a separate name
1642 space. There is no special reason for this, it's just that I've never
1643 had any need for local macro definitions.
1648 .proc Clear ; Define Clear subroutine, start new level
1650 L1: sta Mem,y ; L1 is local and does not cause a
1651 ; duplicate symbol error if used in other
1654 bne L1 ; Reference local symbol
1656 .endproc ; Leave lexical level
1660 <tag><tt><idx>.REF,</idx> <idx>.REFERENCED</idx></tt></tag>
1662 Builtin function. The function expects an identifier as argument in
1663 braces. The argument is evaluated, and the function yields "true" if the
1664 identifier is a symbol that has already been referenced somewhere in the
1665 source file up to the current position. Otherwise the function yields
1666 false. As an example, the <tt/.IFREF/ statement may be replaced by
1673 <tag><tt><idx>.REPEAT</idx></tt></tag>
1675 Repeat all commands between <tt/.REPEAT/ and <tt/.ENDREPEAT/ a constant
1676 number of times. The command is followed by a constant expression that tells
1677 how many times the commands in the body should get repeated. Optionally, a
1678 comma and an identifier may be specified. If this identifier is found in
1679 the body of the repeat statement, it is replaced by the current repeat
1680 count (starting with zero for the first time the body is repeated).
1682 <tt/.REPEAT/ statements may be nested. If you use the same repeat count
1683 identifier for a nested <tt/.REPEAT/ statement, the one from the inner
1684 level will be used, not the one from the outer level.
1688 The following macro will emit a string that is "encrypted" in that all
1689 characters of the string are XORed by the value $55.
1693 .repeat strlen(Arg), I
1694 .byte strat(Arg, I) .xor $55
1700 <tag><tt><idx>.RELOC</idx></tt></tag>
1702 Switch back to relocatable mode. See the <tt/.ORG/ command.
1705 <tag><tt><idx>.RES</idx></tt></tag>
1707 Reserve storage. The command is followed by one or two constant
1708 expressions. The first one is mandatory and defines, how many bytes of
1709 storage should be defined. The second, optional expression must by a
1710 constant byte value that will be used as value of the data. If there
1711 is no fill value given, the linker will use the value defined in the
1712 linker configuration file (default: zero).
1717 ; Reserve 12 bytes of memory with value $AA
1722 <tag><tt><idx>.RIGHT</idx></tt></tag>
1724 Builtin function. Extracts the right part of a given token list.
1729 .RIGHT (<int expr>, <token list>)
1732 The first integer expression gives the number of tokens to extract from
1733 the token list. The second argument is the token list itself.
1735 See also the <tt/.LEFT/ and <tt/.MID/ builtin functions.
1738 <tag><tt><idx>.RODATA</idx></tt></tag>
1740 Switch to the RODATA segment. The name of the RODATA segment is always
1741 "RODATA", so this is a shortcut for
1747 The RODATA segment is a segment that is used by the compiler for
1748 readonly data like string constants. See also the <tt/.SEGMENT/ command.
1751 <tag><tt><idx>.SEGMENT</idx></tt></tag>
1753 Switch to another segment. Code and data is always emitted into a
1754 segment, that is, a named section of data. The default segment is
1755 "CODE". There may be up to 254 different segments per object file
1756 (and up to 65534 per executable). There are shortcut commands for
1757 the most common segments ("CODE", "DATA" and "BSS").
1759 The command is followed by a string containing the segment name (there
1760 are some constraints for the name - as a rule of thumb use only those
1761 segment names that would also be valid identifiers). There may also be
1762 an optional attribute separated by a comma. Valid attributes are
1763 "<tt/zeropage/" and "<tt/absolute/".
1765 When specifying a segment for the first time, "absolute" is the
1766 default. For all other uses, the attribute specified the first time
1769 "absolute" means that this is a segment with absolute addressing. That
1770 is, the segment will reside somewhere in core memory outside the zero
1771 page. "zeropage" means the opposite: The segment will be placed in the
1772 zero page and direct (short) addressing is possible for data in this
1775 Beware: Only labels in a segment with the zeropage attribute are marked
1776 as reachable by short addressing. The `*' (PC counter) operator will
1777 work as in other segments and will create absolute variable values.
1782 .segment "ROM2" ; Switch to ROM2 segment
1783 .segment "ZP2", zeropage ; New direct segment
1784 .segment "ZP2" ; Ok, will use last attribute
1785 .segment "ZP2", absolute ; Error, redecl mismatch
1789 <tag><tt><idx>.SMART</idx></tt></tag>
1791 Switch on or off smart mode. The command must be followed by a '+' or
1792 '-' character to switch the option on or off respectively. The default
1793 is off (that is, the assembler doesn't try to be smart), but this
1794 default may be changed by the -s switch on the command line.
1796 In smart mode the assembler will track usage of the <tt/REP/ and <tt/SEP/
1797 instructions in 65816 mode and update the operand sizes accordingly. If
1798 the operand of such an instruction cannot be evaluated by the assembler
1799 (for example, because the operand is an imported symbol), a warning is
1800 issued. Beware: Since the assembler cannot trace the execution flow this
1801 may lead to false results in some cases. If in doubt, use the <tt/.Inn/ and
1802 <tt/.Ann/ instructions to tell the assembler about the current settings.
1808 .smart - ; Stop being smart
1812 <tag><tt><idx>.STRAT</idx></tt></tag>
1814 Builtin function. The function accepts a string and an index as
1815 arguments and returns the value of the character at the given position
1816 as an integer value. The index is zero based.
1822 ; Check if the argument string starts with '#'
1823 .if (.strat (Arg, 0) = '#')
1830 <tag><tt><idx>.STRING</idx></tt></tag>
1832 Builtin function. The function accepts an argument in braces and
1833 converts this argument into a string constant. The argument may be an
1834 identifier, or a constant numeric value.
1835 Since you can use a string in the first place, the use of the function
1836 may not be obvious. However, it is useful in macros, or more complex
1842 ; Emulate other assemblers:
1844 .segment .string(name)
1849 <tag><tt><idx>.STRLEN</idx></tt></tag>
1851 Builtin function. The function accepts a string argument in braces and
1852 eveluates to the length of the string.
1856 The following macro encodes a string as a pascal style string with
1857 a leading length byte.
1861 .byte .strlen(Arg), Arg
1866 <tag><tt><idx>.TCOUNT</idx></tt></tag>
1868 Builtin function. The function accepts a token list in braces. The
1869 function result is the number of tokens given as argument.
1873 The <tt/ldax/ macro accepts the '#' token to denote immidiate addressing (as
1874 with the normal 6502 instructions). To translate it into two separate 8 bit
1875 load instructions, the '#' token has to get stripped from the argument:
1879 .if (.match (.mid (0, 1, arg), #))
1880 ; ldax called with immidiate operand
1881 lda #<(.right (.tcount (arg)-1, arg))
1882 ldx #>(.right (.tcount (arg)-1, arg))
1890 <tag><tt><idx>.WARNING</idx></tt></tag>
1892 Force an assembly warning. The assembler will output a warning message
1893 preceeded by "User warning". This warning will always be output, even
1894 if other warnings are disabled with the <tt/-W0/ command line option.
1896 This command may be used to output possible problems when assembling
1905 .warning "Forward jump in jne, cannot optimize!"
1915 See also the <tt/.ERROR/ and <tt/.OUT/ directives.
1918 <tag><tt><idx>.WORD</idx></tt></tag>
1920 Define word sized data. Must be followed by a sequence of (word ranged,
1921 but not necessarily constant) expressions.
1926 .word $0D00, $AF13, _Clear
1930 <tag><tt><idx>.ZEROPAGE</idx></tt></tag>
1932 Switch to the ZEROPAGE segment and mark it as direct (zeropage) segment.
1933 The name of the ZEROPAGE segment is always "ZEROPAGE", so this is a
1937 .segment "ZEROPAGE", zeropage
1940 Because of the "zeropage" attribute, labels declared in this segment are
1941 addressed using direct addressing mode if possible. You <em/must/ instruct
1942 the linker to place this segment somewhere in the address range 0..$FF
1943 otherwise you will get errors.
1952 Macros may be thought of as "parametrized super instructions". Macros are
1953 sequences of tokens that have a name. If that name is used in the source
1954 file, the macro is "expanded", that is, it is replaced by the tokens that
1955 were specified when the macro was defined.
1957 In it's simplest form, a macro does not have parameters. Here's an
1961 .macro asr ; Arithmetic shift right
1962 cmp #$80 ; Put bit 7 into carry
1963 ror ; Rotate right with carry
1967 The macro above consists of two real instructions, that are inserted into
1968 the code, whenever the macro is expanded. Macro expansion is simply done
1969 by using the name, like this:
1977 When using macro parameters, macros can be even more useful:
1991 When calling the macro, you may give a parameter, and each occurence of
1992 the name "addr" in the macro definition will be replaced by the given
2011 A macro may have more than one parameter, in this case, the parameters
2012 are separated by commas. You are free to give less parameters than the
2013 macro actually takes in the definition. You may also leave intermediate
2014 parameters empty. Empty parameters are replaced by empty space (that is,
2015 they are removed when the macro is exanded). If you have a look at our
2016 macro definition above, you will see, that replacing the "addr" parameter
2017 by nothing will lead to wrong code in most lines. To help you, writing
2018 macros with a variable parameter list, there are some control commands:
2020 <tt/.IFBLANK/ tests the rest of the line and returns true, if there are any
2021 tokens on the remainder of the line. Since empty parameters are replaced by
2022 nothing, this may be used to test if a given parameter is empty.
2023 <tt/.IFNBLANK/ tests the opposite.
2025 Look at this example:
2028 .macro ldaxy a, x, y
2041 This macro may be called as follows:
2044 ldaxy 1, 2, 3 ; Load all three registers
2046 ldaxy 1, , 3 ; Load only a and y
2048 ldaxy , , 3 ; Load y only
2051 There's another helper command for determining, which macro parameters are
2052 valid: <tt/.PARAMCOUNT/. This command is replaced by the parameter count
2053 given, <em/including/ intermediate empty macro parameters:
2056 ldaxy 1 ; .PARAMCOUNT = 1
2057 ldaxy 1,,3 ; .PARAMCOUNT = 3
2058 ldaxy 1,2 ; .PARAMCOUNT = 2
2059 ldaxy 1, ; .PARAMCOUNT = 2
2060 ldaxy 1,2,3 ; .PARAMCOUNT = 3
2063 Macros may be used recursively:
2066 .macro push r1, r2, r3
2075 There's also a special macro to help writing recursive macros:
2076 <tt/.EXITMACRO/. This command will stop macro expansion immidiately:
2079 .macro push r1, r2, r3, r4, r5, r6, r7
2081 ; First parameter is empty
2087 push r2, r3, r4, r5, r6, r7
2091 When expanding this macro, the expansion will push all given parameters
2092 until an empty one is encountered. The macro may be called like this:
2095 push $20, $21, $32 ; Push 3 ZP locations
2096 push $21 ; Push one ZP location
2099 Now, with recursive macros, <tt/.IFBLANK/ and <tt/.PARAMCOUNT/, what else do
2100 you need? Have a look at the inc16 macro above. Here is it again:
2114 If you have a closer look at the code, you will notice, that it could be
2115 written more efficiently, like this:
2129 But imagine what happens, if you use this macro twice? Since the label
2130 "Skip" has the same name both times, you get a "duplicate symbol" error.
2131 Without a way to circumvent this problem, macros are not as useful, as
2132 they could be. One solution is, to start a new lexical block inside the
2149 Now the label is local to the block and not visible outside. However,
2150 sometimes you want a label inside the macro to be visible outside. To make
2151 that possible, there's a new command that's only usable inside a macro
2152 definition: <tt/.LOCAL/. <tt/.LOCAL/ declares one or more symbols as local to
2153 the macro expansion. The names of local variables are replaced by a unique
2154 name in each separate macro expansion. So we could also solve the problem
2155 above by using <tt/.LOCAL/:
2159 .local Skip ; Make Skip a local symbol
2166 Skip: ; Not visible outside
2170 Starting with version 2.5 of the assembler, there is a second macro type
2171 available: C style macros using the <tt/.DEFINE/ directive. These macros are
2172 similar to the classic macro type speified above, but behaviour is sometimes
2177 <item> Macros defined with <tt/.DEFINE/ may not span more than a line. You
2178 may use line continuation (see <tt/.LINECONT/) to spread the
2179 definition over more than one line for increased readability, but the
2180 macro itself does not contain an end-of-line token.
2182 <item> Macros defined with <tt/.DEFINE/ share the name space with classic
2183 macros, but they are detected and replaced at the scanner level. While
2184 classic macros may be used in every place, where a mnemonic or other
2185 directive is allowed, <tt/.DEFINE/ style macros are allowed anywhere
2186 in a line. So they are more versatile in some situations.
2188 <item> <tt/.DEFINE/ style macros may take parameters. While classic macros
2189 may have empty parameters, this is not true for <tt/.DEFINE/ style
2190 macros. For this macro type, the number of actual parameters must
2191 match exactly the number of formal parameters.
2193 To make this possible, formal parameters are enclosed in braces when
2194 defining the macro. If there are no parameters, the empty braces may
2197 <item> Since <tt/.DEFINE/ style macros may not contain end-of-line tokens,
2198 there are things that cannot be done. They may not contain several
2199 processor instructions for example. So, while some things may be done
2200 with both macro types, each type has special usages. The types
2201 complement each other.
2205 Let's look at a few examples to make the advantages and disadvantages
2208 To emulate assemblers that use "<tt/EQU/" instead of "<tt/=/" you may use the
2209 following <tt/.DEFINE/:
2214 foo EQU $1234 ; This is accepted now
2217 You may use the directive to define string constants used elsewhere:
2220 ; Define the version number
2221 .define VERSION "12.3a"
2227 Macros with parameters may also be useful:
2230 .define DEBUG(message) .out message
2232 DEBUG "Assembling include file #3"
2235 Note that, while formal parameters have to be placed in braces, this is
2236 not true for the actual parameters. Beware: Since the assembler cannot
2237 detect the end of one parameter, only the first token is used. If you
2238 don't like that, use classic macros instead:
2246 (This is an example where a problem can be solved with both macro types).
2250 <sect>Macro packages
2253 Using the <tt/.MACPACK/ directive, predefined macro packages may be included
2254 with just one command. Available macro packages are:
2258 <tag><tt><idx>generic</idx></tt></tag>
2260 This macro package defines macros that are useful in almost any program.
2261 Currently, two macros are defined:
2276 <tag><tt><idx>longbranch</idx></tt></tag>
2278 This macro package defines long conditional jumps. They are named like the
2279 short counterpart but with the 'b' replaced by a 'j'. Here is a sample
2280 definition for the "<tt/jeq/" macro, the other macros are built using the
2285 .if .def(Target) .and ((*+2)-(Target) <= 127)
2294 All macros expand to a short branch, if the label is already defined (back
2295 jump) and is reachable with a short jump. Otherwise the macro expands to a
2296 conditional branch with the branch condition inverted, followed by an
2297 absolute jump to the actual branch target.
2299 The package defines the following macros:
2302 jeq, jne, jmi, jpl, jcs, jcc, jvs, jvc
2308 <sect>Bugs/Feedback<p>
2310 If you have problems using the assembler, if you find any bugs, or if
2311 you're doing something interesting with the assembler, I would be glad to
2312 hear from you. Feel free to contact me by email
2313 (<htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">).
2319 ca65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
2320 Bassewitz. For usage of the binaries and/or sources the following
2321 conditions do apply:
2323 This software is provided 'as-is', without any expressed or implied
2324 warranty. In no event will the authors be held liable for any damages
2325 arising from the use of this software.
2327 Permission is granted to anyone to use this software for any purpose,
2328 including commercial applications, and to alter it and redistribute it
2329 freely, subject to the following restrictions:
2332 <item> The origin of this software must not be misrepresented; you must not
2333 claim that you wrote the original software. If you use this software
2334 in a product, an acknowledgment in the product documentation would be
2335 appreciated but is not required.
2336 <item> Altered source versions must be plainly marked as such, and must not
2337 be misrepresented as being the original software.
2338 <item> This notice may not be removed or altered from any source