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
87 -t sys Set the target system
89 -D name[=value] Define a symbol
90 -I dir Set an include directory search path
91 -U Mark unresolved symbols as import
92 -V Print the assembler version
93 -W n Set warning level n
96 --auto-import Mark unresolved symbols as import
97 --cpu type Set cpu type
98 --debug-info Add debug info to object file
99 --feature name Set an emulation feature
100 --help Help (this text)
101 --ignore-case Ignore case of symbols
102 --include-dir dir Set an include directory search path
103 --listing Create a listing if assembly was ok
104 --pagelength n Set the page length for the listing
105 --smart Enable smart mode
106 --target sys Set the target system
107 --verbose Increase verbosity
108 --version Print the assembler version
109 ---------------------------------------------------------------------------
112 Here is a description of all the command line options:
116 <tag><tt>--cpu type</tt></tag>
118 Set the default for the CPU type. The option takes a parameter, which
121 6502, 65C02, 65816 and sunplus
123 (the latter is not available in the freeware version).
126 <tag><tt>--feature name</tt></tag>
128 Enable an emulation feature. This is identical as using <tt/.FEATURE/
129 in the source with two exceptions: Feature names must be lower case, and
130 each feature must be specified by using an extra <tt/--feature/ option,
131 comma separated lists are not allowed.
133 See the discussion of the <tt/.FEATURE/ command for a list of emulation
137 <tag><tt>-g, --debug-info</tt></tag>
139 When this option (or the equivalent control command <tt/.DEBUGINFO/) is
140 used, the assembler will add a section to the object file that contains
141 all symbols (including local ones) together with the symbol values and
142 source file positions. The linker will put these additional symbols into
143 the VICE label file, so even local symbols can be seen in the VICE
147 <tag><tt>-h, --help</tt></tag>
149 Print the short option summary shown above.
152 <tag><tt>-i, --ignore-case</tt></tag>
154 This option makes the assembler case insensitive on identifiers and
155 labels. This option will override the default, but may itself be
156 overriden by the <tt/.CASE/ control command (see section 6).
159 <tag><tt>-l, --listing</tt></tag>
161 Generate an assembler listing. The listing file will always have the
162 name of the main input file with the extension replaced by ".lst". This
163 may change in future versions.
166 <tag><tt>-o name</tt></tag>
168 The default output name is the name of the input file with the extension
169 replaced by ".o". If you don't like that, you may give another name with
170 the -o option. The output file will be placed in the same directory as
171 the source file, or, if -o is given, the full path in this name is used.
174 <tag><tt>--pagelength n</tt></tag>
176 sets the length of a listing page in lines. See the <tt/.PAGELENGTH/
177 directive for more information.
180 <tag><tt>-s, --smart-mode</tt></tag>
182 In smart mode (enabled by -s or the <tt/.SMART/ pseudo instruction) the
183 assembler will track usage of the REP and SEP instructions in 65816 mode
184 and update the operand sizes accordingly. If the operand of such an
185 instruction cannot be evaluated by the assembler (for example, because
186 the operand is an imported symbol), a warning is issued.
188 Beware: Since the assembler cannot trace the execution flow this may
189 lead to false results in some cases. If in doubt, use the .ixx and .axx
190 instructions to tell the assembler about the current settings. Smart
191 mode is off by default.
194 <tag><tt>-t sys, --target sys</tt></tag>
196 Set the target system. This will enable translation of character strings
197 and character constants into the character set of the target platform.
198 The default for the target system is "none", which means that no translation
199 will take place. The assembler supports the same target systems as the
200 compiler, see there for a list.
203 <tag><tt>-v, --verbose</tt></tag>
205 Increase the assembler verbosity. Usually only needed for debugging
206 purposes. You may use this option more than one time for even more
210 <tag><tt>-D</tt></tag>
212 This option allows you to define symbols on the command line. Without a
213 value, the symbol is defined with the value zero. When giving a value,
214 you may use the '$' prefix for hexadecimal symbols. Please note
215 that for some operating systems, '$' has a special meaning, so
216 you may have to quote the expression.
219 <tag><tt>-I dir, --include-dir dir</tt></tag>
221 Name a directory which is searched for include files. The option may be
222 used more than once to specify more than one directory to search. The
223 current directory is always searched first before considering any
224 additional directores.
227 <tag><tt>-U, --auto-import</tt></tag>
229 Mark symbols that are not defined in the sources as imported symbols.
230 This should be used with care since it delays error messages about typos
231 and such until the linker is run. The compiler uses the equivalent of
232 this switch (<tt/.AUTOIMPORT/, see control command section below) to enable
233 auto imported symbols for the runtime library. However, the compiler is
234 supposed to generate code that runs through the assembler without
235 problems, something which is not always true for assembler programmers.
238 <tag><tt>-V, --version</tt></tag>
240 Print the version number of the assembler. If you send any suggestions
241 or bugfixes, please include the version number.
244 <tag><tt>-Wn</tt></tag>
246 Set the warning level for the assembler. Using -W2 the assembler will
247 even warn about such things like unused imported symbols. The default
248 warning level is 1, and it would probably be silly to set it to
255 <sect>Input format<p>
257 The assembler accepts the standard 6502/65816 assembler syntax. One line
258 may contain a label (which is identified by a colon), and, in addition to
259 the label, an assembler mnemonic, a macro, or a control command (see
260 section 6 for supported control commands). Alternatively, the line may
261 contain a symbol definition using the '=' token. Everything after a
262 semicolon is handled as a comment (that is, it is ignored).
264 Here are some examples for valid input lines:
267 Label: ; A label and a comment
268 lda #$20 ; A 6502 instruction plus comment
269 L1: ldx #$20 ; Same with label
270 L2: .byte "Hello world" ; Label plus control command
271 mymac $20 ; Macro expansion
272 MySym = 3*L1 ; Symbol definition
273 MaSym = Label ; Another symbol
276 The assembler accepts all valid 6502 mnemonics when in 6502 mode (the
277 default). The assembler accepts all valid 65SC02 mnemonics when in 65SC02
278 mode (after a <tt/.PC02/ command is found). The assembler accepts all valid
279 65816 mnemonics with a few exceptions after a .P816 command is found.
280 These exceptions are listed below.
282 In 65816 mode several aliases are accepted in addition to the official
286 BGE is an alias for BCS
287 BLT is an alias for BCC
288 CPA is an alias for CMP
289 DEA is an alias for DEC A
290 INA is an alias for INC A
291 SWA is an alias for XBA
292 TAD is an alias for TCD
293 TAS is an alias for TCS
294 TDA is an alias for TDC
295 TSA is an alias for TSC
298 Evaluation of banked expressions in 65816 mode differs slightly from the
301 Instead of accepting a 24 bit address (something that is difficult for
302 the assembler to determine and would have required one more special
303 .import command), the bank and the absolute address in that bank are
307 jsl 3.$1234 ; Call subroutine at $1234 in bank 3
310 For literal values, the assembler accepts the widely used number formats:
311 A preceeding '$' denotes a hex value, a preceeding '%' denotes a
312 binary value, and a bare number is interpeted as a decimal. There are
313 currently no octal values and no floats.
320 All expressions are evaluated with (at least) 32 bit precision. An
321 expression may contain constant values and any combination of internal and
322 external symbols. Expressions that cannot be evaluated at assembly time
323 are stored inside the object file for evaluation by the linker.
324 Expressions referencing imported symbols must always be evaluated by the
327 Sometimes, the assembler must know about the size of the value that is the
328 result of an expression. This is usually the case, if a decision has to be
329 made, to generate a zero page or an absolute memory references. In this
330 case, the assembler has to make some assumptions about the result of an
334 <item> If the result of an expression is constant, the actual value is
335 checked to see if it's a byte sized expression or not.
336 <item> If the expression is explicitly casted to a byte sized expression by
337 one of the '>'/'<' operators, it is a byte expression.
338 <item> If this is not the case, and the expression contains a symbol,
339 explicitly declared as zero page symbol (by one of the .importzp or
340 .exportzp instructions), then the whole expression is assumed to be
342 <item> If the expression contains symbols that are not defined, and these
343 symbols are local symbols, the enclosing scopes are searched for a
344 symbol with the same name. If one exists and this symbol is defined,
345 it's attributes are used to determine the result size.
346 <item> In all other cases the expression is assumed to be word sized.
349 Note: If the assembler is not able to evaluate the expression at assembly
350 time, the linker will evaluate it and check for range errors as soon as
354 <bf>Boolean expressions:</bf>
356 In the context of a boolean expression, any non zero value is evaluated as
357 true, any other value to false. The result of a boolean expression is 1 if
358 it's true, and zero if it's false. There are boolean operators with extrem
359 low precedence with version 2.x (where x > 0). The <tt/.AND/ and <tt/.OR/
360 operators are shortcut operators. That is, if the result of the expression is
361 already known, after evaluating the left hand side, the right hand side is
365 Available operators sorted by precedence:
368 Op Description Precedence
369 -------------------------------------------------------------------
370 .CONCAT Builtin function 0
371 .LEFT Builtin function 0
372 .MID Builtin function 0
373 .RIGHT Builtin function 0
374 .STRING Builtin function 0
376 * Builtin pseudo variable (r/o) 1
377 .BLANK Builtin function 1
378 .CONST Builtin function 1
379 .CPU Builtin pseudo variable (r/o) 1
380 .DEFINED Builtin function 1
381 .MATCH Builtin function 1
382 .TCOUNT Builtin function 1
383 .XMATCH Builtin function 1
384 .PARAMCOUNT Builtin pseudo variable (r/o) 1
385 .REFERENCED Builtin function 1
386 :: Global namespace override 1
389 ~ Unary bitwise not 1
390 .BITNOT Unary bitwise not 1
391 < Low byte operator 1
392 > High byte operator 1
396 .MOD Modulo operation 2
398 .BITAND Bitwise and 2
400 .BITXOR Bitwise xor 2
401 << Shift left operator 2
402 .SHL Shift left operator 2
403 >> Shift right operator 2
404 .SHR Shift right operator 2
411 = Compare operation (equal) 4
412 <> Compare operation (not equal) 4
413 < Compare operation (less) 4
414 > Compare operation (greater) 4
415 <= Compare operation (less or equal) 4
416 >= Compare operation (greater or equal) 4
418 && Boolean and 5
430 To force a specific order of evaluation, braces may be used as usual.
432 Some of the pseudo variables mentioned above need some more explanation:
434 * This symbol is replaced by the value of the program
435 counter at start of the current instruction. Note, that
436 '*' yields a rvalue, that means, you cannot assign to it.
437 Use <tt/.ORG/ to set the program counter in sections with
442 <sect>Symbols and labels
445 The assembler allows you to use symbols instead of naked values to make
446 the source more readable. There are a lot of different ways to define and
447 use symbols and labels, giving a lot of flexibility.
452 <tag/Numeric constants/
454 Numeric constants are defined using the equal sign. After doing
460 may use the symbol "two" in every place where a number is expected,
461 and it is evaluated to the value 2 in this context. An example would be
468 <tag/Standard labels/
470 A label is defined by writing the name of the label at the start of
471 the line (before any instruction mnemonic, macro or pseudo
472 directive), followed by a colon. This will declare a symbol with the
473 given name and the value of the current program counter.
476 <tag/Local labels and symbols/
478 Using the <tt/.PROC/ directive, it is possible to create regions of code
479 where the names of labels and symbols are local to this region. They
480 are not know outside and cannot be accessed from there. Such regions
481 may be nested like PROCEDUREs in Pascal.
483 See the description of the <tt/.PROC/ directive for more information.
485 <tag/Cheap local labels/
487 Cheap local labels are defined like standard labels, but the name of
488 the label must begin with a special symbol (usually '@', but this can
489 be changed by the <tt/.LOCALCHAR/ directive).
491 Cheap local labels are visible only between two no cheap labels. As
492 soon as a standard symbol is encountered (this may also be a local
493 symbol if inside a region defined with the .PROC directive), the
494 cheap local symbol goes out of scope.
496 You may use cheap local labels as an easy way to reuse common label
497 names like "Loop". Here is an example:
500 Clear: lda #$00 ; Global label
502 @Loop: sta Mem,y ; Local label
506 Sub: ... ; New global label
507 bne @Loop ; ERROR: Unknown identifier!
512 If you really want to write messy code, there are also unnamed
513 labels. These labels do not have a name (you guessed that already,
514 didn't you?). A colon is used to mark the absence of the name.
516 Unnamed labels may be accessed by using the colon plus several minus
517 or plus characters as a label designator. Using the '-' characters
518 will create a back reference (use the n'th label backwards), using
519 '+' will create a forward reference (use the n'th label in forward
520 direction). An example will help to understand this:
542 As you can see from the example, unnamed labels will make even short
543 sections of code hard to understand, because you have to count labels
544 to find branch targets (this is the reason why I for my part do
545 prefer the "cheap" local labels). Nevertheless, unnamed labels are
546 convenient in some situations, so it's your decision.
548 <tag/Using macros to define labels and constants/
550 While there are drawbacks with this approach, it may be handy in some
551 situations. Using <tt/.DEFINE/, it is possible to define symbols or
552 constants that may be used elsewhere. Since the macro facility works
553 on a very low level, there is no scoping. On the other side, you may
554 also define string constants this way (this is not possible with the
561 .DEFINE version "SOS V2.3"
563 four = two * two ; Ok
566 .PROC ; Start local scope
567 two = 3 ; Will give "2 = 3" - invalid!
574 If <tt/.DEBUGINFO/ is enabled (or -g is given on the command line), global,
575 local and cheap local labels are written to the object file and will be
576 available in the symbol file via the linker. Unnamed labels are not
577 written to the object file, because they don't have a name which would
578 allow to access them.
582 <sect>Control commands
585 Here's a list of all control commands and a description, what they do:
589 <tag><tt><idx>.A16</idx></tt></tag>
591 Valid only in 65816 mode. Switch the accumulator to 16 bit.
593 Note: This command will not emit any code, it will tell the assembler to
594 create 16 bit operands for immediate accumulator adressing mode.
596 See also: <tt/.SMART/
599 <tag><tt><idx>.A8</idx></tt></tag>
601 Valid only in 65816 mode. Switch the accumulator to 8 bit.
603 Note: This command will not emit any code, it will tell the assembler to
604 create 8 bit operands for immediate accu adressing mode.
606 See also: <tt/.SMART/
609 <tag><tt><idx>.ADDR</idx></tt></tag>
611 Define word sized data. In 6502 mode, this is an alias for <tt/.WORD/ and
612 may be used for better readability if the data words are address values.
613 In 65816 mode, the address is forced to be 16 bit wide to fit into the
614 current segment. See also <tt/.FARADDR/. The command must be followed by a
615 sequence of (not necessarily constant) expressions.
620 .addr $0D00, $AF13, _Clear
624 <tag><tt><idx>.ALIGN</idx></tt></tag>
626 Align data to a given boundary. The command expects a constant integer
627 argument that must be a power of two, plus an optional second argument
628 in byte range. If there is a second argument, it is used as fill value,
629 otherwise the value defined in the linker configuration file is used
630 (the default for this value is zero).
632 Since alignment depends on the base address of the module, you must
633 give the same (or a greater) alignment for the segment when linking.
634 The linker will give you a warning, if you don't do that.
643 <tag><tt><idx>.ASCIIZ</idx></tt></tag>
645 Define a string with a trailing zero.
650 Msg: .asciiz "Hello world"
653 This will put the string "Hello world" followed by a binary zero into
654 the current segment. There may be more strings separated by commas, but
655 the binary zero is only appended once (after the last one).
658 <tag><tt><idx>.AUTOIMPORT</idx></tt></tag>
660 Is followd by a plus or a minus character. When switched on (using a
661 +), undefined symbols are automatically marked as import instead of
662 giving errors. When switched off (which is the default so this does not
663 make much sense), this does not happen and an error message is
664 displayed. The state of the autoimport flag is evaluated when the
665 complete source was translated, before outputing actual code, so it is
666 <em/not/ possible to switch this feature on or off for separate sections
667 of code. The last setting is used for all symbols.
669 You should probably not use this switch because it delays error
670 messages about undefined symbols until the link stage. The cc65
671 compiler (which is supposed to produce correct assembler code in all
672 circumstances, something which is not true for most assembler
673 programmers) will insert this command to avoid importing each and every
674 routine from the runtime library.
679 .autoimport + ; Switch on auto import
683 <tag><tt><idx>.BLANK</idx></tt></tag>
685 Builtin function. The function evaluates its argument in braces and
686 yields "false" if the argument is non blank (there is an argument), and
687 "true" if there is no argument. As an example, the <tt/.IFBLANK/ statement
695 <tag><tt><idx>.BSS</idx></tt></tag>
697 Switch to the BSS segment. The name of the BSS segment is always "BSS",
698 so this is a shortcut for
704 See also the <tt/.SEGMENT/ command.
707 <tag><tt><idx>.BYTE</idx></tt></tag>
709 Define byte sized data. Must be followed by a sequence of (byte ranged)
710 expressions or strings.
715 .byte "Hello world", $0D, $00
719 <tag><tt><idx>.CASE</idx></tt></tag>
721 Switch on or off case sensitivity on identifiers. The default is off
722 (that is, identifiers are case sensitive), but may be changed by the
723 -i switch on the command line.
724 The command must be followed by a '+' or '-' character to switch the
725 option on or off respectively.
730 .case - ; Identifiers are not case sensitive
734 <tag><tt><idx>.CODE</idx></tt></tag>
736 Switch to the CODE segment. The name of the CODE segment is always
737 "CODE", so this is a shortcut for
743 See also the <tt/.SEGMENT/ command.
746 <tag><tt><idx>.CONCAT</idx></tt></tag>
748 Builtin function. The function allows to concatenate a list of string
749 constants separated by commas. The result is a string constant that
750 is the concatentation of all arguments. This function is most useful
751 in macros and when used together with the <tt/.STRING/ builtin function.
752 The function may be used in any case where a string constant is
758 .include .concat ("myheader", ".", "inc)
761 This is the same as the command
764 .include "myheader.inc"
768 <tag><tt><idx>.CONST</idx></tt></tag>
770 Builtin function. The function evaluates its argument in braces and
771 yields "true" if the argument is a constant expression (that is, an
772 expression that yields a constant value at assembly time) and "false"
773 otherwise. As an example, the .IFCONST statement may be replaced by
780 <tag><tt><idx>.CPU</idx></tt></tag>
782 Reading this pseudo variable will give a constant integer value that
783 tells which instruction set is currently enabled. Possible values are:
792 It may be used to replace the .IFPxx pseudo instructions or to construct
793 even more complex expressions.
798 .if (.cpu = 0) .or (.cpu = 1)
810 <tag><tt><idx>.DATA</idx></tt></tag>
812 Switch to the DATA segment. The name of the DATA segment is always
813 "DATA", so this is a shortcut for
819 See also the <tt/.SEGMENT/ command.
822 <tag><tt><idx>.DBYT</idx></tt></tag>
824 Define word sized data with the hi and lo bytes swapped (use <tt/.WORD/ to
825 create word sized data in native 65XX format). Must be followed by a
826 sequence of (word ranged) expressions.
834 This will emit the bytes
840 into the current segment in that order.
843 <tag><tt><idx>.DEBUGINFO</idx></tt></tag>
845 Switch on or off debug info generation. The default is off (that is,
846 the object file will not contain debug infos), but may be changed by the
847 -g switch on the command line.
848 The command must be followed by a '+' or '-' character to switch the
849 option on or off respectively.
854 .debuginfo + ; Generate debug info
858 <tag><tt><idx>.DEFINE</idx></tt></tag>
860 Start a define style macro definition. The command is followed by an
861 identifier (the macro name) and optionally by a list of formal arguments
863 See separate section about macros.
866 <tag><tt><idx>.DEF,</idx> <idx>.DEFINED</idx></tt></tag>
868 Builtin function. The function expects an identifier as argument in
869 braces. The argument is evaluated, and the function yields "true" if the
870 identifier is a symbol that is already defined somewhere in the source
871 file up to the current position. Otherwise the function yields false. As
872 an example, the <tt/.IFDEF/ statement may be replaced by
879 <tag><tt><idx>.DWORD</idx></tt></tag>
881 Define dword sized data (4 bytes) Must be followed by a sequence of
887 .dword $12344512, $12FA489
891 <tag><tt><idx>.ELSE</idx></tt></tag>
893 Conditional assembly: Reverse the current condition.
896 <tag><tt><idx>.ELSEIF</idx></tt></tag>
898 Conditional assembly: Reverse current condition and test a new one.
901 <tag><tt><idx>.END</idx></tt></tag>
903 Forced end of assembly. Assembly stops at this point, even if the command
904 is read from an include file.
907 <tag><tt><idx>.ENDIF</idx></tt></tag>
909 Conditional assembly: Close a <tt/.IF.../ or <tt/.ELSE/ branch.
912 <tag><tt><idx>.ENDMAC,</idx> <idx>.ENDMACRO</idx></tt></tag>
914 End of macro definition (see separate section).
917 <tag><tt><idx>.ENDPROC</idx></tt></tag>
919 End of local lexical level (see <tt/.PROC/).
922 <tag><tt><idx>.ENDREP,</idx> <idx>.ENDREPEAT</idx></tt></tag>
924 End a <tt/.REPEAT/ block. See the <tt/.REPEAT/ command.
927 <tag><tt><idx>.ERROR</idx></tt></tag>
929 Force an assembly error. The assembler will output an error message
930 preceeded by "User error" and will <em/not/ produce an object file.
932 This command may be used to check for initial conditions that must be
933 set before assembling a source file.
943 .error "Must define foo or bar!"
947 See also the <tt/.WARNING/ and <tt/.OUT/ directives.
950 <tag><tt><idx>.EXITMAC,</idx> <idx>.EXITMACRO</idx></tt></tag>
952 Abort a macro expansion immidiately. This command is often useful in
953 recursive macros. See separate chapter about macros.
956 <tag><tt><idx>.EXPORT</idx></tt></tag>
958 Make symbols accessible from other modules. Must be followed by a comma
959 separated list of symbols to export.
968 <tag><tt><idx>.EXPORTZP</idx></tt></tag>
970 Make symbols accessible from other modules. Must be followed by a comma
971 separated list of symbols to export. The exported symbols are explicitly
972 marked as zero page symols.
981 <tag><tt><idx>.FARADDR</idx></tt></tag>
983 Define far (24 bit) address data. The command must be followed by a
984 sequence of (not necessarily constant) expressions.
989 .faraddr DrawCircle, DrawRectangle, DrawHexagon
993 <tag><tt><idx>.FEATURE</idx></tt></tag>
995 This directive may be used to enable one or more compatibility features
996 of the assembler. While the use of <tt/.FEATURE/ should be avoided when
997 possible, it may be useful when porting sources written for other
998 assemblers. There is no way to switch a feature off, once you have
1005 will enable the feature until end of assembly is reached.
1007 The following features are available:
1011 <tag><tt><idx>dollar_is_pc</idx></tt></tag>
1013 The dollar sign may be used as an alias for the star (`*'), which
1014 gives the value of the current PC in expressions.
1015 Note: Assignment to the pseudo variable is not allowed.
1017 <tag><tt><idx>labels_without_colons</idx></tt></tag>
1019 Allow labels without a trailing colon. These labels are only accepted,
1020 if they start at the beginning of a line (no leading white space).
1022 <tag><tt><idx>loose_string_term</idx></tt></tag>
1024 Accept single quotes as well as double quotes as terminators for string
1027 <tag><tt><idx>at_in_identifiers</idx></tt></tag>
1029 Accept the at character (`@') as a valid character in identifiers. The
1030 at character is not allowed to start an identifier, even with this
1033 <tag><tt><idx>dollar_in_identifiers</idx></tt></tag>
1035 Accept the dollar sign (`$') as a valid character in identifiers. The
1036 at character is not allowed to start an identifier, even with this
1039 <tag><tt><idx>pc_assignment</idx></tt></tag>
1041 Allow assignments to the PC symbol (`*' or `$' if dollar_is_pc is
1042 enabled). Such an assignment is handled identical to the <tt/.ORG/
1043 command (which is usually not needed, so just removing the lines with
1044 the assignments may also be an option when porting code written for older
1050 <tag><tt><idx>.FILEOPT,</idx> <idx>.FOPT</idx></tt></tag>
1052 Insert an option string into the object file. There are two forms of
1053 this command, one specifies the option by a keyword, the second
1054 specifies it as a number. Since usage of the second one needs knowledge
1055 of the internal encoding, its use is not recommended and I will only
1056 describe the first form here.
1058 The command is followed by one of the keywords
1066 a comma and a string. The option is written into the object file
1067 together with the string value. This is currently unidirectional and
1068 there is no way to actually use these options once they are in the
1074 .fileopt comment, "Code stolen from my brother"
1075 .fileopt compiler, "BASIC 2.0"
1076 .fopt author, "J. R. User"
1080 <tag><tt><idx>.GLOBAL</idx></tt></tag>
1082 Declare symbols as global. Must be followed by a comma separated list
1083 of symbols to declare. Symbols from the list, that are defined somewhere
1084 in the source, are exported, all others are imported. An additional
1085 explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same symbol is
1095 <tag><tt><idx>.GLOBALZP</idx></tt></tag>
1097 Declare symbols as global. Must be followed by a comma separated list
1098 of symbols to declare. Symbols from the list, that are defined
1099 somewhere in the source, are exported, all others are imported. An
1100 additional explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same
1101 symbol is explicitly allowed. The symbols in the list are explicitly
1102 marked as zero page symols.
1111 <tag><tt><idx>.I16</idx></tt></tag>
1113 Valid only in 65816 mode. Switch the index registers to 16 bit.
1115 Note: This command will not emit any code, it will tell the assembler to
1116 create 16 bit operands for immediate operands.
1118 See also the <tt/.SMART/ command.
1121 <tag><tt><idx>.I8</idx></tt></tag>
1123 Valid only in 65816 mode. Switch the index registers to 8 bit.
1125 Note: This command will not emit any code, it will tell the assembler to
1126 create 8 bit operands for immediate operands.
1128 See also the <tt/.SMART/ command.
1131 <tag><tt><idx>.IF</idx></tt></tag>
1133 Conditional assembly: Evalute an expression and switch assembler output
1134 on or off depending on the expression. The expression must be a constant
1135 expression, that is, all operands must be defined.
1137 A expression value of zero evaluates to FALSE, any other value evaluates
1141 <tag><tt><idx>.IFBLANK</idx></tt></tag>
1143 Conditional assembly: Check if there are any remaining tokens in this
1144 line, and evaluate to FALSE if this is the case, and to TRUE otherwise.
1145 If the condition is not true, further lines are not assembled until
1146 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1148 This command is often used to check if a macro parameter was given.
1149 Since an empty macro parameter will evaluate to nothing, the condition
1150 will evaluate to FALSE if an empty parameter was given.
1164 See also: <tt/.BLANK/
1167 <tag><tt><idx>.IFCONST</idx></tt></tag>
1169 Conditional assembly: Evaluate an expression and switch assembler output
1170 on or off depending on the constness of the expression.
1172 A const expression evaluates to to TRUE, a non const expression (one
1173 containing an imported or currently undefined symbol) evaluates to
1176 See also: <tt/.CONST/
1179 <tag><tt><idx>.IFDEF</idx></tt></tag>
1181 Conditional assembly: Check if a symbol is defined. Must be followed by
1182 a symbol name. The condition is true if the the given symbol is already
1183 defined, and false otherwise.
1185 See also: <tt/.DEFINED/
1188 <tag><tt><idx>.IFNBLANK</idx></tt></tag>
1190 Conditional assembly: Check if there are any remaining tokens in this
1191 line, and evaluate to TRUE if this is the case, and to FALSE otherwise.
1192 If the condition is not true, further lines are not assembled until
1193 an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1195 This command is often used to check if a macro parameter was given.
1196 Since an empty macro parameter will evaluate to nothing, the condition
1197 will evaluate to FALSE if an empty parameter was given.
1210 See also: <tt/.BLANK/
1213 <tag><tt><idx>.IFNDEF</idx></tt></tag>
1215 Conditional assembly: Check if a symbol is defined. Must be followed by
1216 a symbol name. The condition is true if the the given symbol is not
1217 defined, and false otherwise.
1219 See also: <tt/.DEFINED/
1222 <tag><tt><idx>.IFNREF</idx></tt></tag>
1224 Conditional assembly: Check if a symbol is referenced. Must be followed
1225 by a symbol name. The condition is true if if the the given symbol was
1226 not referenced before, and false otherwise.
1228 See also: <tt/.REFERENCED/
1231 <tag><tt><idx>.IFP02</idx></tt></tag>
1233 Conditional assembly: Check if the assembler is currently in 6502 mode
1234 (see <tt/.P02/ command).
1237 <tag><tt><idx>.IFP816</idx></tt></tag>
1239 Conditional assembly: Check if the assembler is currently in 65816 mode
1240 (see <tt/.P816/ command).
1243 <tag><tt><idx>.IFPC02</idx></tt></tag>
1245 Conditional assembly: Check if the assembler is currently in 65C02 mode
1246 (see <tt/.PC02/ command).
1249 <tag><tt><idx>.IFREF</idx></tt></tag>
1251 Conditional assembly: Check if a symbol is referenced. Must be followed
1252 by a symbol name. The condition is true if if the the given symbol was
1253 referenced before, and false otherwise.
1255 This command may be used to build subroutine libraries in include files
1256 (you may use separate object modules for this purpose too).
1261 .ifref ToHex ; If someone used this subroutine
1262 ToHex: tay ; Define subroutine
1268 See also: <tt/.REFERENCED/
1271 <tag><tt><idx>.IMPORT</idx></tt></tag>
1273 Import a symbol from another module. The command is followed by a comma
1274 separated list of symbols to import.
1283 <tag><tt><idx>.IMPORTZP</idx></tt></tag>
1285 Import a symbol from another module. The command is followed by a comma
1286 separated list of symbols to import. The symbols are explicitly imported
1287 as zero page symbols (that is, symbols with values in byte range).
1296 <tag><tt><idx>.INCBIN</idx></tt></tag>
1298 Include a file as binary data. The command expects a string argument
1299 that is the name of a file to include literally in the current segment.
1304 .incbin "sprites.dat"
1308 <tag><tt><idx>.INCLUDE</idx></tt></tag>
1310 Include another file. Include files may be nested up to a depth of 16.
1319 <tag><tt><idx>.LEFT</idx></tt></tag>
1321 Builtin function. Extracts the left part of a given token list.
1326 .LEFT (<int expr>, <token list>)
1329 The first integer expression gives the number of tokens to extract from
1330 the token list. The second argument is the token list itself.
1334 To check in a macro if the given argument has a '#' as first token
1335 (immidiate addressing mode), use something like this:
1340 .if (.match (.left (1, arg), #))
1342 ; ldax called with immidiate operand
1350 See also the <tt/.MID/ and <tt/.RIGHT/ builtin functions.
1353 <tag><tt><idx>.LINECONT</idx></tt></tag>
1355 Switch on or off line continuations using the backslash character
1356 before a newline. The option is off by default.
1357 Note: Line continuations do not work in a comment. A backslash at the
1358 end of a comment is treated as part of the comment and does not trigger
1360 The command must be followed by a '+' or '-' character to switch the
1361 option on or off respectively.
1366 .linecont + ; Allow line continuations
1369 #$20 ; This is legal now
1373 <tag><tt><idx>.LIST</idx></tt></tag>
1375 Enable output to the listing. The command must be followed by a boolean
1376 switch ("on", "off", "+" or "-") and will enable or disable listing
1378 The option has no effect if the listing is not enabled by the command line
1379 switch -l. If -l is used, an internal counter is set to 1. Lines are output
1380 to the listing file, if the counter is greater than zero, and suppressed if
1381 the counter is zero. Each use of <tt/.LIST/ will increment or decrement the
1387 .list on ; Enable listing output
1391 <tag><tt><idx>.LISTBYTES</idx></tt></tag>
1393 Set, how many bytes are shown in the listing for one source line. The
1394 default is 12, so the listing will show only the first 12 bytes for any
1395 source line that generates more than 12 bytes of code or data.
1396 The directive needs an argument, which is either "unlimited", or an
1397 integer constant in the range 4..255.
1402 .listbytes unlimited ; List all bytes
1403 .listbytes 12 ; List the first 12 bytes
1404 .incbin "data.bin" ; Include large binary file
1408 <tag><tt><idx>.LOCAL</idx></tt></tag>
1410 This command may only be used inside a macro definition. It declares a
1411 list of identifiers as local to the macro expansion.
1413 A problem when using macros are labels: Since they don't change their
1414 name, you get a "duplicate symbol" error if the macro is expanded the
1415 second time. Labels declared with <tt/.LOCAL/ have their name mapped to
1416 an internal unique name (<tt/___ABCD__/) with each macro invocation.
1418 Some other assemblers start a new lexical block inside a macro
1419 expansion. This has some drawbacks however, since that will not allow
1420 <em/any/ symbol to be visible outside a macro, a feature that is sometimes
1421 useful. The <tt/.LOCAL/ command is in my eyes a better way to address
1424 You get an error when using <tt/.LOCAL/ outside a macro.
1427 <tag><tt><idx>.LOCALCHAR</idx></tt></tag>
1429 Defines the character that start "cheap" local labels. You may use one
1430 of '@' and '?' as start character. The default is '@'.
1432 Cheap local labels are labels that are visible only between two non
1433 cheap labels. This way you can reuse identifiers like "<tt/loop/" without
1434 using explicit lexical nesting.
1441 Clear: lda #$00 ; Global label
1442 ?Loop: sta Mem,y ; Local label
1446 Sub: ... ; New global label
1447 bne ?Loop ; ERROR: Unknown identifier!
1451 <tag><tt><idx>.MACPACK</idx></tt></tag>
1453 Insert a predefined macro package. The command is followed by an
1454 identifier specifying the macro package to insert. Available macro
1457 generic Defines generic macros like add and sub.
1458 longbranch Defines conditional long jump macros.
1460 Including a macro package twice, or including a macro package that
1461 redefines already existing macros will lead to an error.
1466 .macpack longbranch ; Include macro package
1468 cmp #$20 ; Set condition codes
1469 jne Label ; Jump long on condition
1472 See separate section about macros packages.
1475 <tag><tt><idx>.MAC,</idx> <idx>.MACRO</idx></tt></tag>
1477 Start a classic macro definition. The command is followed by an identifier
1478 (the macro name) and optionally by a comma separated list of identifiers
1479 that are macro parameters.
1480 See separate section about macros.
1483 <tag><tt><idx>.MATCH</idx></tt></tag>
1485 Builtin function. Matches two token lists against each other. This is
1486 most useful within macros, since macros are not stored as strings, but
1492 .MATCH(<token list #1>, <token list #2>)
1495 Both token list may contain arbitrary tokens with the exception of the
1496 terminator token (comma resp. right parenthesis) and
1503 Often a macro parameter is used for any of the token lists.
1505 Please note that the function does only compare tokens, not token
1506 attributes. So any number is equal to any other number, regardless of
1507 the actual value. The same is true for strings. If you need to compare
1508 tokens <em/and/ token attributes, use the <tt/.XMATCH/ function.
1512 Assume the macro <tt/ASR/, that will shift right the accumulator by one, while
1513 honoring the sign bit. The builtin processor instructions will allow an
1514 optional "A" for accu addressing for instructions like <tt/ROL/ and <tt/ROR/.
1515 We will use the <tt/.MATCH/ function to check for this and print and error
1521 .if (.not .blank(arg)) .and (.not .match (arg, a))
1522 .error "Syntax error"
1525 cmp #$80 ; Bit 7 into carry
1526 lsr a ; Shit carry into bit 7
1531 The macro will only accept no arguments, or one argument that must be the
1532 reserved keyword "A".
1535 <tag><tt><idx>.MID</idx></tt></tag>
1537 Builtin function. Takes a starting index, a count and a token list as
1538 arguments. Will return part of the token list.
1543 .MID (<int expr>, <int expr>, <token list>)
1546 The first integer expression gives the starting token in the list (the
1547 first token has index 0). The second integer expression gives the number
1548 of tokens to extract from the token list. The third argument is the
1553 To check in a macro if the given argument has a '<tt/#/' as first token
1554 (immidiate addressing mode), use something like this:
1559 .if (.match (.mid (0, 1, arg), #))
1561 ; ldax called with immidiate operand
1569 See also the <tt/.LEFT/ and <tt/.RIGHT/ builtin functions.
1572 <tag><tt><idx>.ORG</idx></tt></tag>
1574 Start a section of absolute code. The command is followed by a constant
1575 expression that gives the new PC counter location for which the code is
1576 assembled. Use <tt/.RELOC/ to switch back to relocatable code.
1578 You may not switch segments while inside a section of absolute code.
1583 .org $7FF ; Emit code starting at $7FF
1587 <tag><tt><idx>.OUT</idx></tt></tag>
1589 Output a string to the console without producing an error. This command
1590 is similiar to <tt/.ERROR/, however, it does not force an assembler error
1591 that prevents the creation of an object file.
1596 .out "This code was written by the codebuster(tm)"
1599 See also the <tt/.WARNING/ and <tt/.ERROR/ directives.
1602 <tag><tt><idx>.P02</idx></tt></tag>
1604 Enable the 6502 instruction set, disable 65C02 and 65816 instructions.
1605 This is the default if not overridden by the <tt/--cpu/ command line
1609 <tag><tt><idx>.P816</idx></tt></tag>
1611 Enable the 65816 instruction set. This is a superset of the 65C02 and
1612 6502 instruction sets.
1615 <tag><tt><idx>.PAGELEN,</idx> <idx>.PAGELENGTH</idx></tt></tag>
1617 Set the page length for the listing. Must be followed by an integer
1618 constant. The value may be "unlimited", or in the range 32 to 127. The
1619 statement has no effect if no listing is generated. The default value
1620 is -1 but may be overridden by the <tt/--pagelength/ command line option.
1621 Beware: Since the listing is generated after assembly is complete, you
1622 cannot use multiple line lengths with one source. Instead, the value
1623 set with the last <tt/.PAGELENGTH/ is used.
1628 .pagelength 66 ; Use 66 lines per listing page
1630 .pagelength unlimited ; Unlimited page length
1634 <tag><tt><idx>.PARAMCOUNT</idx></tt></tag>
1636 This builtin pseudo variable is only available in macros. It is replaced
1637 by the actual number of parameters that were given in the macro
1643 .macro foo arg1, arg2, arg3
1644 .if .paramcount <> 3
1645 .error "Too few parameters for macro foo"
1652 <tag><tt><idx>.PC02</idx></tt></tag>
1654 Enable the 65C02 instructions set. This instruction set includes all
1658 <tag><tt><idx>.PROC</idx></tt></tag>
1660 Start a nested lexical level. All new symbols from now on are in the
1661 local lexical level and are not accessible from outside. Symbols defined
1662 outside this local level may be accessed as long as their names are not
1663 used for new symbols inside the level. Symbols names in other lexical
1664 levels do not clash, so you may use the same names for identifiers. The
1665 lexical level ends when the <tt/.ENDPROC/ command is read. Lexical levels
1666 may be nested up to a depth of 16.
1668 The command may be followed by an identifier, in this case the
1669 identifier is declared in the outer level as a label having the value of
1670 the program counter at the start of the lexical level.
1672 Note: Macro names are always in the global level and in a separate name
1673 space. There is no special reason for this, it's just that I've never
1674 had any need for local macro definitions.
1679 .proc Clear ; Define Clear subroutine, start new level
1681 L1: sta Mem,y ; L1 is local and does not cause a
1682 ; duplicate symbol error if used in other
1685 bne L1 ; Reference local symbol
1687 .endproc ; Leave lexical level
1691 <tag><tt><idx>.REF,</idx> <idx>.REFERENCED</idx></tt></tag>
1693 Builtin function. The function expects an identifier as argument in
1694 braces. The argument is evaluated, and the function yields "true" if the
1695 identifier is a symbol that has already been referenced somewhere in the
1696 source file up to the current position. Otherwise the function yields
1697 false. As an example, the <tt/.IFREF/ statement may be replaced by
1704 <tag><tt><idx>.REPEAT</idx></tt></tag>
1706 Repeat all commands between <tt/.REPEAT/ and <tt/.ENDREPEAT/ a constant
1707 number of times. The command is followed by a constant expression that tells
1708 how many times the commands in the body should get repeated. Optionally, a
1709 comma and an identifier may be specified. If this identifier is found in
1710 the body of the repeat statement, it is replaced by the current repeat
1711 count (starting with zero for the first time the body is repeated).
1713 <tt/.REPEAT/ statements may be nested. If you use the same repeat count
1714 identifier for a nested <tt/.REPEAT/ statement, the one from the inner
1715 level will be used, not the one from the outer level.
1719 The following macro will emit a string that is "encrypted" in that all
1720 characters of the string are XORed by the value $55.
1724 .repeat strlen(Arg), I
1725 .byte strat(Arg, I) .xor $55
1731 <tag><tt><idx>.RELOC</idx></tt></tag>
1733 Switch back to relocatable mode. See the <tt/.ORG/ command.
1736 <tag><tt><idx>.RES</idx></tt></tag>
1738 Reserve storage. The command is followed by one or two constant
1739 expressions. The first one is mandatory and defines, how many bytes of
1740 storage should be defined. The second, optional expression must by a
1741 constant byte value that will be used as value of the data. If there
1742 is no fill value given, the linker will use the value defined in the
1743 linker configuration file (default: zero).
1748 ; Reserve 12 bytes of memory with value $AA
1753 <tag><tt><idx>.RIGHT</idx></tt></tag>
1755 Builtin function. Extracts the right part of a given token list.
1760 .RIGHT (<int expr>, <token list>)
1763 The first integer expression gives the number of tokens to extract from
1764 the token list. The second argument is the token list itself.
1766 See also the <tt/.LEFT/ and <tt/.MID/ builtin functions.
1769 <tag><tt><idx>.RODATA</idx></tt></tag>
1771 Switch to the RODATA segment. The name of the RODATA segment is always
1772 "RODATA", so this is a shortcut for
1778 The RODATA segment is a segment that is used by the compiler for
1779 readonly data like string constants. See also the <tt/.SEGMENT/ command.
1782 <tag><tt><idx>.SEGMENT</idx></tt></tag>
1784 Switch to another segment. Code and data is always emitted into a
1785 segment, that is, a named section of data. The default segment is
1786 "CODE". There may be up to 254 different segments per object file
1787 (and up to 65534 per executable). There are shortcut commands for
1788 the most common segments ("CODE", "DATA" and "BSS").
1790 The command is followed by a string containing the segment name (there
1791 are some constraints for the name - as a rule of thumb use only those
1792 segment names that would also be valid identifiers). There may also be
1793 an optional attribute separated by a comma. Valid attributes are
1794 "<tt/zeropage/" and "<tt/absolute/".
1796 When specifying a segment for the first time, "absolute" is the
1797 default. For all other uses, the attribute specified the first time
1800 "absolute" means that this is a segment with absolute addressing. That
1801 is, the segment will reside somewhere in core memory outside the zero
1802 page. "zeropage" means the opposite: The segment will be placed in the
1803 zero page and direct (short) addressing is possible for data in this
1806 Beware: Only labels in a segment with the zeropage attribute are marked
1807 as reachable by short addressing. The `*' (PC counter) operator will
1808 work as in other segments and will create absolute variable values.
1813 .segment "ROM2" ; Switch to ROM2 segment
1814 .segment "ZP2", zeropage ; New direct segment
1815 .segment "ZP2" ; Ok, will use last attribute
1816 .segment "ZP2", absolute ; Error, redecl mismatch
1820 <tag><tt><idx>.SMART</idx></tt></tag>
1822 Switch on or off smart mode. The command must be followed by a '+' or
1823 '-' character to switch the option on or off respectively. The default
1824 is off (that is, the assembler doesn't try to be smart), but this
1825 default may be changed by the -s switch on the command line.
1827 In smart mode the assembler will track usage of the <tt/REP/ and <tt/SEP/
1828 instructions in 65816 mode and update the operand sizes accordingly. If
1829 the operand of such an instruction cannot be evaluated by the assembler
1830 (for example, because the operand is an imported symbol), a warning is
1831 issued. Beware: Since the assembler cannot trace the execution flow this
1832 may lead to false results in some cases. If in doubt, use the <tt/.Inn/ and
1833 <tt/.Ann/ instructions to tell the assembler about the current settings.
1839 .smart - ; Stop being smart
1843 <tag><tt><idx>.STRAT</idx></tt></tag>
1845 Builtin function. The function accepts a string and an index as
1846 arguments and returns the value of the character at the given position
1847 as an integer value. The index is zero based.
1853 ; Check if the argument string starts with '#'
1854 .if (.strat (Arg, 0) = '#')
1861 <tag><tt><idx>.STRING</idx></tt></tag>
1863 Builtin function. The function accepts an argument in braces and
1864 converts this argument into a string constant. The argument may be an
1865 identifier, or a constant numeric value.
1866 Since you can use a string in the first place, the use of the function
1867 may not be obvious. However, it is useful in macros, or more complex
1873 ; Emulate other assemblers:
1875 .segment .string(name)
1880 <tag><tt><idx>.STRLEN</idx></tt></tag>
1882 Builtin function. The function accepts a string argument in braces and
1883 eveluates to the length of the string.
1887 The following macro encodes a string as a pascal style string with
1888 a leading length byte.
1892 .byte .strlen(Arg), Arg
1897 <tag><tt><idx>.TCOUNT</idx></tt></tag>
1899 Builtin function. The function accepts a token list in braces. The
1900 function result is the number of tokens given as argument.
1904 The <tt/ldax/ macro accepts the '#' token to denote immidiate addressing (as
1905 with the normal 6502 instructions). To translate it into two separate 8 bit
1906 load instructions, the '#' token has to get stripped from the argument:
1910 .if (.match (.mid (0, 1, arg), #))
1911 ; ldax called with immidiate operand
1912 lda #<(.right (.tcount (arg)-1, arg))
1913 ldx #>(.right (.tcount (arg)-1, arg))
1921 <tag><tt><idx>.WARNING</idx></tt></tag>
1923 Force an assembly warning. The assembler will output a warning message
1924 preceeded by "User warning". This warning will always be output, even
1925 if other warnings are disabled with the <tt/-W0/ command line option.
1927 This command may be used to output possible problems when assembling
1936 .warning "Forward jump in jne, cannot optimize!"
1946 See also the <tt/.ERROR/ and <tt/.OUT/ directives.
1949 <tag><tt><idx>.WORD</idx></tt></tag>
1951 Define word sized data. Must be followed by a sequence of (word ranged,
1952 but not necessarily constant) expressions.
1957 .word $0D00, $AF13, _Clear
1961 <tag><tt><idx>.ZEROPAGE</idx></tt></tag>
1963 Switch to the ZEROPAGE segment and mark it as direct (zeropage) segment.
1964 The name of the ZEROPAGE segment is always "ZEROPAGE", so this is a
1968 .segment "ZEROPAGE", zeropage
1971 Because of the "zeropage" attribute, labels declared in this segment are
1972 addressed using direct addressing mode if possible. You <em/must/ instruct
1973 the linker to place this segment somewhere in the address range 0..$FF
1974 otherwise you will get errors.
1983 Macros may be thought of as "parametrized super instructions". Macros are
1984 sequences of tokens that have a name. If that name is used in the source
1985 file, the macro is "expanded", that is, it is replaced by the tokens that
1986 were specified when the macro was defined.
1988 In it's simplest form, a macro does not have parameters. Here's an
1992 .macro asr ; Arithmetic shift right
1993 cmp #$80 ; Put bit 7 into carry
1994 ror ; Rotate right with carry
1998 The macro above consists of two real instructions, that are inserted into
1999 the code, whenever the macro is expanded. Macro expansion is simply done
2000 by using the name, like this:
2008 When using macro parameters, macros can be even more useful:
2022 When calling the macro, you may give a parameter, and each occurence of
2023 the name "addr" in the macro definition will be replaced by the given
2042 A macro may have more than one parameter, in this case, the parameters
2043 are separated by commas. You are free to give less parameters than the
2044 macro actually takes in the definition. You may also leave intermediate
2045 parameters empty. Empty parameters are replaced by empty space (that is,
2046 they are removed when the macro is exanded). If you have a look at our
2047 macro definition above, you will see, that replacing the "addr" parameter
2048 by nothing will lead to wrong code in most lines. To help you, writing
2049 macros with a variable parameter list, there are some control commands:
2051 <tt/.IFBLANK/ tests the rest of the line and returns true, if there are any
2052 tokens on the remainder of the line. Since empty parameters are replaced by
2053 nothing, this may be used to test if a given parameter is empty.
2054 <tt/.IFNBLANK/ tests the opposite.
2056 Look at this example:
2059 .macro ldaxy a, x, y
2072 This macro may be called as follows:
2075 ldaxy 1, 2, 3 ; Load all three registers
2077 ldaxy 1, , 3 ; Load only a and y
2079 ldaxy , , 3 ; Load y only
2082 There's another helper command for determining, which macro parameters are
2083 valid: <tt/.PARAMCOUNT/. This command is replaced by the parameter count
2084 given, <em/including/ intermediate empty macro parameters:
2087 ldaxy 1 ; .PARAMCOUNT = 1
2088 ldaxy 1,,3 ; .PARAMCOUNT = 3
2089 ldaxy 1,2 ; .PARAMCOUNT = 2
2090 ldaxy 1, ; .PARAMCOUNT = 2
2091 ldaxy 1,2,3 ; .PARAMCOUNT = 3
2094 Macros may be used recursively:
2097 .macro push r1, r2, r3
2106 There's also a special macro to help writing recursive macros:
2107 <tt/.EXITMACRO/. This command will stop macro expansion immidiately:
2110 .macro push r1, r2, r3, r4, r5, r6, r7
2112 ; First parameter is empty
2118 push r2, r3, r4, r5, r6, r7
2122 When expanding this macro, the expansion will push all given parameters
2123 until an empty one is encountered. The macro may be called like this:
2126 push $20, $21, $32 ; Push 3 ZP locations
2127 push $21 ; Push one ZP location
2130 Now, with recursive macros, <tt/.IFBLANK/ and <tt/.PARAMCOUNT/, what else do
2131 you need? Have a look at the inc16 macro above. Here is it again:
2145 If you have a closer look at the code, you will notice, that it could be
2146 written more efficiently, like this:
2160 But imagine what happens, if you use this macro twice? Since the label
2161 "Skip" has the same name both times, you get a "duplicate symbol" error.
2162 Without a way to circumvent this problem, macros are not as useful, as
2163 they could be. One solution is, to start a new lexical block inside the
2180 Now the label is local to the block and not visible outside. However,
2181 sometimes you want a label inside the macro to be visible outside. To make
2182 that possible, there's a new command that's only usable inside a macro
2183 definition: <tt/.LOCAL/. <tt/.LOCAL/ declares one or more symbols as local to
2184 the macro expansion. The names of local variables are replaced by a unique
2185 name in each separate macro expansion. So we could also solve the problem
2186 above by using <tt/.LOCAL/:
2190 .local Skip ; Make Skip a local symbol
2197 Skip: ; Not visible outside
2201 Starting with version 2.5 of the assembler, there is a second macro type
2202 available: C style macros using the <tt/.DEFINE/ directive. These macros are
2203 similar to the classic macro type speified above, but behaviour is sometimes
2208 <item> Macros defined with <tt/.DEFINE/ may not span more than a line. You
2209 may use line continuation (see <tt/.LINECONT/) to spread the
2210 definition over more than one line for increased readability, but the
2211 macro itself does not contain an end-of-line token.
2213 <item> Macros defined with <tt/.DEFINE/ share the name space with classic
2214 macros, but they are detected and replaced at the scanner level. While
2215 classic macros may be used in every place, where a mnemonic or other
2216 directive is allowed, <tt/.DEFINE/ style macros are allowed anywhere
2217 in a line. So they are more versatile in some situations.
2219 <item> <tt/.DEFINE/ style macros may take parameters. While classic macros
2220 may have empty parameters, this is not true for <tt/.DEFINE/ style
2221 macros. For this macro type, the number of actual parameters must
2222 match exactly the number of formal parameters.
2224 To make this possible, formal parameters are enclosed in braces when
2225 defining the macro. If there are no parameters, the empty braces may
2228 <item> Since <tt/.DEFINE/ style macros may not contain end-of-line tokens,
2229 there are things that cannot be done. They may not contain several
2230 processor instructions for example. So, while some things may be done
2231 with both macro types, each type has special usages. The types
2232 complement each other.
2236 Let's look at a few examples to make the advantages and disadvantages
2239 To emulate assemblers that use "<tt/EQU/" instead of "<tt/=/" you may use the
2240 following <tt/.DEFINE/:
2245 foo EQU $1234 ; This is accepted now
2248 You may use the directive to define string constants used elsewhere:
2251 ; Define the version number
2252 .define VERSION "12.3a"
2258 Macros with parameters may also be useful:
2261 .define DEBUG(message) .out message
2263 DEBUG "Assembling include file #3"
2266 Note that, while formal parameters have to be placed in braces, this is
2267 not true for the actual parameters. Beware: Since the assembler cannot
2268 detect the end of one parameter, only the first token is used. If you
2269 don't like that, use classic macros instead:
2277 (This is an example where a problem can be solved with both macro types).
2281 <sect>Macro packages
2284 Using the <tt/.MACPACK/ directive, predefined macro packages may be included
2285 with just one command. Available macro packages are:
2289 <tag><tt><idx>generic</idx></tt></tag>
2291 This macro package defines macros that are useful in almost any program.
2292 Currently, two macros are defined:
2307 <tag><tt><idx>longbranch</idx></tt></tag>
2309 This macro package defines long conditional jumps. They are named like the
2310 short counterpart but with the 'b' replaced by a 'j'. Here is a sample
2311 definition for the "<tt/jeq/" macro, the other macros are built using the
2316 .if .def(Target) .and ((*+2)-(Target) <= 127)
2325 All macros expand to a short branch, if the label is already defined (back
2326 jump) and is reachable with a short jump. Otherwise the macro expands to a
2327 conditional branch with the branch condition inverted, followed by an
2328 absolute jump to the actual branch target.
2330 The package defines the following macros:
2333 jeq, jne, jmi, jpl, jcs, jcc, jvs, jvc
2339 <sect>Bugs/Feedback<p>
2341 If you have problems using the assembler, if you find any bugs, or if
2342 you're doing something interesting with the assembler, I would be glad to
2343 hear from you. Feel free to contact me by email
2344 (<htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">).
2350 ca65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
2351 Bassewitz. For usage of the binaries and/or sources the following
2352 conditions do apply:
2354 This software is provided 'as-is', without any expressed or implied
2355 warranty. In no event will the authors be held liable for any damages
2356 arising from the use of this software.
2358 Permission is granted to anyone to use this software for any purpose,
2359 including commercial applications, and to alter it and redistribute it
2360 freely, subject to the following restrictions:
2363 <item> The origin of this software must not be misrepresented; you must not
2364 claim that you wrote the original software. If you use this software
2365 in a product, an acknowledgment in the product documentation would be
2366 appreciated but is not required.
2367 <item> Altered source versions must be plainly marked as such, and must not
2368 be misrepresented as being the original software.
2369 <item> This notice may not be removed or altered from any source