]> git.sur5r.net Git - cc65/blob - doc/ca65.sgml
ccbef2007656393ee67362f8ec7a422d2881af1e
[cc65] / doc / ca65.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4 <title>ca65 Users Guide
5 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">
6 <date>19.07.2000
7
8 <abstract>
9 ca65 is a macro assembler for the 6502, 65C02 and 65816 CPUs. It is used as a
10 companion assembler for the cc65 crosscompiler, but it may also be used as a
11 standalone product.
12 </abstract>
13
14 <!-- Table of contents -->
15 <toc>
16
17 <!-- Begin the document -->
18
19 <sect>Overview<p>
20
21 ca65 is a replacement for the ra65 assembler that was part of the cc65 C
22 compiler, originally developed by John R. Dunning. I had some problems with
23 ra65 and the copyright does not permit some things which I wanted to be
24 possible, so I decided to write a completely new assembler/linker/archiver
25 suite for the cc65 compiler. ca65 is part of this suite.
26
27 Some parts of the assembler (code generation and some routines for symbol
28 table handling) are taken from an older crossassembler named a816 written
29 by me a long time ago.
30
31 Here's a list of the design criteria, that I considered important for the
32 development:
33
34 <itemize>
35
36 <item>  The assembler must support macros. Macros are not essential, but they
37         make some things easier, especially when you use the assembler in the
38         backend of a compiler.
39 <item>  The assembler must support the newer 65C02 and 65816 CPUs. I have been
40         thinking about a 65816 backend for the C compiler, and even my old
41         a816 assembler had support for these CPUs, so this wasn't really a
42         problem.
43 <item>  The assembler must produce relocatable code. This necessary for the
44         compiler support, and it is more convenient.
45 <item>  Conditional assembly must be supported. This is a must for bigger
46         projects written in assembler (like Elite128).
47 <item>  The assembler must support segments, and it must support more than
48         three segments (this is the count, most other assemblers support).
49         Having more than one code segments helps developing code for systems
50         with a divided ROM area (like the C64).
51 <item>  The linker must be able to resolve arbitrary expressions. It should
52         be able to get things like
53 <tscreen><verb>
54         .import S1, S2
55         .export Special
56         Special = 2*S1 + S2/7
57 </verb></tscreen>
58         right.
59 <item>  True lexical nesting for symbols. This is very convenient for larger
60         assembly projects.
61 <item>  "Cheap" local symbols without lexical nesting for those quick, late
62         night hacks.
63 <item>  I liked the idea of "options" as Anre Fachats .o65 format has it, so I
64         introduced the concept into the object file format use by the new cc65
65         binutils.
66 <item>  The assembler will be a one pass assembler. There was no real need for
67         this decision, but I've written several multipass assemblers, and it
68         started to get boring. A one pass assembler needs much more elaborated
69         data structures, and because of that it's much more fun:-)
70 <item>  Non-GPLed code that may be used in any project without restrictions or
71         fear of "GPL infecting" other code.
72 </itemize>
73 <p>
74
75
76 <sect>Usage<p>
77
78 The assembler accepts the following options:
79
80 <tscreen><verb>
81 ---------------------------------------------------------------------------
82 Usage: ca65 [options] file
83 Short options:
84   -g                    Add debug info to object file
85   -h                    Help (this text)
86   -i                    Ignore case of symbols
87   -l                    Create a listing if assembly was ok
88   -o name               Name the output file
89   -s                    Enable smart mode
90   -t sys                Set the target system
91   -v                    Increase verbosity
92   -D name[=value]       Define a symbol
93   -I dir                Set an include directory search path
94   -U                    Mark unresolved symbols as import
95   -V                    Print the assembler version
96   -W n                  Set warning level n
97
98 Long options:
99   --auto-import         Mark unresolved symbols as import
100   --cpu type            Set cpu type
101   --debug-info          Add debug info to object file
102   --feature name        Set an emulation feature
103   --help                Help (this text)
104   --ignore-case         Ignore case of symbols
105   --include-dir dir     Set an include directory search path
106   --listing             Create a listing if assembly was ok
107   --pagelength n        Set the page length for the listing
108   --smart               Enable smart mode
109   --target sys          Set the target system
110   --verbose             Increase verbosity
111   --version             Print the assembler version
112 ---------------------------------------------------------------------------
113 </verb></tscreen>
114
115 Here is a description of all the command line options:
116
117 <descrip>
118
119   <tag><tt>--cpu type</tt></tag>
120
121   Set the default for the CPU type. The option takes a parameter, which
122   may be one of
123
124         6502, 65C02, 65816 and sunplus
125
126   The latter (sunplus) is not available in the freeware version, because the
127   instruction set of the sunplus CPU is "confidential".
128
129
130   <tag><tt>--feature name</tt></tag>
131
132   Enable an emulation feature. This is identical as using <tt/.FEATURE/
133   in the source with two exceptions: Feature names must be lower case, and
134   each feature must be specified by using an extra <tt/--feature/ option,
135   comma separated lists are not allowed.
136
137   See the discussion of the <tt/.FEATURE/ command for a list of emulation
138   features.
139
140
141   <tag><tt>-g, --debug-info</tt></tag>
142
143   When this option (or the equivalent control command <tt/.DEBUGINFO/) is
144   used, the assembler will add a section to the object file that contains
145   all symbols (including local ones) together with the symbol values and
146   source file positions. The linker will put these additional symbols into
147   the VICE label file, so even local symbols can be seen in the VICE
148   monitor.
149
150
151   <tag><tt>-h, --help</tt></tag>
152
153   Print the short option summary shown above.
154
155
156   <tag><tt>-i, --ignore-case</tt></tag>
157
158   This option makes the assembler case insensitive on identifiers and
159   labels. This option will override the default, but may itself be
160   overriden by the <tt/.CASE/ control command (see section 6).
161
162
163   <tag><tt>-l, --listing</tt></tag>
164
165   Generate an assembler listing. The listing file will always have the
166   name of the main input file with the extension replaced by ".lst". This
167   may change in future versions.
168
169
170   <tag><tt>-o name</tt></tag>
171
172   The default output name is the name of the input file with the extension
173   replaced by ".o". If you don't like that, you may give another name with
174   the -o option. The output file will be placed in the same directory as
175   the source file, or, if -o is given, the full path in this name is used.
176
177
178   <tag><tt>--pagelength n</tt></tag>
179
180   sets the length of a listing page in lines. See the <tt/.PAGELENGTH/
181   directive for more information.
182
183
184   <tag><tt>-s, --smart-mode</tt></tag>
185
186   In smart mode (enabled by -s or the <tt/.SMART/ pseudo instruction) the
187   assembler will track usage of the REP and SEP instructions in 65816 mode
188   and update the operand sizes accordingly. If the operand of such an
189   instruction cannot be evaluated by the assembler (for example, because
190   the operand is an imported symbol), a warning is issued.
191
192   Beware: Since the assembler cannot trace the execution flow this may
193   lead to false results in some cases. If in doubt, use the .ixx and .axx
194   instructions to tell the assembler about the current settings. Smart
195   mode is off by default.
196
197
198   <tag><tt>-t sys, --target sys</tt></tag>
199
200   Set the target system. This will enable translation of character strings
201   and character constants into the character set of the target platform.
202   The default for the target system is "none", which means that no translation
203   will take place. The assembler supports the same target systems as the
204   compiler, see there for a list.
205
206
207   <tag><tt>-v, --verbose</tt></tag>
208
209   Increase the assembler verbosity. Usually only needed for debugging
210   purposes. You may use this option more than one time for even more
211   verbose output.
212
213
214   <tag><tt>-D</tt></tag>
215
216   This option allows you to define symbols on the command line. Without a
217   value, the symbol is defined with the value zero. When giving a value,
218   you may use the '&dollar;' prefix for hexadecimal symbols. Please note
219   that for some operating systems, '&dollar;' has a special meaning, so
220   you may have to quote the expression.
221
222
223   <tag><tt>-I dir, --include-dir dir</tt></tag>
224
225   Name a directory which is searched for include files. The option may be
226   used more than once to specify more than one directory to search. The
227   current directory is always searched first before considering any
228   additional directores.
229
230
231   <tag><tt>-U, --auto-import</tt></tag>
232
233   Mark symbols that are not defined in the sources as imported symbols.
234   This should be used with care since it delays error messages about typos
235   and such until the linker is run. The compiler uses the equivalent of
236   this switch (<tt/.AUTOIMPORT/, see control command section below) to enable
237   auto imported symbols for the runtime library. However, the compiler is
238   supposed to generate code that runs through the assembler without
239   problems, something which is not always true for assembler programmers.
240
241
242   <tag><tt>-V, --version</tt></tag>
243
244   Print the version number of the assembler. If you send any suggestions
245   or bugfixes, please include the version number.
246
247
248   <tag><tt>-Wn</tt></tag>
249
250   Set the warning level for the assembler. Using -W2 the assembler will
251   even warn about such things like unused imported symbols. The default
252   warning level is 1, and it would probably be silly to set it to
253   something lower.
254
255 </descrip>
256 <p>
257
258
259 <sect>Input format<p>
260
261 The assembler accepts the standard 6502/65816 assembler syntax. One line
262 may contain a label (which is identified by a colon), and, in addition to
263 the label, an assembler mnemonic, a macro, or a control command (see
264 section 6 for supported control commands). Alternatively, the line may
265 contain a symbol definition using the '=' token. Everything after a
266 semicolon is handled as a comment (that is, it is ignored).
267
268 Here are some examples for valid input lines:
269
270 <tscreen><verb>
271         Label:                          ; A label and a comment
272                 lda     #$20            ; A 6502 instruction plus comment
273         L1:     ldx     #$20            ; Same with label
274         L2:     .byte   "Hello world"   ; Label plus control command
275                 mymac   $20             ; Macro expansion
276                 MySym = 3*L1            ; Symbol definition
277         MaSym   = Label                 ; Another symbol
278 </verb></tscreen>
279
280 The assembler accepts all valid 6502 mnemonics when in 6502 mode (the
281 default). The assembler accepts all valid 65SC02 mnemonics when in 65SC02
282 mode (after a <tt/.PC02/ command is found). The assembler accepts all valid
283 65816 mnemonics with a few exceptions after a .P816 command is found.
284 These exceptions are listed below.
285
286 In 65816 mode several aliases are accepted in addition to the official
287 mnemonics:
288
289 <tscreen><verb>
290         BGE is an alias for BCS
291         BLT is an alias for BCC
292         CPA is an alias for CMP
293         DEA is an alias for DEC A
294         INA is an alias for INC A
295         SWA is an alias for XBA
296         TAD is an alias for TCD
297         TAS is an alias for TCS
298         TDA is an alias for TDC
299         TSA is an alias for TSC
300 </verb></tscreen>
301
302 Evaluation of banked expressions in 65816 mode differs slightly from the
303 official syntax:
304
305 Instead of accepting a 24 bit address (something that is difficult for
306 the assembler to determine and would have required one more special
307 .import command), the bank and the absolute address in that bank are
308 separated by a dot:
309
310 <tscreen><verb>
311         jsl     3.$1234         ; Call subroutine at $1234 in bank 3
312 </verb></tscreen>
313
314 For literal values, the assembler accepts the widely used number formats:
315 A preceeding '&dollar;' denotes a hex value, a preceeding '%' denotes a
316 binary value, and a bare number is interpeted as a decimal. There are
317 currently no octal values and no floats.
318 <p>
319
320
321 <sect>Expressions
322
323 <p>
324 All expressions are evaluated with (at least) 32 bit precision. An
325 expression may contain constant values and any combination of internal and
326 external symbols. Expressions that cannot be evaluated at assembly time
327 are stored inside the object file for evaluation by the linker.
328 Expressions referencing imported symbols must always be evaluated by the
329 linker.
330
331 Sometimes, the assembler must know about the size of the value that is the
332 result of an expression. This is usually the case, if a decision has to be
333 made, to generate a zero page or an absolute memory references. In this
334 case, the assembler has to make some assumptions about the result of an
335 expression:
336
337 <itemize>
338 <item>  If the result of an expression is constant, the actual value is
339         checked to see if it's a byte sized expression or not.
340 <item>  If the expression is explicitly casted to a byte sized expression by
341         one of the '&gt;'/'&lt;' operators, it is a byte expression.
342 <item>  If this is not the case, and the expression contains a symbol,
343         explicitly declared as zero page symbol (by one of the .importzp or
344         .exportzp instructions), then the whole expression is assumed to be
345         byte sized.
346 <item>  If the expression contains symbols that are not defined, and these
347         symbols are local symbols, the enclosing scopes are searched for a
348         symbol with the same name. If one exists and this symbol is defined,
349         it's attributes are used to determine the result size.
350 <item>  In all other cases the expression is assumed to be word sized.
351 </itemize>
352
353 Note: If the assembler is not able to evaluate the expression at assembly
354 time, the linker will evaluate it and check for range errors as soon as
355 the result is known.
356
357
358 <bf>Boolean expressions:</bf>
359
360 In the context of a boolean expression, any non zero value is evaluated as
361 true, any other value to false. The result of a boolean expression is 1 if
362 it's true, and zero if it's false. There are boolean operators with extrem
363 low precedence with version 2.x (where x &gt; 0). The <tt/.AND/ and <tt/.OR/
364 operators are shortcut operators. That is, if the result of the expression is
365 already known, after evaluating the left hand side, the right hand side is
366 not evaluated.
367
368
369 Available operators sorted by precedence:
370
371 <tscreen><verb>
372     Op          Description                             Precedence
373   -------------------------------------------------------------------
374     .CONCAT     Builtin function                        0
375     .LEFT       Builtin function                        0
376     .MID        Builtin function                        0
377     .RIGHT      Builtin function                        0
378     .STRING     Builtin function                        0
379
380     *           Builtin pseudo variable (r/o)           1
381     .BLANK      Builtin function                        1
382     .CONST      Builtin function                        1
383     .CPU        Builtin pseudo variable (r/o)           1
384     .DEFINED    Builtin function                        1
385     .MATCH      Builtin function                        1
386     .TCOUNT     Builtin function                        1
387     .XMATCH     Builtin function                        1
388     .PARAMCOUNT Builtin pseudo variable (r/o)           1
389     .REFERENCED Builtin function                        1
390     ::          Global namespace override               1
391     +           Unary plus                              1
392     -           Unary minus                             1
393     ~           Unary bitwise not                       1
394     .BITNOT     Unary bitwise not                       1
395     &lt;        Low byte operator                       1
396     &gt;        High byte operator                      1
397
398     *           Multiplication                          2
399     /           Division                                2
400     .MOD        Modulo operation                        2
401     &amp;       Bitwise and                             2
402     .BITAND     Bitwise and                             2
403     ^           Bitwise xor                             2
404     .BITXOR     Bitwise xor                             2
405     &lt;&lt;    Shift left operator                     2
406     .SHL        Shift left operator                     2
407     &gt;&gt;    Shift right operator                    2
408     .SHR        Shift right operator                    2
409
410     +           Binary plus                             3
411     -           Binary minus                            3
412     |           Binary or                               3
413     .BITOR      Binary or                               3
414
415     =           Compare operation (equal)               4
416     &lt;&gt;    Compare operation (not equal)           4
417     &lt;        Compare operation (less)                4
418     &gt;        Compare operation (greater)             4
419     &lt;=       Compare operation (less or equal)       4
420     &gt;=       Compare operation (greater or equal)    4
421
422     &amp;&amp;          Boolean and                             5
423     .AND        Boolean and                             5
424     .XOR        Boolean xor                             5
425
426     ||          Boolean or                              6
427     .OR         Boolean or                              6
428
429     !           Boolean not                             7
430     .NOT        Boolean not                             7
431 </verb></tscreen>
432
433
434 To force a specific order of evaluation, braces may be used as usual.
435
436 Some of the pseudo variables mentioned above need some more explanation:
437
438 <tscreen><verb>
439   *             This symbol is replaced by the value of the program
440                 counter at start of the current instruction. Note, that
441                 '*' yields a rvalue, that means, you cannot assign to it.
442                 Use <tt/.ORG/ to set the program counter in sections with
443                 absolute code.
444 </verb></tscreen>
445 <p>
446
447
448
449 <sect>Symbols and labels
450
451 <p>
452 The assembler allows you to use symbols instead of naked values to make
453 the source more readable. There are a lot of different ways to define and
454 use symbols and labels, giving a lot of flexibility.
455
456 <descrip>
457
458
459 <tag/Numeric constants/
460
461     Numeric constants are defined using the equal sign. After doing
462
463     <tscreen><verb>
464           two = 2
465     </verb></tscreen>
466
467     may use the symbol "two" in every place where a number is expected,
468     and it is evaluated to the value 2 in this context. An example would be
469
470     <tscreen><verb>
471           four = two * two
472     </verb></tscreen>
473
474
475 <tag/Standard labels/
476
477     A label is defined by writing the name of the label at the start of
478     the line (before any instruction mnemonic, macro or pseudo
479     directive), followed by a colon. This will declare a symbol with the
480     given name and the value of the current program counter.
481
482
483 <tag/Local labels and symbols/
484
485     Using the <tt/.PROC/ directive, it is possible to create regions of code
486     where the names of labels and symbols are local to this region. They
487     are not know outside and cannot be accessed from there. Such regions
488     may be nested like PROCEDUREs in Pascal.
489
490     See the description of the <tt/.PROC/ directive for more information.
491
492 <tag/Cheap local labels/
493
494     Cheap local labels are defined like standard labels, but the name of
495     the label must begin with a special symbol (usually '@', but this can
496     be changed by the <tt/.LOCALCHAR/ directive).
497
498     Cheap local labels are visible only between two no cheap labels. As
499     soon as a standard symbol is encountered (this may also be a local
500     symbol if inside a region defined with the .PROC directive), the
501     cheap local symbol goes out of scope.
502
503     You may use cheap local labels as an easy way to reuse common label
504     names like "Loop". Here is an example:
505
506     <tscreen><verb>
507         Clear:  lda     #$00            ; Global label
508                 ldy     #$20
509         @Loop:  sta     Mem,y           ; Local label
510                 dey
511                 bne     @Loop           ; Ok
512                 rts
513         Sub:    ...                     ; New global label
514                 bne     @Loop           ; ERROR: Unknown identifier!
515     </verb></tscreen>
516
517 <tag/Unnamed labels/
518
519     If you really want to write messy code, there are also unnamed
520     labels. These labels do not have a name (you guessed that already,
521     didn't you?). A colon is used to mark the absence of the name.
522
523     Unnamed labels may be accessed by using the colon plus several minus
524     or plus characters as a label designator. Using the '-' characters
525     will create a back reference (use the n'th label backwards), using
526     '+' will create a forward reference (use the n'th label in forward
527     direction). An example will help to understand this:
528
529     <tscreen><verb>
530         :       lda     (ptr1),y        ; #1
531                 cmp     (ptr2),y
532                 bne     :+              ; -> #2
533                 tax
534                 beq     :+++            ; -> #4
535                 iny
536                 bne     :-              ; -> #1
537                 inc     ptr1+1
538                 inc     ptr2+1
539                 bne     :-              ; -> #1
540
541         :       bcs     :+              ; #2 -> #3
542                 ldx     #$FF
543                 rts
544
545         :       ldx     #$01            ; #3
546         :       rts                     ; #4
547     </verb></tscreen>
548
549     As you can see from the example, unnamed labels will make even short
550     sections of code hard to understand, because you have to count labels
551     to find branch targets (this is the reason why I for my part do
552     prefer the "cheap" local labels). Nevertheless, unnamed labels are
553     convenient in some situations, so it's your decision.
554
555 <tag/Using macros to define labels and constants/
556
557     While there are drawbacks with this approach, it may be handy in some
558     situations. Using <tt/.DEFINE/, it is possible to define symbols or
559     constants that may be used elsewhere. Since the macro facility works
560     on a very low level, there is no scoping. On the other side, you may
561     also define string constants this way (this is not possible with the
562     other symbol types).
563
564     Example:
565
566     <tscreen><verb>
567         .DEFINE two     2
568         .DEFINE version "SOS V2.3"
569
570         four = two * two        ; Ok
571         .byte   version         ; Ok
572
573         .PROC                   ; Start local scope
574         two = 3                 ; Will give "2 = 3" - invalid!
575         .ENDPROC
576     </verb></tscreen>
577
578 </descrip>
579
580
581 If <tt/.DEBUGINFO/ is enabled (or -g is given on the command line), global,
582 local and cheap local labels are written to the object file and will be
583 available in the symbol file via the linker. Unnamed labels are not
584 written to the object file, because they don't have a name which would
585 allow to access them.
586
587
588
589 <sect>Control commands
590
591 <p>
592 Here's a list of all control commands and a description, what they do:
593
594 <descrip>
595
596 <tag><tt><idx>.A16</idx></tt></tag>
597
598   Valid only in 65816 mode. Switch the accumulator to 16 bit.
599
600   Note: This command will not emit any code, it will tell the assembler to
601   create 16 bit operands for immediate accumulator adressing mode.
602
603   See also: <tt/.SMART/
604
605
606 <tag><tt><idx>.A8</idx></tt></tag>
607
608   Valid only in 65816 mode. Switch the accumulator to 8 bit.
609
610   Note: This command will not emit any code, it will tell the assembler to
611   create 8 bit operands for immediate accu adressing mode.
612
613   See also: <tt/.SMART/
614
615
616 <tag><tt><idx>.ADDR</idx></tt></tag>
617
618   Define word sized data. In 6502 mode, this is an alias for <tt/.WORD/ and
619   may be used for better readability if the data words are address values.
620   In 65816 mode, the address is forced to be 16 bit wide to fit into the
621   current segment. See also <tt/.FARADDR/. The command must be followed by a
622   sequence of (not necessarily constant) expressions.
623
624   Example:
625
626   <tscreen><verb>
627         .addr   $0D00, $AF13, _Clear
628   </verb></tscreen>
629
630
631 <tag><tt><idx>.ALIGN</idx></tt></tag>
632
633   Align data to a given boundary. The command expects a constant integer
634   argument that must be a power of two, plus an optional second argument
635   in byte range. If there is a second argument, it is used as fill value,
636   otherwise the value defined in the linker configuration file is used
637   (the default for this value is zero).
638
639   Since alignment depends on the base address of the module, you must
640   give the same (or a greater) alignment for the segment when linking.
641   The linker will give you a warning, if you don't do that.
642
643   Example:
644
645   <tscreen><verb>
646         .align  256
647   </verb></tscreen>
648
649
650 <tag><tt><idx>.ASCIIZ</idx></tt></tag>
651
652   Define a string with a trailing zero.
653
654   Example:
655
656   <tscreen><verb>
657         Msg:    .asciiz "Hello world"
658   </verb></tscreen>
659
660   This will put the string "Hello world" followed by a binary zero into
661   the current segment. There may be more strings separated by commas, but
662   the binary zero is only appended once (after the last one).
663
664
665 <tag><tt><idx>.AUTOIMPORT</idx></tt></tag>
666
667   Is followd by a plus or a minus character. When switched on (using a
668   +), undefined symbols are automatically marked as import instead of
669   giving errors. When switched off (which is the default so this does not
670   make much sense), this does not happen and an error message is
671   displayed. The state of the autoimport flag is evaluated when the
672   complete source was translated, before outputing actual code, so it is
673   <em/not/ possible to switch this feature on or off for separate sections
674   of code. The last setting is used for all symbols.
675
676   You should probably not use this switch because it delays error
677   messages about undefined symbols until the link stage. The cc65
678   compiler (which is supposed to produce correct assembler code in all
679   circumstances, something which is not true for most assembler
680   programmers) will insert this command to avoid importing each and every
681   routine from the runtime library.
682
683   Example:
684
685   <tscreen><verb>
686         .autoimport     +       ; Switch on auto import
687   </verb></tscreen>
688
689
690 <tag><tt><idx>.BLANK</idx></tt></tag>
691
692   Builtin function. The function evaluates its argument in braces and
693   yields "false" if the argument is non blank (there is an argument), and
694   "true" if there is no argument. As an example, the <tt/.IFBLANK/ statement
695   may be replaced by
696
697   <tscreen><verb>
698         .if     .blank(arg)
699   </verb></tscreen>
700
701
702 <tag><tt><idx>.BSS</idx></tt></tag>
703
704   Switch to the BSS segment. The name of the BSS segment is always "BSS",
705   so this is a shortcut for
706
707   <tscreen><verb>
708         .segment  "BSS"
709   </verb></tscreen>
710
711   See also the <tt/.SEGMENT/ command.
712
713
714 <tag><tt><idx>.BYTE</idx></tt></tag>
715
716   Define byte sized data. Must be followed by a sequence of (byte ranged)
717   expressions or strings.
718
719   Example:
720
721   <tscreen><verb>
722         .byte   "Hello world", $0D, $00
723   </verb></tscreen>
724
725
726 <tag><tt><idx>.CASE</idx></tt></tag>
727
728   Switch on or off case sensitivity on identifiers. The default is off
729   (that is, identifiers are case sensitive), but may be changed by the
730   -i switch on the command line.
731   The command must be followed by a '+' or '-' character to switch the
732   option on or off respectively.
733
734   Example:
735
736   <tscreen><verb>
737         .case   -               ; Identifiers are not case sensitive
738   </verb></tscreen>
739
740
741 <tag><tt><idx>.CODE</idx></tt></tag>
742
743   Switch to the CODE segment. The name of the CODE segment is always
744   "CODE", so this is a shortcut for
745
746   <tscreen><verb>
747         .segment  "CODE"
748   </verb></tscreen>
749
750   See also the <tt/.SEGMENT/ command.
751
752
753 <tag><tt><idx>.CONCAT</idx></tt></tag>
754
755   Builtin function. The function allows to concatenate a list of string
756   constants separated by commas. The result is a string constant that
757   is the concatentation of all arguments. This function is most useful
758   in macros and when used together with the <tt/.STRING/ builtin function.
759   The function may be used in any case where a string constant is
760   expected.
761
762   Example:
763
764   <tscreen><verb>
765         .include        .concat ("myheader", ".", "inc)
766   </verb></tscreen>
767
768   This is the same as the command
769
770   <tscreen><verb>
771         .include        "myheader.inc"
772   </verb></tscreen>
773
774
775 <tag><tt><idx>.CONST</idx></tt></tag>
776
777   Builtin function. The function evaluates its argument in braces and
778   yields "true" if the argument is a constant expression (that is, an
779   expression that yields a constant value at assembly time) and "false"
780   otherwise. As an example, the .IFCONST statement may be replaced by
781
782   <tscreen><verb>
783         .if     .const(a + 3)
784   </verb></tscreen>
785
786
787 <tag><tt><idx>.CPU</idx></tt></tag>
788
789   Reading this pseudo variable will give a constant integer value that
790   tells which instruction set is currently enabled. Possible values are:
791
792   <tscreen><verb>
793         0 -->   6502
794         1 -->   65SC02
795         2 -->   65SC816
796         3 -->   SunPlus SPC
797   </verb></tscreen>
798
799   It may be used to replace the .IFPxx pseudo instructions or to construct
800   even more complex expressions.
801
802   Example:
803
804   <tscreen><verb>
805         .if     (.cpu = 0) .or (.cpu = 1)
806                 txa
807                 pha
808                 tya
809                 pha
810         .else
811                 phx
812                 phy
813         .endif
814   </verb></tscreen>
815
816
817 <tag><tt><idx>.DATA</idx></tt></tag>
818
819   Switch to the DATA segment. The name of the DATA segment is always
820   "DATA", so this is a shortcut for
821
822   <tscreen><verb>
823         .segment  "DATA"
824   </verb></tscreen>
825
826   See also the <tt/.SEGMENT/ command.
827
828
829 <tag><tt><idx>.DBYT</idx></tt></tag>
830
831   Define word sized data with the hi and lo bytes swapped (use <tt/.WORD/ to
832   create word sized data in native 65XX format). Must be followed by a
833   sequence of (word ranged) expressions.
834
835   Example:
836
837   <tscreen><verb>
838         .dbyt   $1234, $4512
839   </verb></tscreen>
840
841   This will emit the bytes
842
843   <tscreen><verb>
844         $12 $34 $45 $12
845   </verb></tscreen>
846
847   into the current segment in that order.
848
849
850 <tag><tt><idx>.DEBUGINFO</idx></tt></tag>
851
852   Switch on or off debug info generation. The default is off (that is,
853   the object file will not contain debug infos), but may be changed by the
854   -g switch on the command line.
855   The command must be followed by a '+' or '-' character to switch the
856   option on or off respectively.
857
858   Example:
859
860   <tscreen><verb>
861         .debuginfo      +       ; Generate debug info
862   </verb></tscreen>
863
864
865 <tag><tt><idx>.DEFINE</idx></tt></tag>
866
867   Start a define style macro definition. The command is followed by an
868   identifier (the macro name) and optionally by a list of formal arguments
869   in braces.
870   See separate section about macros.
871
872
873 <tag><tt><idx>.DEF,</idx> <idx>.DEFINED</idx></tt></tag>
874
875   Builtin function. The function expects an identifier as argument in
876   braces. The argument is evaluated, and the function yields "true" if the
877   identifier is a symbol that is already defined somewhere in the source
878   file up to the current position. Otherwise the function yields false. As
879   an example, the <tt/.IFDEF/ statement may be replaced by
880
881   <tscreen><verb>
882         .if     .defined(a)
883   </verb></tscreen>
884
885
886 <tag><tt><idx>.DWORD</idx></tt></tag>
887
888   Define dword sized data (4 bytes) Must be followed by a sequence of
889   expressions.
890
891   Example:
892
893   <tscreen><verb>
894         .dword  $12344512, $12FA489
895   </verb></tscreen>
896
897
898 <tag><tt><idx>.ELSE</idx></tt></tag>
899
900   Conditional assembly: Reverse the current condition.
901
902
903 <tag><tt><idx>.ELSEIF</idx></tt></tag>
904
905   Conditional assembly: Reverse current condition and test a new one.
906
907
908 <tag><tt><idx>.END</idx></tt></tag>
909
910   Forced end of assembly. Assembly stops at this point, even if the command
911   is read from an include file.
912
913
914 <tag><tt><idx>.ENDIF</idx></tt></tag>
915
916   Conditional assembly: Close a <tt/.IF.../ or <tt/.ELSE/ branch.
917
918
919 <tag><tt><idx>.ENDMAC,</idx> <idx>.ENDMACRO</idx></tt></tag>
920
921   End of macro definition (see separate section).
922
923
924 <tag><tt><idx>.ENDPROC</idx></tt></tag>
925
926   End of local lexical level (see <tt/.PROC/).
927
928
929 <tag><tt><idx>.ENDREP,</idx> <idx>.ENDREPEAT</idx></tt></tag>
930
931   End a <tt/.REPEAT/ block. See the <tt/.REPEAT/ command.
932
933
934 <tag><tt><idx>.ERROR</idx></tt></tag>
935
936   Force an assembly error. The assembler will output an error message
937   preceeded by "User error" and will <em/not/ produce an object file.
938
939   This command may be used to check for initial conditions that must be
940   set before assembling a source file.
941
942   Example:
943
944   <tscreen><verb>
945         .if     foo = 1
946         ...
947         .elseif bar = 1
948         ...
949         .else
950         .error  "Must define foo or bar!"
951         .endif
952   </verb></tscreen>
953
954   See also the <tt/.WARNING/ and <tt/.OUT/ directives.
955
956
957 <tag><tt><idx>.EXITMAC,</idx> <idx>.EXITMACRO</idx></tt></tag>
958
959   Abort a macro expansion immidiately. This command is often useful in
960   recursive macros. See separate chapter about macros.
961
962
963 <tag><tt><idx>.EXPORT</idx></tt></tag>
964
965   Make symbols accessible from other modules. Must be followed by a comma
966   separated list of symbols to export.
967
968   Example:
969
970   <tscreen><verb>
971         .export foo, bar
972   </verb></tscreen>
973
974
975 <tag><tt><idx>.EXPORTZP</idx></tt></tag>
976
977   Make symbols accessible from other modules. Must be followed by a comma
978   separated list of symbols to export. The exported symbols are explicitly
979   marked as zero page symols.
980
981   Example:
982
983   <tscreen><verb>
984         .exportzp  foo, bar
985   </verb></tscreen>
986
987
988 <tag><tt><idx>.FARADDR</idx></tt></tag>
989
990   Define far (24 bit) address data. The command must be followed by a
991   sequence of (not necessarily constant) expressions.
992
993   Example:
994
995   <tscreen><verb>
996         .faraddr        DrawCircle, DrawRectangle, DrawHexagon
997   </verb></tscreen>
998
999
1000 <tag><tt><idx>.FEATURE</idx></tt></tag>
1001
1002   This directive may be used to enable one or more compatibility features
1003   of the assembler. While the use of <tt/.FEATURE/ should be avoided when
1004   possible, it may be useful when porting sources written for other
1005   assemblers. There is no way to switch a feature off, once you have
1006   enabled it, so using
1007
1008   <tscreen><verb>
1009         .FEATURE        xxx
1010   </verb></tscreen>
1011
1012   will enable the feature until end of assembly is reached.
1013
1014   The following features are available:
1015
1016   <descrip>
1017
1018   <tag><tt><idx>dollar_is_pc</idx></tt></tag>
1019
1020     The dollar sign may be used as an alias for the star (`*'), which
1021     gives the value of the current PC in expressions.
1022     Note: Assignment to the pseudo variable is not allowed.
1023
1024   <tag><tt><idx>labels_without_colons</idx></tt></tag>
1025
1026     Allow labels without a trailing colon. These labels are only accepted,
1027     if they start at the beginning of a line (no leading white space).
1028
1029   <tag><tt><idx>loose_string_term</idx></tt></tag>
1030
1031     Accept single quotes as well as double quotes as terminators for string
1032     constants.
1033
1034   <tag><tt><idx>at_in_identifiers</idx></tt></tag>
1035
1036     Accept the at character (`@') as a valid character in identifiers. The
1037     at character is not allowed to start an identifier, even with this
1038     feature enabled.
1039
1040   <tag><tt><idx>dollar_in_identifiers</idx></tt></tag>
1041
1042     Accept the dollar sign (`&dollar;') as a valid character in identifiers. The
1043     at character is not allowed to start an identifier, even with this
1044     feature enabled.
1045
1046   <tag><tt><idx>pc_assignment</idx></tt></tag>
1047
1048     Allow assignments to the PC symbol (`*' or `&dollar;' if dollar_is_pc is
1049     enabled). Such an assignment is handled identical to the <tt/.ORG/
1050     command (which is usually not needed, so just removing the lines with
1051     the assignments may also be an option when porting code written for older
1052     assemblers).
1053
1054   </descrip>
1055
1056
1057 <tag><tt><idx>.FILEOPT,</idx> <idx>.FOPT</idx></tt></tag>
1058
1059   Insert an option string into the object file. There are two forms of
1060   this command, one specifies the option by a keyword, the second
1061   specifies it as a number. Since usage of the second one needs knowledge
1062   of the internal encoding, its use is not recommended and I will only
1063   describe the first form here.
1064
1065   The command is followed by one of the keywords
1066
1067   <tscreen><verb>
1068         author
1069         comment
1070         compiler
1071   </verb></tscreen>
1072
1073   a comma and a string. The option is written into the object file
1074   together with the string value. This is currently unidirectional and
1075   there is no way to actually use these options once they are in the
1076   object file.
1077
1078   Examples:
1079
1080   <tscreen><verb>
1081         .fileopt        comment, "Code stolen from my brother"
1082         .fileopt        compiler, "BASIC 2.0"
1083         .fopt           author, "J. R. User"
1084   </verb></tscreen>
1085
1086
1087 <tag><tt><idx>.GLOBAL</idx></tt></tag>
1088
1089   Declare symbols as global. Must be followed by a comma separated list
1090   of symbols to declare. Symbols from the list, that are defined somewhere
1091   in the source, are exported, all others are imported. An additional
1092   explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same symbol is
1093   allowed.
1094
1095   Example:
1096
1097   <tscreen><verb>
1098         .global foo, bar
1099   </verb></tscreen>
1100
1101
1102 <tag><tt><idx>.GLOBALZP</idx></tt></tag>
1103
1104   Declare symbols as global. Must be followed by a comma separated list
1105   of symbols to declare. Symbols from the list, that are defined
1106   somewhere in the source, are exported, all others are imported. An
1107   additional explicit <tt/.IMPORT/ or <tt/.EXPORT/ command for the same
1108   symbol is explicitly allowed. The symbols in the list are explicitly
1109   marked as zero page symols.
1110
1111   Example:
1112
1113   <tscreen><verb>
1114         .globalzp foo, bar
1115   </verb></tscreen>
1116
1117
1118 <tag><tt><idx>.I16</idx></tt></tag>
1119
1120   Valid only in 65816 mode. Switch the index registers to 16 bit.
1121
1122   Note: This command will not emit any code, it will tell the assembler to
1123   create 16 bit operands for immediate operands.
1124
1125   See also the <tt/.SMART/ command.
1126
1127
1128 <tag><tt><idx>.I8</idx></tt></tag>
1129
1130   Valid only in 65816 mode. Switch the index registers to 8 bit.
1131
1132   Note: This command will not emit any code, it will tell the assembler to
1133   create 8 bit operands for immediate operands.
1134
1135   See also the <tt/.SMART/ command.
1136
1137
1138 <tag><tt><idx>.IF</idx></tt></tag>
1139
1140   Conditional assembly: Evalute an expression and switch assembler output
1141   on or off depending on the expression. The expression must be a constant
1142   expression, that is, all operands must be defined.
1143
1144   A expression value of zero evaluates to FALSE, any other value evaluates
1145   to TRUE.
1146
1147
1148 <tag><tt><idx>.IFBLANK</idx></tt></tag>
1149
1150   Conditional assembly: Check if there are any remaining tokens in this
1151   line, and evaluate to FALSE if this is the case, and to TRUE otherwise.
1152   If the condition is not true, further lines are not assembled until
1153   an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1154
1155   This command is often used to check if a macro parameter was given.
1156   Since an empty macro parameter will evaluate to nothing, the condition
1157   will evaluate to FALSE if an empty parameter was given.
1158
1159   Example:
1160
1161   <tscreen><verb>
1162         .macro     arg1, arg2
1163         .ifblank   arg2
1164                    lda     #arg1
1165         .else
1166                    lda     #arg2
1167         .endif
1168         .endmacro
1169   </verb></tscreen>
1170
1171   See also: <tt/.BLANK/
1172
1173
1174 <tag><tt><idx>.IFCONST</idx></tt></tag>
1175
1176   Conditional assembly: Evaluate an expression and switch assembler output
1177   on or off depending on the constness of the expression.
1178
1179   A const expression evaluates to to TRUE, a non const expression (one
1180   containing an imported or currently undefined symbol) evaluates to
1181   FALSE.
1182
1183   See also: <tt/.CONST/
1184
1185
1186 <tag><tt><idx>.IFDEF</idx></tt></tag>
1187
1188   Conditional assembly: Check if a symbol is defined. Must be followed by
1189   a symbol name. The condition is true if the the given symbol is already
1190   defined, and false otherwise.
1191
1192   See also: <tt/.DEFINED/
1193
1194
1195 <tag><tt><idx>.IFNBLANK</idx></tt></tag>
1196
1197   Conditional assembly: Check if there are any remaining tokens in this
1198   line, and evaluate to TRUE if this is the case, and to FALSE otherwise.
1199   If the condition is not true, further lines are not assembled until
1200   an <tt/.ELSE/, <tt/.ELSEIF/ or <tt/.ENDIF/ directive.
1201
1202   This command is often used to check if a macro parameter was given.
1203   Since an empty macro parameter will evaluate to nothing, the condition
1204   will evaluate to FALSE if an empty parameter was given.
1205
1206   Example:
1207
1208   <tscreen><verb>
1209         .macro     arg1, arg2
1210                    lda     #arg1
1211         .ifnblank  arg2
1212                    lda     #arg2
1213         .endif
1214         .endmacro
1215   </verb></tscreen>
1216
1217   See also: <tt/.BLANK/
1218
1219
1220 <tag><tt><idx>.IFNDEF</idx></tt></tag>
1221
1222   Conditional assembly: Check if a symbol is defined. Must be followed by
1223   a symbol name. The condition is true if the the given symbol is not
1224   defined, and false otherwise.
1225
1226   See also: <tt/.DEFINED/
1227
1228
1229 <tag><tt><idx>.IFNREF</idx></tt></tag>
1230
1231   Conditional assembly: Check if a symbol is referenced. Must be followed
1232   by a symbol name. The condition is true if if the the given symbol was
1233   not referenced before, and false otherwise.
1234
1235   See also: <tt/.REFERENCED/
1236
1237
1238 <tag><tt><idx>.IFP02</idx></tt></tag>
1239
1240   Conditional assembly: Check if the assembler is currently in 6502 mode
1241   (see <tt/.P02/ command).
1242
1243
1244 <tag><tt><idx>.IFP816</idx></tt></tag>
1245
1246   Conditional assembly: Check if the assembler is currently in 65816 mode
1247   (see <tt/.P816/ command).
1248
1249
1250 <tag><tt><idx>.IFPC02</idx></tt></tag>
1251
1252   Conditional assembly: Check if the assembler is currently in 65C02 mode
1253   (see <tt/.PC02/ command).
1254
1255
1256 <tag><tt><idx>.IFREF</idx></tt></tag>
1257
1258   Conditional assembly: Check if a symbol is referenced. Must be followed
1259   by a symbol name. The condition is true if if the the given symbol was
1260   referenced before, and false otherwise.
1261
1262   This command may be used to build subroutine libraries in include files
1263   (you may use separate object modules for this purpose too).
1264
1265   Example:
1266
1267   <tscreen><verb>
1268         .ifref  ToHex                   ; If someone used this subroutine
1269         ToHex:  tay                     ; Define subroutine
1270                 lda     HexTab,y
1271                 rts
1272         .endif
1273   </verb></tscreen>
1274
1275   See also: <tt/.REFERENCED/
1276
1277
1278 <tag><tt><idx>.IMPORT</idx></tt></tag>
1279
1280   Import a symbol from another module. The command is followed by a comma
1281   separated list of symbols to import.
1282
1283   Example:
1284
1285   <tscreen><verb>
1286         .import foo, bar
1287   </verb></tscreen>
1288
1289
1290 <tag><tt><idx>.IMPORTZP</idx></tt></tag>
1291
1292   Import a symbol from another module. The command is followed by a comma
1293   separated list of symbols to import. The symbols are explicitly imported
1294   as zero page symbols (that is, symbols with values in byte range).
1295
1296   Example:
1297
1298   <tscreen><verb>
1299         .includezp  foo, bar
1300   </verb></tscreen>
1301
1302
1303 <tag><tt><idx>.INCBIN</idx></tt></tag>
1304
1305   Include a file as binary data. The command expects a string argument
1306   that is the name of a file to include literally in the current segment.
1307
1308   Example:
1309
1310   <tscreen><verb>
1311         .incbin         "sprites.dat"
1312   </verb></tscreen>
1313
1314
1315 <tag><tt><idx>.INCLUDE</idx></tt></tag>
1316
1317   Include another file. Include files may be nested up to a depth of 16.
1318
1319   Example:
1320
1321   <tscreen><verb>
1322         .include        "subs.inc"
1323   </verb></tscreen>
1324
1325
1326 <tag><tt><idx>.INITIALIZER</idx></tt></tag>
1327
1328   Export a symbol and mark it as an initializer. This may be used together
1329   with the linker to build a table of initializer subroutines that are called
1330   by the startup code. 
1331
1332   Note: The linker has a feature to build a table of initializer routines, but
1333   it is your code that must call these routines, so just declaring a symbol
1334   as initializer does nothing by itself.
1335
1336   An initializer is always exported as an absolute (16 bit) symbol. It may
1337   have an optional initializer priority that is separated by a comma. If no
1338   priority is given, the default priority of 24 is used. Priority values 1-15
1339   are reserved for the runtime and C libraries of the cc65 C compiler.
1340
1341   Example:
1342
1343   <tscreen><verb>
1344         .initializer    ModuleInit
1345         .initializer    ModInit, 16
1346   </verb></tscreen>
1347
1348   See the C runtime libraries for more examples how to use initializers.
1349
1350
1351 <tag><tt><idx>.LEFT</idx></tt></tag>
1352
1353   Builtin function. Extracts the left part of a given token list.
1354
1355   Syntax:
1356
1357   <tscreen><verb>
1358         .LEFT (&lt;int expr&gt;, &lt;token list&gt;)
1359   </verb></tscreen>
1360
1361   The first integer expression gives the number of tokens to extract from
1362   the token list. The second argument is the token list itself.
1363
1364   Example:
1365
1366   To check in a macro if the given argument has a '#' as first token
1367   (immidiate addressing mode), use something like this:
1368
1369   <tscreen><verb>
1370         .macro  ldax    arg
1371                 ...
1372                 .if (.match (.left (1, arg), #))
1373
1374                 ; ldax called with immidiate operand
1375                 ...
1376
1377                 .endif
1378                 ...
1379         .endmacro
1380   </verb></tscreen>
1381
1382   See also the <tt/.MID/ and <tt/.RIGHT/ builtin functions.
1383
1384
1385 <tag><tt><idx>.LINECONT</idx></tt></tag>
1386
1387   Switch on or off line continuations using the backslash character
1388   before a newline. The option is off by default.
1389   Note: Line continuations do not work in a comment. A backslash at the
1390   end of a comment is treated as part of the comment and does not trigger
1391   line continuation.
1392   The command must be followed by a '+' or '-' character to switch the
1393   option on or off respectively.
1394
1395   Example:
1396
1397   <tscreen><verb>
1398         .linecont       +               ; Allow line continuations
1399
1400         lda     \
1401                 #$20                    ; This is legal now
1402   </verb></tscreen>
1403
1404
1405 <tag><tt><idx>.LIST</idx></tt></tag>
1406
1407   Enable output to the listing. The command must be followed by a boolean
1408   switch ("on", "off", "+" or "-") and will enable or disable listing
1409   output.
1410   The option has no effect if the listing is not enabled by the command line
1411   switch -l. If -l is used, an internal counter is set to 1. Lines are output
1412   to the listing file, if the counter is greater than zero, and suppressed if
1413   the counter is zero. Each use of <tt/.LIST/ will increment or decrement the
1414   counter.
1415
1416   Example:
1417
1418   <tscreen><verb>
1419         .list   on              ; Enable listing output
1420   </verb></tscreen>
1421
1422
1423 <tag><tt><idx>.LISTBYTES</idx></tt></tag>
1424
1425   Set, how many bytes are shown in the listing for one source line. The
1426   default is 12, so the listing will show only the first 12 bytes for any
1427   source line that generates more than 12 bytes of code or data.
1428   The directive needs an argument, which is either "unlimited", or an
1429   integer constant in the range 4..255.
1430
1431   Examples:
1432
1433   <tscreen><verb>
1434         .listbytes      unlimited       ; List all bytes
1435         .listbytes      12              ; List the first 12 bytes
1436         .incbin         "data.bin"      ; Include large binary file
1437   </verb></tscreen>
1438
1439
1440 <tag><tt><idx>.LOCAL</idx></tt></tag>
1441
1442   This command may only be used inside a macro definition. It declares a
1443   list of identifiers as local to the macro expansion.
1444
1445   A problem when using macros are labels: Since they don't change their
1446   name, you get a "duplicate symbol" error if the macro is expanded the
1447   second time. Labels declared with <tt/.LOCAL/ have their name mapped to
1448   an internal unique name (<tt/___ABCD__/) with each macro invocation.
1449
1450   Some other assemblers start a new lexical block inside a macro
1451   expansion. This has some drawbacks however, since that will not allow
1452   <em/any/ symbol to be visible outside a macro, a feature that is sometimes
1453   useful. The <tt/.LOCAL/ command is in my eyes a better way to address
1454   the problem.
1455
1456   You get an error when using <tt/.LOCAL/ outside a macro.
1457
1458
1459 <tag><tt><idx>.LOCALCHAR</idx></tt></tag>
1460
1461   Defines the character that start "cheap" local labels. You may use one
1462   of '@' and '?' as start character. The default is '@'.
1463
1464   Cheap local labels are labels that are visible only between two non
1465   cheap labels. This way you can reuse identifiers like "<tt/loop/" without
1466   using explicit lexical nesting.
1467
1468   Example:
1469
1470   <tscreen><verb>
1471         .localchar      '?'
1472
1473         Clear:  lda     #$00            ; Global label
1474         ?Loop:  sta     Mem,y           ; Local label
1475                 dey
1476                 bne     ?Loop           ; Ok
1477                 rts
1478         Sub:    ...                     ; New global label
1479                 bne     ?Loop           ; ERROR: Unknown identifier!
1480   </verb></tscreen>
1481
1482
1483 <tag><tt><idx>.MACPACK</idx></tt></tag>
1484
1485   Insert a predefined macro package. The command is followed by an
1486   identifier specifying the macro package to insert. Available macro
1487   packages are:
1488
1489         generic         Defines generic macros like add and sub.
1490         longbranch      Defines conditional long jump macros.
1491
1492   Including a macro package twice, or including a macro package that
1493   redefines already existing macros will lead to an error.
1494
1495   Example:
1496
1497   <tscreen><verb>
1498         .macpack        longbranch      ; Include macro package
1499
1500                 cmp     #$20            ; Set condition codes
1501                 jne     Label           ; Jump long on condition
1502   </verb></tscreen>
1503
1504   See separate section about macros packages.
1505
1506
1507 <tag><tt><idx>.MAC,</idx> <idx>.MACRO</idx></tt></tag>
1508
1509   Start a classic macro definition. The command is followed by an identifier
1510   (the macro name) and optionally by a comma separated list of identifiers
1511   that are macro parameters.
1512   See separate section about macros.
1513
1514
1515 <tag><tt><idx>.MATCH</idx></tt></tag>
1516
1517   Builtin function. Matches two token lists against each other. This is
1518   most useful within macros, since macros are not stored as strings, but
1519   as lists of tokens.
1520
1521   The syntax is
1522
1523   <tscreen><verb>
1524         .MATCH(&lt;token list #1&gt;, &lt;token list #2&gt;)
1525   </verb></tscreen>
1526
1527   Both token list may contain arbitrary tokens with the exception of the
1528   terminator token (comma resp. right parenthesis) and
1529
1530   <itemize>
1531   <item>end-of-line
1532   <item>end-of-file
1533   </itemize>
1534
1535   Often a macro parameter is used for any of the token lists.
1536
1537   Please note that the function does only compare tokens, not token
1538   attributes. So any number is equal to any other number, regardless of
1539   the actual value. The same is true for strings. If you need to compare
1540   tokens <em/and/ token attributes, use the <tt/.XMATCH/ function.
1541
1542   Example:
1543
1544   Assume the macro <tt/ASR/, that will shift right the accumulator by one, while
1545   honoring the sign bit. The builtin processor instructions will allow an
1546   optional "A" for accu addressing for instructions like <tt/ROL/ and <tt/ROR/.
1547   We will use the <tt/.MATCH/ function to check for this and print and error
1548   for invalid calls.
1549
1550   <tscreen><verb>
1551         .macro  asr     arg
1552
1553                 .if (.not .blank(arg)) .and (.not .match (arg, a))
1554                 .error "Syntax error"
1555                 .endif
1556
1557                 cmp     #$80            ; Bit 7 into carry
1558                 lsr     a               ; Shit carry into bit 7
1559
1560         .endmacro
1561   </verb></tscreen>
1562
1563   The macro will only accept no arguments, or one argument that must be the
1564   reserved keyword "A".
1565
1566
1567 <tag><tt><idx>.MID</idx></tt></tag>
1568
1569   Builtin function. Takes a starting index, a count and a token list as
1570   arguments. Will return part of the token list.
1571
1572   Syntax:
1573
1574   <tscreen><verb>
1575         .MID (&lt;int expr&gt;, &lt;int expr&gt;, &lt;token list&gt;)
1576   </verb></tscreen>
1577
1578   The first integer expression gives the starting token in the list (the
1579   first token has index 0). The second integer expression gives the number
1580   of tokens to extract from the token list. The third argument is the
1581   token list itself.
1582
1583   Example:
1584
1585   To check in a macro if the given argument has a '<tt/#/' as first token
1586   (immidiate addressing mode), use something like this:
1587
1588     <tscreen><verb>
1589         .macro  ldax    arg
1590                 ...
1591                 .if (.match (.mid (0, 1, arg), #))
1592
1593                 ; ldax called with immidiate operand
1594                 ...
1595
1596                 .endif
1597                 ...
1598         .endmacro
1599   </verb></tscreen>
1600
1601   See also the <tt/.LEFT/ and <tt/.RIGHT/ builtin functions.
1602
1603
1604 <tag><tt><idx>.ORG</idx></tt></tag>
1605
1606   Start a section of absolute code. The command is followed by a constant
1607   expression that gives the new PC counter location for which the code is
1608   assembled. Use <tt/.RELOC/ to switch back to relocatable code.
1609
1610   You may not switch segments while inside a section of absolute code.
1611
1612   Example:
1613
1614   <tscreen><verb>
1615         .org    $7FF            ; Emit code starting at $7FF
1616   </verb></tscreen>
1617
1618
1619 <tag><tt><idx>.OUT</idx></tt></tag>
1620
1621   Output a string to the console without producing an error. This command
1622   is similiar to <tt/.ERROR/, however, it does not force an assembler error
1623   that prevents the creation of an object file.
1624
1625   Example:
1626
1627   <tscreen><verb>
1628         .out    "This code was written by the codebuster(tm)"
1629   </verb></tscreen>
1630
1631   See also the <tt/.WARNING/ and <tt/.ERROR/ directives.
1632
1633
1634 <tag><tt><idx>.P02</idx></tt></tag>
1635
1636   Enable the 6502 instruction set, disable 65C02 and 65816 instructions.
1637   This is the default if not overridden by the <tt/--cpu/ command line
1638   option.
1639
1640
1641 <tag><tt><idx>.P816</idx></tt></tag>
1642
1643   Enable the 65816 instruction set. This is a superset of the 65C02 and
1644   6502 instruction sets.
1645
1646
1647 <tag><tt><idx>.PAGELEN,</idx> <idx>.PAGELENGTH</idx></tt></tag>
1648
1649   Set the page length for the listing. Must be followed by an integer
1650   constant. The value may be "unlimited", or in the range 32 to 127. The
1651   statement has no effect if no listing is generated. The default value
1652   is -1 but may be overridden by the <tt/--pagelength/ command line option.
1653   Beware: Since the listing is generated after assembly is complete, you
1654   cannot use multiple line lengths with one source. Instead, the value
1655   set with the last <tt/.PAGELENGTH/ is used.
1656
1657   Examples:
1658
1659   <tscreen><verb>
1660         .pagelength     66              ; Use 66 lines per listing page
1661
1662         .pagelength     unlimited       ; Unlimited page length
1663   </verb></tscreen>
1664
1665
1666 <tag><tt><idx>.PARAMCOUNT</idx></tt></tag>
1667
1668   This builtin pseudo variable is only available in macros. It is replaced
1669   by the actual number of parameters that were given in the macro
1670   invocation.
1671
1672   Example:
1673
1674   <tscreen><verb>
1675         .macro  foo     arg1, arg2, arg3
1676         .if     .paramcount <> 3
1677         .error  "Too few parameters for macro foo"
1678         .endif
1679         ...
1680         .endmacro
1681   </verb></tscreen>
1682
1683
1684 <tag><tt><idx>.PC02</idx></tt></tag>
1685
1686   Enable the 65C02 instructions set. This instruction set includes all
1687   6502 instructions.
1688
1689
1690 <tag><tt><idx>.PROC</idx></tt></tag>
1691
1692   Start a nested lexical level. All new symbols from now on are in the
1693   local lexical level and are not accessible from outside. Symbols defined
1694   outside this local level may be accessed as long as their names are not
1695   used for new symbols inside the level. Symbols names in other lexical
1696   levels do not clash, so you may use the same names for identifiers. The
1697   lexical level ends when the <tt/.ENDPROC/ command is read. Lexical levels
1698   may be nested up to a depth of 16.
1699
1700   The command may be followed by an identifier, in this case the
1701   identifier is declared in the outer level as a label having the value of
1702   the program counter at the start of the lexical level.
1703
1704   Note: Macro names are always in the global level and in a separate name
1705   space. There is no special reason for this, it's just that I've never
1706   had any need for local macro definitions.
1707
1708   Example:
1709
1710   <tscreen><verb>
1711         .proc   Clear           ; Define Clear subroutine, start new level
1712                 lda     #$00
1713         L1:     sta     Mem,y   ; L1 is local and does not cause a
1714                                 ; duplicate symbol error if used in other
1715                                 ; places
1716                 dey
1717                 bne     L1      ; Reference local symbol
1718                 rts
1719         .endproc                ; Leave lexical level
1720   </verb></tscreen>
1721
1722
1723 <tag><tt><idx>.REF,</idx> <idx>.REFERENCED</idx></tt></tag>
1724
1725   Builtin function. The function expects an identifier as argument in
1726   braces. The argument is evaluated, and the function yields "true" if the
1727   identifier is a symbol that has already been referenced somewhere in the
1728   source file up to the current position. Otherwise the function yields
1729   false. As an example, the <tt/.IFREF/ statement may be replaced by
1730
1731   <tscreen><verb>
1732         .if     .referenced(a)
1733   </verb></tscreen>
1734
1735
1736 <tag><tt><idx>.REPEAT</idx></tt></tag>
1737
1738   Repeat all commands between <tt/.REPEAT/ and <tt/.ENDREPEAT/ a constant
1739   number of times. The command is followed by a constant expression that tells
1740   how many times the commands in the body should get repeated. Optionally, a
1741   comma and an identifier may be specified. If this identifier is found in
1742   the body of the repeat statement, it is replaced by the current repeat
1743   count (starting with zero for the first time the body is repeated).
1744
1745   <tt/.REPEAT/ statements may be nested. If you use the same repeat count
1746   identifier for a nested <tt/.REPEAT/ statement, the one from the inner
1747   level will be used, not the one from the outer level.
1748
1749   Example:
1750
1751   The following macro will emit a string that is "encrypted" in that all
1752   characters of the string are XORed by the value $55.
1753
1754   <tscreen><verb>
1755         .macro  Crypt   Arg
1756                 .repeat strlen(Arg), I
1757                 .byte   strat(Arg, I) .xor $55
1758                 .endrep
1759         .endmacro
1760   </verb></tscreen>
1761
1762
1763 <tag><tt><idx>.RELOC</idx></tt></tag>
1764
1765   Switch back to relocatable mode. See the <tt/.ORG/ command.
1766
1767
1768 <tag><tt><idx>.RES</idx></tt></tag>
1769
1770   Reserve storage. The command is followed by one or two constant
1771   expressions. The first one is mandatory and defines, how many bytes of
1772   storage should be defined. The second, optional expression must by a
1773   constant byte value that will be used as value of the data. If there
1774   is no fill value given, the linker will use the value defined in the
1775   linker configuration file (default: zero).
1776
1777   Example:
1778
1779   <tscreen><verb>
1780         ; Reserve 12 bytes of memory with value $AA
1781         .res    12, $AA
1782   </verb></tscreen>
1783
1784
1785 <tag><tt><idx>.RIGHT</idx></tt></tag>
1786
1787   Builtin function. Extracts the right part of a given token list.
1788
1789   Syntax:
1790
1791   <tscreen><verb>
1792         .RIGHT (&lt;int expr&gt;, &lt;token list&gt;)
1793   </verb></tscreen>
1794
1795   The first integer expression gives the number of tokens to extract from
1796   the token list. The second argument is the token list itself.
1797
1798   See also the <tt/.LEFT/ and <tt/.MID/ builtin functions.
1799
1800
1801 <tag><tt><idx>.RODATA</idx></tt></tag>
1802
1803   Switch to the RODATA segment. The name of the RODATA segment is always
1804   "RODATA", so this is a shortcut for
1805
1806   <tscreen><verb>
1807         .segment  "RODATA"
1808   </verb></tscreen>
1809
1810   The RODATA segment is a segment that is used by the compiler for
1811   readonly data like string constants. See also the <tt/.SEGMENT/ command.
1812
1813
1814 <tag><tt><idx>.SEGMENT</idx></tt></tag>
1815
1816   Switch to another segment. Code and data is always emitted into a
1817   segment, that is, a named section of data. The default segment is
1818   "CODE". There may be up to 254 different segments per object file
1819   (and up to 65534 per executable). There are shortcut commands for
1820   the most common segments ("CODE", "DATA" and "BSS").
1821
1822   The command is followed by a string containing the segment name (there
1823   are some constraints for the name - as a rule of thumb use only those
1824   segment names that would also be valid identifiers). There may also be
1825   an optional attribute separated by a comma. Valid attributes are
1826   "<tt/zeropage/" and "<tt/absolute/".
1827
1828   When specifying a segment for the first time, "absolute" is the
1829   default. For all other uses, the attribute specified the first time
1830   is the default.
1831
1832   "absolute" means that this is a segment with absolute addressing. That
1833   is, the segment will reside somewhere in core memory outside the zero
1834   page. "zeropage" means the opposite: The segment will be placed in the
1835   zero page and direct (short) addressing is possible for data in this
1836   segment.
1837
1838   Beware: Only labels in a segment with the zeropage attribute are marked
1839   as reachable by short addressing. The `*' (PC counter) operator will
1840   work as in other segments and will create absolute variable values.
1841
1842   Example:
1843
1844   <tscreen><verb>
1845         .segment "ROM2"                 ; Switch to ROM2 segment
1846         .segment "ZP2", zeropage        ; New direct segment
1847         .segment "ZP2"                  ; Ok, will use last attribute
1848         .segment "ZP2", absolute        ; Error, redecl mismatch
1849   </verb></tscreen>
1850
1851
1852 <tag><tt><idx>.SMART</idx></tt></tag>
1853
1854   Switch on or off smart mode. The command must be followed by a '+' or
1855   '-' character to switch the option on or off respectively. The default
1856   is off (that is, the assembler doesn't try to be smart), but this
1857   default may be changed by the -s switch on the command line.
1858
1859   In smart mode the assembler will track usage of the <tt/REP/ and <tt/SEP/
1860   instructions in 65816 mode and update the operand sizes accordingly. If
1861   the operand of such an instruction cannot be evaluated by the assembler
1862   (for example, because the operand is an imported symbol), a warning is
1863   issued. Beware: Since the assembler cannot trace the execution flow this
1864   may lead to false results in some cases. If in doubt, use the <tt/.Inn/ and
1865   <tt/.Ann/ instructions to tell the assembler about the current settings.
1866
1867   Example:
1868
1869   <tscreen><verb>
1870         .smart                          ; Be smart
1871         .smart  -                       ; Stop being smart
1872   </verb></tscreen>
1873
1874
1875 <tag><tt><idx>.STRAT</idx></tt></tag>
1876
1877   Builtin function. The function accepts a string and an index as
1878   arguments and returns the value of the character at the given position
1879   as an integer value. The index is zero based.
1880
1881   Example:
1882
1883   <tscreen><verb>
1884         .macro  M       Arg
1885                 ; Check if the argument string starts with '#'
1886                 .if (.strat (Arg, 0) = '#')
1887                 ...
1888                 .endif
1889         .endmacro
1890   </verb></tscreen>
1891
1892
1893 <tag><tt><idx>.STRING</idx></tt></tag>
1894
1895   Builtin function. The function accepts an argument in braces and
1896   converts this argument into a string constant. The argument may be an
1897   identifier, or a constant numeric value.
1898   Since you can use a string in the first place, the use of the function
1899   may not be obvious. However, it is useful in macros, or more complex
1900   setups.
1901
1902   Example:
1903
1904   <tscreen><verb>
1905         ; Emulate other assemblers:
1906         .macro  section name
1907                 .segment        .string(name)
1908         .endmacro
1909   </verb></tscreen>
1910
1911
1912 <tag><tt><idx>.STRLEN</idx></tt></tag>
1913
1914   Builtin function. The function accepts a string argument in braces and
1915   eveluates to the length of the string.
1916
1917   Example:
1918
1919   The following macro encodes a string as a pascal style string with
1920   a leading length byte.
1921
1922   <tscreen><verb>
1923         .macro  PString Arg
1924                 .byte   .strlen(Arg), Arg
1925         .endmacro
1926   </verb></tscreen>
1927
1928
1929 <tag><tt><idx>.TCOUNT</idx></tt></tag>
1930
1931   Builtin function. The function accepts a token list in braces. The
1932   function result is the number of tokens given as argument.
1933
1934   Example:
1935
1936   The <tt/ldax/ macro accepts the '#' token to denote immidiate addressing (as
1937   with the normal 6502 instructions). To translate it into two separate 8 bit
1938   load instructions, the '#' token has to get stripped from the argument:
1939
1940   <tscreen><verb>
1941         .macro  ldax    arg
1942                 .if (.match (.mid (0, 1, arg), #))
1943                 ; ldax called with immidiate operand
1944                 lda     #<(.right (.tcount (arg)-1, arg))
1945                 ldx     #>(.right (.tcount (arg)-1, arg))
1946                 .else
1947                 ...
1948                 .endif
1949         .endmacro
1950   </verb></tscreen>
1951
1952
1953 <tag><tt><idx>.WARNING</idx></tt></tag>
1954
1955   Force an assembly warning. The assembler will output a warning message
1956   preceeded by "User warning". This warning will always be output, even
1957   if other warnings are disabled with the <tt/-W0/ command line option.
1958
1959   This command may be used to output possible problems when assembling
1960   the source file.
1961
1962   Example:
1963
1964   <tscreen><verb>
1965         .macro  jne     target
1966                 .local L1
1967                 .ifndef target
1968                 .warning "Forward jump in jne, cannot optimize!"
1969                 beq     L1
1970                 jmp     target
1971         L1:
1972                 .else
1973                 ...
1974                 .endif
1975         .endmacro
1976   </verb></tscreen>
1977
1978   See also the <tt/.ERROR/ and <tt/.OUT/ directives.
1979
1980
1981 <tag><tt><idx>.WORD</idx></tt></tag>
1982
1983   Define word sized data. Must be followed by a sequence of (word ranged,
1984   but not necessarily constant) expressions.
1985
1986   Example:
1987
1988   <tscreen><verb>
1989         .word   $0D00, $AF13, _Clear
1990   </verb></tscreen>
1991
1992
1993 <tag><tt><idx>.ZEROPAGE</idx></tt></tag>
1994
1995   Switch to the ZEROPAGE segment and mark it as direct (zeropage) segment.
1996   The name of the ZEROPAGE segment is always "ZEROPAGE", so this is a
1997   shortcut for
1998
1999   <tscreen><verb>
2000         .segment  "ZEROPAGE", zeropage
2001   </verb></tscreen>
2002
2003   Because of the "zeropage" attribute, labels declared in this segment are
2004   addressed using direct addressing mode if possible. You <em/must/ instruct
2005   the linker to place this segment somewhere in the address range 0..$FF
2006   otherwise you will get errors.
2007
2008 </descrip>
2009
2010
2011
2012 <sect>Macros
2013
2014 <p>
2015 Macros may be thought of as "parametrized super instructions". Macros are
2016 sequences of tokens that have a name. If that name is used in the source
2017 file, the macro is "expanded", that is, it is replaced by the tokens that
2018 were specified when the macro was defined.
2019
2020 In it's simplest form, a macro does not have parameters. Here's an
2021 example:
2022
2023 <tscreen><verb>
2024         .macro  asr             ; Arithmetic shift right
2025                 cmp     #$80    ; Put bit 7 into carry
2026                 ror             ; Rotate right with carry
2027         .endmacro
2028 </verb></tscreen>
2029
2030 The macro above consists of two real instructions, that are inserted into
2031 the code, whenever the macro is expanded. Macro expansion is simply done
2032 by using the name, like this:
2033
2034 <tscreen><verb>
2035         lda     $2010
2036         asr
2037         sta     $2010
2038 </verb></tscreen>
2039
2040 When using macro parameters, macros can be even more useful:
2041
2042 <tscreen><verb>
2043         .macro  inc16   addr
2044                 clc
2045                 lda     addr
2046                 adc     #$01
2047                 sta     addr
2048                 lda     addr+1
2049                 adc     #$00
2050                 sta     addr+1
2051         .endmacro
2052 </verb></tscreen>
2053
2054 When calling the macro, you may give a parameter, and each occurence of
2055 the name "addr" in the macro definition will be replaced by the given
2056 parameter. So
2057
2058 <tscreen><verb>
2059         inc16   $1000
2060 </verb></tscreen>
2061
2062 will be expanded to
2063
2064 <tscreen><verb>
2065                 clc
2066                 lda     $1000
2067                 adc     #$01
2068                 sta     $1000
2069                 lda     $1000+1
2070                 adc     #$00
2071                 sta     $1000+1
2072 </verb></tscreen>
2073
2074 A macro may have more than one parameter, in this case, the parameters
2075 are separated by commas. You are free to give less parameters than the
2076 macro actually takes in the definition. You may also leave intermediate
2077 parameters empty. Empty parameters are replaced by empty space (that is,
2078 they are removed when the macro is exanded). If you have a look at our
2079 macro definition above, you will see, that replacing the "addr" parameter
2080 by nothing will lead to wrong code in most lines. To help you, writing
2081 macros with a variable parameter list, there are some control commands:
2082
2083 <tt/.IFBLANK/ tests the rest of the line and returns true, if there are any
2084 tokens on the remainder of the line. Since empty parameters are replaced by
2085 nothing, this may be used to test if a given parameter is empty.
2086 <tt/.IFNBLANK/ tests the opposite.
2087
2088 Look at this example:
2089
2090 <tscreen><verb>
2091         .macro  ldaxy   a, x, y
2092         .ifnblank       a
2093                 lda     #a
2094         .endif
2095         .ifnblank       x
2096                 ldx     #x
2097         .endif
2098         .ifnblank       y
2099                 ldy     #y
2100         .endif
2101         .endmacro
2102 </verb></tscreen>
2103
2104 This macro may be called as follows:
2105
2106 <tscreen><verb>
2107         ldaxy   1, 2, 3         ; Load all three registers
2108
2109         ldaxy   1, , 3          ; Load only a and y
2110
2111         ldaxy   , , 3           ; Load y only
2112 </verb></tscreen>
2113
2114 There's another helper command for determining, which macro parameters are
2115 valid: <tt/.PARAMCOUNT/. This command is replaced by the parameter count
2116 given, <em/including/ intermediate empty macro parameters:
2117
2118 <tscreen><verb>
2119         ldaxy   1               ; .PARAMCOUNT = 1
2120         ldaxy   1,,3            ; .PARAMCOUNT = 3
2121         ldaxy   1,2             ; .PARAMCOUNT = 2
2122         ldaxy   1,              ; .PARAMCOUNT = 2
2123         ldaxy   1,2,3           ; .PARAMCOUNT = 3
2124 </verb></tscreen>
2125
2126 Macros may be used recursively:
2127
2128 <tscreen><verb>
2129         .macro  push    r1, r2, r3
2130                 lda     r1
2131                 pha
2132         .if     .paramcount > 1
2133                 push    r2, r3
2134         .endif
2135         .endmacro
2136 </verb></tscreen>
2137
2138 There's also a special macro to help writing recursive macros:
2139 <tt/.EXITMACRO/. This command will stop macro expansion immidiately:
2140
2141 <tscreen><verb>
2142         .macro  push    r1, r2, r3, r4, r5, r6, r7
2143         .ifblank        r1
2144                 ; First parameter is empty
2145                 .exitmacro
2146         .else
2147                 lda     r1
2148                 pha
2149         .endif
2150                 push    r2, r3, r4, r5, r6, r7
2151         .endmacro
2152 </verb></tscreen>
2153
2154 When expanding this macro, the expansion will push all given parameters
2155 until an empty one is encountered. The macro may be called like this:
2156
2157 <tscreen><verb>
2158         push    $20, $21, $32           ; Push 3 ZP locations
2159         push    $21                     ; Push one ZP location
2160 </verb></tscreen>
2161
2162 Now, with recursive macros, <tt/.IFBLANK/ and <tt/.PARAMCOUNT/, what else do
2163 you need? Have a look at the inc16 macro above. Here is it again:
2164
2165 <tscreen><verb>
2166         .macro  inc16   addr
2167                 clc
2168                 lda     addr
2169                 adc     #$01
2170                 sta     addr
2171                 lda     addr+1
2172                 adc     #$00
2173                 sta     addr+1
2174         .endmacro
2175 </verb></tscreen>
2176
2177 If you have a closer look at the code, you will notice, that it could be
2178 written more efficiently, like this:
2179
2180 <tscreen><verb>
2181         .macro  inc16   addr
2182                 clc
2183                 lda     addr
2184                 adc     #$01
2185                 sta     addr
2186                 bcc     Skip
2187                 inc     addr+1
2188         Skip:
2189         .endmacro
2190 </verb></tscreen>
2191
2192 But imagine what happens, if you use this macro twice? Since the label
2193 "Skip" has the same name both times, you get a "duplicate symbol" error.
2194 Without a way to circumvent this problem, macros are not as useful, as
2195 they could be. One solution is, to start a new lexical block inside the
2196 macro:
2197
2198 <tscreen><verb>
2199         .macro  inc16   addr
2200         .proc
2201                 clc
2202                 lda     addr
2203                 adc     #$01
2204                 sta     addr
2205                 bcc     Skip
2206                 inc     addr+1
2207         Skip:
2208         .endproc
2209         .endmacro
2210 </verb></tscreen>
2211
2212 Now the label is local to the block and not visible outside. However,
2213 sometimes you want a label inside the macro to be visible outside. To make
2214 that possible, there's a new command that's only usable inside a macro
2215 definition: <tt/.LOCAL/. <tt/.LOCAL/ declares one or more symbols as local to
2216 the macro expansion. The names of local variables are replaced by a unique
2217 name in each separate macro expansion. So we could also solve the problem
2218 above by using <tt/.LOCAL/:
2219
2220 <tscreen><verb>
2221         .macro  inc16   addr
2222                 .local  Skip            ; Make Skip a local symbol
2223                 clc
2224                 lda     addr
2225                 adc     #$01
2226                 sta     addr
2227                 bcc     Skip
2228                 inc     addr+1
2229         Skip:                           ; Not visible outside
2230         .endmacro
2231 </verb></tscreen>
2232
2233 Starting with version 2.5 of the assembler, there is a second macro type
2234 available: C style macros using the <tt/.DEFINE/ directive. These macros are
2235 similar to the classic macro type speified above, but behaviour is sometimes
2236 different:
2237
2238 <itemize>
2239
2240 <item>  Macros defined with <tt/.DEFINE/ may not span more than a line. You
2241         may use line continuation (see <tt/.LINECONT/) to spread the
2242         definition over more than one line for increased readability, but the
2243         macro itself does not contain an end-of-line token.
2244
2245 <item>  Macros defined with <tt/.DEFINE/ share the name space with classic
2246         macros, but they are detected and replaced at the scanner level. While
2247         classic macros may be used in every place, where a mnemonic or other
2248         directive is allowed, <tt/.DEFINE/ style macros are allowed anywhere
2249         in a line. So they are more versatile in some situations.
2250
2251 <item>  <tt/.DEFINE/ style macros may take parameters. While classic macros
2252         may have empty parameters, this is not true for <tt/.DEFINE/ style
2253         macros. For this macro type, the number of actual parameters must
2254         match exactly the number of formal parameters.
2255
2256         To make this possible, formal parameters are enclosed in braces when
2257         defining the macro. If there are no parameters, the empty braces may
2258         be omitted.
2259
2260 <item>  Since <tt/.DEFINE/ style macros may not contain end-of-line tokens,
2261         there are things that cannot be done. They may not contain several
2262         processor instructions for example. So, while some things may be done
2263         with both macro types, each type has special usages. The types
2264         complement each other.
2265
2266 </itemize>
2267
2268 Let's look at a few examples to make the advantages and disadvantages
2269 clear.
2270
2271 To emulate assemblers that use "<tt/EQU/" instead of "<tt/=/" you may use the
2272 following <tt/.DEFINE/:
2273
2274 <tscreen><verb>
2275         .define EQU     =
2276
2277         foo     EQU     $1234           ; This is accepted now
2278 </verb></tscreen>
2279
2280 You may use the directive to define string constants used elsewhere:
2281
2282 <tscreen><verb>
2283         ; Define the version number
2284         .define VERSION         "12.3a"
2285
2286         ; ... and use it
2287         .asciiz VERSION
2288 </verb></tscreen>
2289
2290 Macros with parameters may also be useful:
2291
2292 <tscreen><verb>
2293         .define DEBUG(message)  .out    message
2294
2295         DEBUG   "Assembling include file #3"
2296 </verb></tscreen>
2297
2298 Note that, while formal parameters have to be placed in braces, this is
2299 not true for the actual parameters. Beware: Since the assembler cannot
2300 detect the end of one parameter, only the first token is used. If you
2301 don't like that, use classic macros instead:
2302
2303 <tscreen><verb>
2304         .macro  message
2305                 .out    message
2306         .endmacro
2307 </verb></tscreen>
2308
2309 (This is an example where a problem can be solved with both macro types).
2310
2311
2312
2313 <sect>Macro packages
2314
2315 <p>
2316 Using the <tt/.MACPACK/ directive, predefined macro packages may be included
2317 with just one command. Available macro packages are:
2318
2319 <descrip>
2320
2321 <tag><tt><idx>generic</idx></tt></tag>
2322
2323   This macro package defines macros that are useful in almost any program.
2324   Currently, two macros are defined:
2325
2326   <tscreen><verb>
2327         .macro  add     Arg
2328                 clc
2329                 adc     Arg
2330         .endmacro
2331
2332         .macro  sub     Arg
2333                 sec
2334                 sbc     Arg
2335         .endmacro
2336   </verb></tscreen>
2337
2338
2339 <tag><tt><idx>longbranch</idx></tt></tag>
2340
2341   This macro package defines long conditional jumps. They are named like the
2342   short counterpart but with the 'b' replaced by a 'j'. Here is a sample
2343   definition for the "<tt/jeq/" macro, the other macros are built using the
2344   same scheme:
2345
2346   <tscreen><verb>
2347         .macro  jeq     Target
2348                 .if     .def(Target) .and ((*+2)-(Target) <= 127)
2349                 beq     Target
2350                 .else
2351                 bne     *+5
2352                 jmp     Target
2353                 .endif
2354         .endmacro
2355   </verb></tscreen>
2356
2357   All macros expand to a short branch, if the label is already defined (back
2358   jump) and is reachable with a short jump. Otherwise the macro expands to a
2359   conditional branch with the branch condition inverted, followed by an
2360   absolute jump to the actual branch target.
2361
2362   The package defines the following macros:
2363
2364   <tscreen><verb>
2365         jeq, jne, jmi, jpl, jcs, jcc, jvs, jvc
2366   </verb></tscreen>
2367
2368 </descrip>
2369
2370
2371 <sect>Bugs/Feedback<p>
2372
2373 If you have problems using the assembler, if you find any bugs, or if
2374 you're doing something interesting with the assembler, I would be glad to
2375 hear from you. Feel free to contact me by email
2376 (<htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">).
2377
2378
2379
2380 <sect>Copyright<p>
2381
2382 ca65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
2383 Bassewitz. For usage of the binaries and/or sources the following
2384 conditions do apply:
2385
2386 This software is provided 'as-is', without any expressed or implied
2387 warranty.  In no event will the authors be held liable for any damages
2388 arising from the use of this software.
2389
2390 Permission is granted to anyone to use this software for any purpose,
2391 including commercial applications, and to alter it and redistribute it
2392 freely, subject to the following restrictions:
2393
2394 <enum>
2395 <item>  The origin of this software must not be misrepresented; you must not
2396         claim that you wrote the original software. If you use this software
2397         in a product, an acknowledgment in the product documentation would be
2398         appreciated but is not required.
2399 <item>  Altered source versions must be plainly marked as such, and must not
2400         be misrepresented as being the original software.
2401 <item>  This notice may not be removed or altered from any source
2402         distribution.
2403 </enum>
2404
2405
2406
2407 </article>
2408
2409
2410