Debian Codegraph @

docs/sources/manual.tex

raw
1 \begin{savequote}
2 \sffamily
3 ``Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.''
4 \qauthor{Martin Golding}
5
6
7 \end{savequote}
8
9
10 \chapter{Source Code}
11 \index{Source Code}
12 \label{changelog:20200908source}
13
14 \section{Encoding}
15 \index{Source Code!Format!Encoding}
16
17 Source code files are written as plain-text (generally ASCII encoding) files.
18
19 \index{Source Code!Format!End-of-Line}
20 Both Windows-style (carriage-return, line-feed) and Unix-style (line-feed) line endings are supported by \dasm.
21
22 \section{Comments}
23 \index{Comments}
24 \index{Source Code!Comments}
25 \label{changelog:20200909comment}
26
27 \dasm supports two different comment styles: traditional assembler single-line semicolon-delimited comments, and C-style ``\mono{/* ... */}'' delimited multiline comments.
28
29 \index{Source Code!Format!Unicode}
30 Note that comments may contain Unicode characters, as \dasm effectively ignores the contents of comments, but assembled code cannot contain Unicode.
31
32 An open comment directive anywhere on a line ("\mono{;}" or "\mono{/*}") terminates \dasm's label/directive/instruction parser for the rest of that line.
33
34 \subsection{Assembler-style Comments: ;...}
35 \index{Source Code!Comments!Assembler-style}
36 \index{Comments!Assembler-style}
37
38 The presence of a semicolon at any character in a line will flag the rest of that line (semicolon included) as a comment. It is common when writing code to place a semicolon at the beginning of lines to disable them from being assembled.
39
40 There may be valid labels, directives, and/or instructions before the comment. These will be correctly assembled.
41
42 \subsubsection{Examples}
43 \index{Examples!Comment Styles}
44 \begin{code}
45 ; this is a comment
46 ; and so is this
47 label ; this is a label followed by a comment
48 ;label this is a comment, NOT a label
49 lda #2 ; <-- this instruction IS assembled
50 ;lda #2 <-- this instruction is NOT assembled
51 \end{code}
52
53 \begin{code}
54 MAC TEST ;{1}=value <-- a comment showing params
55 ENDM
56 \end{code}
57
58 \subsection{C-style Comments: /* ... */ }
59 \index{Source Code!Comments!C-style}
60 \index{Comments!C-style}
61
62 C-style comments mark all text between and including \mono{/*} and \mono{*/} as a comment.
63
64 C-style comments make it easy to disable the assembly of blocks of code spanning multiple lines.
65
66 \subsubsection{Correct Examples}
67 \begin{code}
68 /* This single line comment is valid */
69 lda #1 /* And this comment is valid too */
70
71 /* And so is this
72 Multiline comment */
73
74
75 \end{code}
76
77 It can sometimes be useful to enable/disable multi-line comments with a semicolon...
78
79 \begin{code}
80 ;/*
81 lda #2 ;<-- comment disabled. This IS assembled
82 ;*/
83
84 /*
85 lda #2 ;<-- comment enabled. This is NOT assembled
86 */
87 \end{code}
88
89 \subsection{Commenting Out Large Blocks}
90 \index{Source Code!Comments!Disabling Large Blocks}
91 \index{Comments!Disabling Large Blocks}
92
93 There are several ways to disable ("comment-out") large parts of the source code.
94
95 \begin{itemize}
96 \item Place a semicolon at the start of every line of the code to disable (Assembler-style)
97 \item Bracket the part to disable with \mono{/*} and \mono{*/} (C-style)
98 \item Surround the block with the conditionals "\mono{IF 0}" at the start, followed by "\mono{ENDIF}" at the end. To re-enable the code, change the \mono{0} to \mono{1}. Note that this method is may fail if the enclosed code has conditionals.
99 \end{itemize}
100
101
102
103 \chapter{Command-Line}
104 \index{Command Line}
105 %\index{AUV!\emph{dasm}}
106 %\label{chap:Starbug}
107
108 %\lstset{basicstyle=\linespread{0.5}}
109
110 % {\centering
111 % \fbox{\includegraphics[width=5in]{dasm-logo.png}}}
112
113
114
115
116 \section{Usage}
117 \index{Command Line!Usage}
118 \dasm is a command-line tool. It parses the command-line for the input source file, which must be present, and optional assemble-time options, assembles the source file, and produces outputs as specified in the options. The source file must be ASCII-encoded, but comments may contain Unicode characters.
119
120 One option you are most likely to need is \nameref{flag:outputfile} to specify the binary file for output.
121
122 The assembler will return 0 on successful compilation, 1 otherwise.
123
124 \begin{usage}
125 dasm
126 dasm sourcefile
127 dasm sourcefile [option ...]
128 \end{usage}
129
130 \label{changelog:20200824sourcefile}
131 If no \mono{sourcefile} is given, then \dasm will output a short help message, and exit with an assembly error. Otherise, the \mono{sourcefile} becomes the file that \dasm will assemble, and any further parameters are parsed as options.
132
133 \label{changelog:20200831help}
134 \begin{outputx2}
135 % ./dasm
136 DASM 2.20.14
137 Copyright (c) 1988-2020 by the DASM team.
138 License GPLv2+: GNU GPL version 2 or later (see file LICENSE).
139 DASM is free software: you are free to change and redistribute it.
140 There is ABSOLUTELY NO WARRANTY, to the extent permitted by law.
141
142 Usage: dasm sourcefile [options]
143
144 -f# output format 1-3 (default 1)
145 -oname output file name (else a.out)
146 -lname list file name (else none generated)
147 -Lname list file, containing all passes
148 -sname symbol dump file name (else none generated)
149 -v# verboseness 0-4 (default 0)
150 -d debug mode (for developers)
151 -Dsymbol define symbol, set to 0
152 -Dsymbol=expression define symbol, set to expression
153 -Msymbol=expression define symbol using EQM (same as -D)
154 -Idir search directory for INCLUDE and INCBIN
155 -p# maximum number of passes
156 -P# maximum number of passes, with fewer checks
157 -T# symbol table sorting
158 (default 0 = alphabetical, 1 = address/value)
159 -E# error format (default 0 = MS, 1 = Dillon, 2 = GNU)
160 -S strict syntax checking
161 -R remove binary output-file in case of errors
162 -m# max. allowed file-size in kB
163
164 Report bugs on https://github.com/dasm-assembler/dasm please!
165
166
167 Fatal assembly error: Check command-line format.
168 \end{outputx2}
169
170
171
172
173
174 \section{Format}
175 \index{Command Line!Format}
176 \subsection{Spaces in Filenames}
177 \label{changelog:20200906spaces}
178 \index{Command Line!Spaces in Filenames}
179
180 If a filename (source, output, listing file) contains spaces, the whole filename may be surrounded with quotes, or the spaces may be escaped with a backslash character - depending on your OS support for these.
181
182 \begin{usage}
183 dasm "source file.asm"
184 dasm source\ file.asm
185 \end{usage}
186
187
188
189 \subsection{Options}
190
191 Options are specified on the command-line, after the source file. There may be zero or more options each separated by whitespace. Some options require their own parameters. Default values (where an option is not explicitly defined) are described with each option below.
192
193 An option is prefixed by a dash ``\mono{-}'' or a slash ``\mono{/}'' prefix, and followed by the option letter and then the parameter (if there is one). There must be no whitespace between the prefix, option letter or the parameter.
194
195 \subsubsection{Example}
196 \index{Examples!Command Line}
197 \begin{outputx}
198 > dasm source.asm -f2 -oout.bin -llist.txt -v3 -DVER=4
199 \end{outputx}
200
201 This example will assemble the file \mono{source.asm}, using output format \mono{2} (random access segments). The resultant binary will be saved to the file \mono{out.bin} and a listing file written to \mono{list.txt}. During the assembly, verbosity of the output is set to \mono{3} (unresolved and unreferenced symbols displayed every pass). The value of the symbol (which will be available in the source code) \mono{VER} is set to \mono{4}.
202
203 \section{Options}
204 \index{Command Line!Options}
205 \subsection{\texttt{-d Debug}}
206 \index{Command Line!Options!Debug}
207 \begin{usage}
208 -d
209 \end{usage}
210
211 \label{changelog:20200824developers}
212 This option is for \dasm's developers, and is essentially inactive in release versions.
213 \\
214 \hrule
215
216 \subsection{\texttt{-D Define Symbol}}
217 \label{flag:definesymbol}
218 \index{Command Line!Options!Define Symbol}
219 \begin{usage}
220 -Dsymbol=exp
221 \end{usage}
222
223 Defines a symbol and sets it to the expression \mono{exp}.
224
225 Can be used to set values for symbols used inside the code.
226
227 See \nameref{flag:definesymbolF}, \nameref{flag:definesymbolM}.
228
229 \subsubsection{Example}
230
231 \begin{outputx}
232 > dasm source.asm -DSPEED=40
233 \end{outputx}
234
235 \begin{code}
236 lda #SPEED ; will load 40
237 sta velocity
238 \end{code}
239
240 \begin{code}
241 ; Use the symbol to assemble different code
242 IF SPEED < 30
243 jsr FastDraw ; not assembled
244 ELSE
245 jsr SlowDraw ; is assembled
246 ENDIF
247 \end{code}
248
249
250 \hrule
251
252 \subsection{\texttt{-E Error Format}}
253 \index{Command Line!Options!Error Format}
254 \begin{usage}
255 -Eformat
256 \end{usage}
257
258 Sets the format of the output of error information. Many programmers' editing environments (IDEs) are able to monitor the output from tools such as \dasm and parse it for information about errors and warnings. If an IDE is able to resolve file names and line numbers for these errors, then the IDE can provide quick-links to the user to allow ease of editing.
259
260 This option switches the format of error/warning output of \dasm between several ``standard'' formats.
261
262 \begin{table}[H]
263 \begin{tabularx}{\linewidth}{cl}
264 \toprule
265 \mono{\textbf{format}}&\textbf{Result}\\
266 \hline
267 \\
268 \mono{0}&Microsoft (\mono{default})\\
269 \mono{1}&Dillon\\
270 \mono{2}&GNU\\
271 \\
272 \bottomrule
273 \end{tabularx}
274 \end{table}
275
276 \subsection{\texttt{-f Output Format}}
277 \label{flag:outputformat}
278 \index{Command Line!Options!Output Format}
279
280 \begin{usage}
281 -fvalue
282 \end{usage}
283
284 Defines the format used in the binary output file generated by \dasm.
285
286 \begin{table}[H]
287 \begin{tabularx}{\textwidth}{cp{12cm}}
288
289 \toprule
290 \textbf{value} & \textbf{Function}\\
291 \hline
292
293 \\
294
295 \label{changelog:20200824org}
296 \mono{1} & \mono{default}. The output file contains a two byte origin in little-endian order, then
297 data until the end of the file. Any instructions which generate output (within an initialised segment) must do so with an ascending \mono{ORG} address (this address being the offset in the binary/ROM where the output is placed, as opposed to the \mono{RORG} which is the address to which the code is assembled). Initialised segments must occur in ascending order. \\
298
299
300 \\
301 \mono{2} & \mono{RAS} (Random Access Segment). The output file contains one or more hunks. Each hunk consists of a 2 byte origin (little-endian), 2 byte length (little-endian), and that number of data bytes. The hunks occur in the same order as initialized segments in the assembly. There are no restrictions to segment ordering (i.e. reverse indexed ORG statements are allowed). The next hunk begins after the previous hunk's data, until the end of the file.\\
302
303 \\
304 \mono{3} & \mono{RAW}. The output file contains data only (format \#1 without the 2 byte
305 header). Restrictions are the same as for format \#1.
306 \textbf{No} header origin is generated. You get
307 nothing but data.\\
308
309 \\
310 \bottomrule
311 \end{tabularx}
312 \end{table}
313
314 It is a \textbf{common problem} to forget the format option on the command line, especially if you are expecting a pure binary ROM without a header. Use \mono{-f3} if you are assembling ROMs.
315
316 %\hrule
317 \subsection{\texttt{-F Define Symbol}}
318 \label{flag:definesymbolF}
319 \index{Command Line!Options!Define Symbol}
320
321 \begin{usage}
322 -Fsymbol
323 \end{usage}
324
325 Define a symbol and set its value to 0.
326
327 This symbol is then usable in the source code as if it were a part of the code itself. This can be useful for controlling the conditional assembly of parts of code.
328
329 See related options \nameref{flag:definesymbol} and \nameref{flag:definesymbolM}.
330
331 \subsubsection{Example}
332
333 \begin{outputx}
334 > dasm source.asm -FTEST
335 \end{outputx}
336
337 \begin{code}
338 IFCONST TEST
339 ; code here only assembled when TEST is defined
340 ENDIF
341 \end{code}
342
343 \hrule
344 \subsection{\texttt{-I Include Directory}}
345 \label{flag:includedirectory}
346 \index{Command Line!Options!Include Directory}
347
348 \begin{usage}
349 -Idirectory
350 \end{usage}
351
352 This adds the \mono{directory} to the search path \dasm uses when looking for files when it encounters \nameref{pseudoop:include} and \nameref{pseudoop:incbin} directives. Use of this option on the command line is equivalent to an \nameref{pseudoop:incdir} directive placed at the beginning of the source file.
353
354 See \nameref{pseudoop:incdir} for the format of the directory name.\\
355
356 \hrule
357
358 \subsection{\texttt{-l Listing Filename}}
359 \index{Command Line!Options!Listing}
360 \label{flag:listing}
361
362 \begin{usage}
363 -lfilename
364 \end{usage}
365
366 \dasm is able to produce a comprehensive and extremely useful listing of the assembled source code. This file includes symbol values, code locations, and the source code itself. To enable generation of a listing file, use the \mono{-l} option.
367
368 See alternate: \nameref{flag:listingall}.\\
369
370 \hrule
371 \subsection{\texttt{-L Listing Filename (all Passes)}}
372 \label{flag:listingall}
373 \index{Command Line!Options!Listing (all)}
374
375 \begin{usage}
376 -Lfilename
377 \end{usage}
378
379 This option behaves the same as \nameref{flag:listing}, but lists the code on every pass. Warning: this can lead to some very big listings if \dasm needs to perform many passes on your code!
380
381 See alternate: \nameref{flag:listing}.
382
383 \subsection{\texttt{-M Define Symbol}}
384 \label{flag:definesymbolM}
385 \index{Command Line!Options!Define Symbol}
386 \begin{usage}
387 -Msymbol=exp
388 \end{usage}
389
390 Deprecated.
391
392 Defines a symbol and sets it to the expression \mono{exp]}.
393
394 See similar: \nameref{flag:definesymbol}, \nameref{flag:definesymbolF}.\\
395
396 \hrule
397
398 \subsection{\texttt{-m Maximum Output File Size}}
399 \label{flag:maximum}
400 \index{Command Line!Options!Maximum Output Size}
401 \begin{usage}
402 -msize
403 \end{usage}
404
405 \label{changelog:20200908optionm}
406
407 The \mono{-m} switch is not intended to limit the size of the created binary. Instead, it provides a safety-halting mechanism to prevent erroneous/recursive code from creating huge binaries, in situations that may not otherwise be detected.
408
409 The \mono{size} parameter is the halting condition. \dasm will halt with an error if the size of the generated binary exceeds this value. The default maximum size is \mono{640 KiB}.
410
411 This switch is generally useful for architectures with more than \mono{10} memory banks. It can be used in conjunction with \nameref{flag:remove} to detect assembly anomalies and clean-up afterwards.
412
413 \subsubsection{Example}
414 \begin{outputx}
415 > dasm source.asm -orom.bin -m128
416 \end{outputx}
417
418 This example will assemble the file \mono{source.asm} and write the file \mono{rom.bin}, halting with an error if the output binary exceeds \mono{128 KiB} (an indication of a potential recursion error in the code generation).
419
420 See aslo \nameref{flag:remove}.\\
421
422 \hrule
423
424
425 \subsection{\texttt{-o Output File}}
426 \label{flag:outputfile}
427 \index{Command Line!Options!Output File}
428
429 \begin{usage}
430 -ofilename
431 \end{usage}
432
433 Set the name of the filename for the output binary result of the assembly. If no name is specified, \dasm will write to the file ``\mono{a.out}''. See \nameref{flag:outputformat} for the format of the output file. If you want a pure binary output file without headers, you \textbf{must} add option \mono{-f3}.
434
435 \subsubsection{Example}
436 \begin{outputx}
437 > dasm source.asm -orom.bin -f3
438 \end{outputx}
439
440 This example will assemble the file \mono{source.asm} and write the file \mono{rom.bin} with the binary results of the assembly, without header information.\\
441
442
443 \hrule
444 \subsection{\texttt{-p Number of Passes}}
445 \label{flag:passes}
446 \index{Command Line!Options!Passes}
447 \begin{usage}
448 -pvalue
449 \end{usage}
450
451 Sets the maximum number of passes performed by \dasm to \mono{value}.
452
453 \dasm will keep performing passes until all references are resolved, or until the maximum number of passes is reached (in which case it will exit with an unresolved symbol error). Typically on machines these days, \dasm is so fast that a high number of passes is acceptable.
454
455 \label{changelog:20200824passes}
456 The default number of pases is 3.
457
458 See \nameref{flag:passes2}.\\
459
460 \hrule
461 \subsection{\texttt{-P Number of Passes (Fewer Checks)}}
462 \label{flag:passes2}
463 \index{Command Line!Options!Passes (Fewer Checks)}
464
465 \begin{usage}
466 -pvalue
467 \end{usage}
468
469 Sets the maximum number of passes performed by \dasm to \mono{value}.
470
471 This is the same as \nameref{flag:passes}, but instructs \dasm to perform fewer checks.
472
473 \label{todo}
474 {\color{red}And these are...?}
475
476 See \nameref{flag:passes}.\\
477
478 \hrule
479
480
481 \subsection{\texttt{-R Remove Output File}}
482 \label{flag:remove}
483 \index{Command Line!Options!Remove Output}
484 \begin{usage}
485 -R
486 \end{usage}
487
488 \label{changelog:20200908optionR}
489 Removes the binary output file if \dasm encounters an error during assembly.\\
490
491 Can be used in conjunction with \nameref{flag:maximum} to remove erroneous binaries, for example if a ROM size limit is exceeded.
492
493 See aslo \nameref{flag:maximum}.
494 \\
495
496
497 \hrule
498
499 \subsection{\texttt{-s Symbol Table File}}
500 \label{flag:symboltable}
501 \index{Command Line!Options!Symbol Table File}
502
503 \begin{usage}
504 -sfilename
505 \end{usage}
506
507 At the conclusion of assembly, the \mono{-s} option directs \dasm to save a symbol table to the specified file. A symbol table is a table listing all the symbols encountered during an assembly, and their values if known. By default, no symbol table file is generated.
508
509 The symbol table may be sorted alphabetically or numerically with the \nameref{flag:symboltablesort} option.
510
511
512 \subsubsection{Example}
513
514 \begin{outputx}
515 > dasm source.asm -ssource.sym
516 \end{outputx}
517
518 After the execution of the above, the file \mono{source.sym} would contain the symbol table in the format as shown in the example below. Each line gives a symbol name, its final resolved address/value, and a flag field. In the flag field, \mono{(R )} indicates the symbol has been used/referenced and not just defined.
519
520 \begin{code}
521 --- Symbol List (sorted by symbol)
522 AUDC0 0015
523 StartAddress 1000 (R )
524 TIA_BASE_ADDRESS 0000 (R )
525 TIM1T 0294
526 TIM64T 0296
527 TIM8T 0295
528 TIMINT 0285
529 var1 0080
530 var2 0081
531 varn 008b
532 VBLANK 0001
533 VERSION_VCS 0069
534 WSYNC 0002
535 --- End of Symbol List.
536 \end{code}
537 \\
538 \\
539 \hrule
540
541 \subsection{\texttt{-S Strict Syntax Checking}}
542 \label{flags:strictsyntax}
543 \index{Command Line!Options!Strict Syntax Checking}
544
545 \begin{usage}
546 -S
547 \end{usage}
548
549 This option switches on more stringent checking of source code issues.
550
551 Duplicate macro definitions are flagged as errors.
552
553 \index{todo}
554 {\color{red}TODO complete list of strict checks}\\
555
556 \hrule
557
558 \subsection{\texttt{-T Sort Symbol Table}}
559 \label{flag:symboltablesort}
560 \index{Command Line!Options!Sort Symbol Table}
561 \begin{usage}
562 -Tvalue
563 \end{usage}
564
565 Controls the sorting of lines in the symbol table.
566
567 See \nameref{flag:symboltable} to enable symbol table output.
568
569
570 \begin{table}[H]
571 \begin{tabularx}{\textwidth}{cl}
572 \toprule
573 \mono{\textbf{value}}&\textbf{Sort By}\\
574 \hline
575 \\
576 \mono{0}&Symbol Alphabetically (default)\\
577 \mono{1}&Address/Value\\
578 \\
579 \bottomrule
580 \end{tabularx}
581 \end{table}
582
583
584 \subsection{\texttt{-v Verbosity Level}}
585 \index{Command Line!Options!Verbosity Level}
586 \label{flag:verbosity}
587
588 \begin{usage}
589 -vvalue
590 \end{usage}
591
592 The \mono{-v} option controls the amount of information output by \dasm while it is assembling your code. This information includes warnings, errors, a segement table, a symbol table, unresolved and unreferenced symbols, and reasons for performing extra passes. Use of the \mono{-v} option can assist with diagnosing anomalous behaviour.
593
594 \begin{table}[H]
595 \begin{tabularx}{\textwidth}{cl}
596 \toprule
597 \mono{\textbf{value}}&\textbf{Result}\\
598 \hline
599 \\
600 \mono{0}&Only warnings and errors (default)\\
601 \\
602 \mono{1}&Segment table information generated after each pass\\
603 &Include file names are displayed\\
604 &Item statistics on why the assembler is going to make another pass\\
605 &R1,R2 reason code: R3\\
606 &where R1 is the number of times the assembler encountered\\
607 &something requiring another pass to resolve. R2 is the\\
608 &number of references to unknown symbols which occured in the\\
609 &pass (but only R1 determines the need for another pass). R3\\
610 &is a BITMASK of the reasons why another pass is required.\\
611
612 \mono{2}&Mismatches between program labels and symbols are displayed\\
613 &on every pass (usually none occur in the first pass unless you\\
614 &have re-declared a symbol name).\\
615 \\
616 &Displayed information for symbols:\\
617 \\
618 &~~\mono{????} = unknown value\\
619 &~~\mono{str} = symbol is a string\\
620 &~~\mono{eqm} = symbol is an eqm macro\\
621 &~~\mono{(R)} = symbol has been referenced\\
622 &~~\mono{(s)} = symbol created with \nameref{pseudoop:set} or \nameref{pseudoop:eqm} directive\\
623 \\
624
625 \mono{3}&Unresolved and unreferenced symbols are displayed every pass\\
626 \mono{4}&Symbol table displayed to \mono{STDOUT} every pass\\
627 \\
628 \bottomrule
629 \end{tabularx}
630 \end{table}
631
632
633 \begin{savequote}
634 \sffamily
635 ``If you have the words, there's always a chance that you'll find the way.''
636 \qauthor{Seamus Heaney}
637 \end{savequote}
638
639 \chapter{Numbers, Expressions and Operators}
640 \index{Expressions}
641 \index{Numbers}
642 \index{Operators}
643
644 \section{Constants}
645 \label{section:numberformat}
646 \index{Expressions!Constants}
647 \index{Number Format}
648 \index{Bases!2 binary}
649 \index{Bases!8 octal}
650 \index{Bases!10 decimal}
651 \index{Bases!16 hexadecimal}
652
653 \label{changelog:20200905range}
654 \subsection{Magnitude}
655 \index{Numbers!Magnitude}
656 All numbers and addresses in \dasm are represented internally as signed 32-bit values.
657
658 \index{Numbers!Range Checking}
659 Numbers are range-checked at point of usage. Byte values should be between \mono{\$80} (\mono{-128}) and \mono{\$FF} (\mono{255}) inclusive, as these values are the extremes of what can be represented with signed and unsigned 8-bit values. Word values should be between \mono{\$8000} (\mono{-32768}) and \mono{\$FFFF} (\mono{65535}) as these are the extremes of signed and unigned 16-bit values.
660
661 Note that the assembler cannot tell the difference between the representation (in 8 and 16 bits) of signed/unsigned representation of negative and positive numbers, as they share the same bit patterns.
662
663 \begin{code}
664 lda #%11111111 ; is this 255 or -1?
665 \end{code}
666
667 The answer to the above question is that it depends on what the programmer does with the value, as in signed 8-bit arithmetic, \mono{\%11111111} is \mono{-1}, and in unsigned 8-bit arithmetic it is \mono{255}. This is not as confusing as it sounds, as the assembler works in signed 32-bit arithmetic and the signedness of these values (particularly when taking the low byte/word) is unambiguous.
668
669 It is common for programmers who want all bits set to simply use \mono{-1}.
670
671 \subsection{Base Representation}
672 \index{Numbers!Bases}
673 \index{Bases}
674
675 Values in \dasm can be specified in base 2 (binary), 8 (octal), 10 (decimal), 16 (hexadecimal), or as ASCII characters. It doesn't matter to \dasm which format you use, so use what makes the most sense in your code. The interpretation of the value is determined by the prefix and digits used, as shown in the following table.
676
677 \label{changelog:20200824const}
678 \begin{table}[H]
679 \begin{tabularx}{\linewidth}{lll}
680 \toprule
681 \textbf{Type}&\textbf{Format}&\textbf{Content}\\
682 \hline
683 \\
684 \mono{Binary} &\mono{\%}n & \mono{0-1}\\
685 \\
686 \mono{Octal} &\mono{0}n & \mono{0-7}\\
687 \\
688 \mono{Decimal} &\mono{n} & \mono{0-9}, first digit non-\mono{0}\\
689 \\
690 \mono{Hexadecimal} &\mono{\$}n &case insensitive \mono{0-9,A-F}\\
691 \\
692 \mono{Character} &\mono{'c} &\mono{ASCII} character\\
693 \\
694 \mono{String} &\mono{``cc..''} & zero-terminated ASCII character string\\
695 & & \textbf{not} zero-terminated when used in \mono{DC}/\mono{DS}/\mono{DV}.\\
696 \\
697 &\mono{[exp]d} & The constant expressions is evaluated and its\\
698 & &decimal result converted into an ASCII string.\\
699 & &Useful in conjunction with ECHO diagnostic output.\\
700 \\
701 \bottomrule
702 \end{tabularx}
703 \end{table}
704
705 Even though decimal numbers can't start with \mono{0}, as that describes an octal number, the octal \mono{0} is equivalent. In other words, \mono{0\textsubscript{10}} = \mono{0\textsubscript{8}}.
706
707 Negative signs are placed before the number prefix (e.g., \mono{-\$123}).
708
709 \subsubsection{Examples}
710
711 \begin{code}
712 lda #%101 ; binary = 5 decimal
713 lda #%10101010 ; binary = 170 decimal
714 lda #015 ; octal = 13 decimal
715 lda #$FF ; hexadecimal = 255 decimal (unsigned)
716 ; = -1 decimal (signed)
717 lda #'A ; ASCII character = 65 decimal
718 lda #'A' ; ERROR - no trailing quote allowed!
719 \end{code}
720
721 \begin{code}
722 VAL = -129
723 lda #VAL ; ERROR - outside byte range
724 \end{code}
725
726
727
728 \begin{code}
729 ; A great approximation for Pi is 355/113
730 PIDIGITS = 1000000 * 355/113
731 ECHO "PI DIGITS: ", PIDIGITS ; obscure
732 ECHO "PI DIGITS: ",[PIDIGITS]d ; aha!
733 \end{code}
734
735 \begin{outputx}
736 PI DIGITS: $2fefd8
737 PI DIGITS: 3141592
738 \end{outputx}
739
740
741
742
743 \hrule
744 \index{Expressions}
745 \index{Operators}
746
747 \index{Expressions!Brackets}
748 \index{Expressions!\mono{[ ]}}
749 \index{Expressions!\mono{( )}}
750
751 \section{Expressions}
752
753 Expressions are calculations involving symbols and numbers. These calculations are performed by \dasm during the assembly process. Often, symbols will have unkonwn values during an assembly pass, and thus an expression cannot be evaluated. \dasm will, in these cases, perform another assembly pass - often, unknown values are resolved later in the assembly. A successful assembly is one where no errors have been detected, and all referenced symbols have been resolved.
754
755 \subsection{Brackets}
756 \label{changelog:20200908brackets}
757 \index{Brackets}
758 \index{Expressions!Brackets}
759
760 The precedence of operators is the same as for the C-language in almost all respects.
761
762 Either square brackets \mono{[]} or round brackets \mono{()} may be used to group expressions and to clarify/adjust precedence, depending on which \nameref{pseudoop:processor} is in effect for the assembly. Differences are related to the use of round brackets in assembler instructions.
763
764 \subsubsection{F8 and Other Processors}
765 \index{Expressions!Brackets!F8 and Other}
766 Either bracket type may be used in all situatinos.
767
768 \subsubsection{6502 Processor}
769 \index{Expressions!Brackets!6502}
770
771 Use square brackets \mono{[]} when you are unsure. The reason round brackets \mono{()} cannot be used to
772 group expressions is due to a conflict with 6502 assembly language which use them to specifiy indirect memory access (for example, ``\mono{lda (zp),y}'').
773
774 It is possible to use round brackets \mono{()} instead of square brackets \mono{[]} in expressions following directives, but not following mnemonics.
775
776 So this works:
777
778 \begin{code}[caption=Valid Bracket Usage]
779 IF target & (pet3001 | pet4001) ; OK
780 \end{code}
781
782 but this doesn't:
783
784 \begin{code}[caption=Invalid Bracket Usage]
785 lda #target & (pet3001 | pet4001) ; fails
786 \end{code}
787
788 \section{Operators}
789 \index{Operators}
790 \index{Operators!Logical!OR}
791 \index{Operators!Logical!NOT}
792 \index{Operators!\mono{< LSB}}
793 \index{Operators!\mono{> MSB}}
794
795 Some operators, such as \mono{||} (logical-OR), can return a resolved value even if
796 one of the expressions is not resolved.
797
798 \subsection{Operator Precedence}
799 \index{Operators!Precedence}
800
801 Operators in any expression are evaluated in order of precedence. The following tables list the various operators, their function, and precedence (PR). Operators are handled in precedence order high to low.
802
803
804 \subsubsection{Unary Operators}
805 \label{operators:unary}
806 \index{Operators!Unary}
807
808 \label{changelog:alternateunary}
809 \noindent
810 \begin{table}[H]
811 \begin{tabularx}{\textwidth}{cllr}
812 \toprule
813 \textbf{Operator} &\textbf{Alternate} & \textbf{Function} & \textbf{PR}\\
814 \hline
815 \\
816 \mono{$\sim$exp}&\mono{exp\textasciicircum -1}&one's complement & 20\\
817 \mono{-exp}&\mono{[exp\textasciicircum -1]+1}& mathematical negation & 20\\
818 \mono{!exp}&\mono{exp==0}& logical \mono{NOT}& 20\\
819 &&(0 if \mono{exp} is non-zero, 1 if \mono{exp} is zero)&\\
820 \mono{<exp}&\mono{exp\&\$FF}& take \mono{LSB} of the low word& 20\\
821 \mono{>exp}&\mono{[exp>\,>8]\&\$FF}&take \mono{MSB} of the low word& 20\\
822 \\
823 \bottomrule
824 \end{tabularx}
825 \caption{\label{tab:unaryoperators}Unary Operators}
826 \end{table}
827
828 \label{operators:specialcase}
829 \subsubsection{Special Case}
830 \index{Operators!Unary!Special Case}
831 \label{changelog:specialcase}
832
833 Some operations will result in non-byte values when a byte value was wanted. For example: \mono{$\sim$1} is not \mono{\$FF}, but instead \mono{\$FFFFFFFF}. Preceding it with a \mono{<} (take LSB of) will solve the problem.
834
835 However, there is a special-case for negative numbers used in operands. Although internally 32-bit, numbers in the range -1 to -128 are treated as two's complement 8-bit numbers in this situation. Another way of thinking of this - it is not necessary to take the \mono{LSB} of the number if it is in the range -128 to 255, as \dasm will recognise this as a valid signed/unsigned 8-bit number and do this automatically.
836
837 \label{changelog:20200824rangebug}
838 \index{Bugs!Range}{\color{bug}Bug: Currently \dasm allows values in the range \mono{-\$FF} to \mono{+\$FF}. This is incorrect. The correct range is \mono{-\$80} to \mono{+\$FF}}.
839
840 \subsubsection{Examples}
841
842 \begin{code}
843 ; Special case handling of 8-bit negatives
844 lda #-1 ; OK
845 lda #$FF ; same as -1
846 lda #-129 ; ERROR - outside 8-bit size
847 \end{code}
848
849 \begin{code}
850 ; Extracting low and high byte of value
851 lda #<addr ; low byte of symbol address/value
852 lda #>$12345678 ; = $56, the high byte of the low word
853 \end{code}
854
855
856
857 \subsubsection{Binary Operators}
858 \index{Operators!Binary}
859
860 \label{changelog:20200824arithmetic}
861 \begin{table}[H]
862 \begin{tabularx}{\textwidth}{clr}
863
864 \toprule
865 \textbf{Operator} & \textbf{Function} & \textbf{PR}\\
866 \hline
867 \\
868 \mono{*}& Multiplication &19\\
869 \mono{/}& Division &19\\
870 \mono{\%}& Modulus &19\\
871 \mono{+}& Addition &18\\
872 \mono{-}& Subtraction &18\\
873 \mono{\textgreater\,\textgreater}& Aritmetic shift right & 17\\
874 \mono{<\,<}& Arithmetic shift left &17\\
875 \mono{>}& Greater than & 16\\
876 \mono{>=}& Greater than or equal to &16\\
877 \mono{<} & Less than & 16\\
878 \mono{<=}& Less than or equal to&16\\
879 \mono{==}& Logical equal to.&15\\
880 \mono{=}& Logical equal to. Deprecated! (use `\mono{==}')&15\\
881 \mono{!=}& Not equal to &15\\
882 \mono{\&} & Arithmetic \mono{AND} &14\\
883 \mono{\^}& Arithmetic exclusive-\mono{OR} &13\\
884 \mono{|}& Arithmetic \mono{OR} &12\\
885 \mono{\&\&}& Logical \mono{AND}. Evaluates as 0 or 1&11\\
886 \mono{||}& Logical \mono{OR}. Evaluates as 0 or 1&10\\
887 \mono{?}& If the left expression is \mono{TRUE}, result is the right&9\\
888 &expression, else result is \mono{0}. \mono{[10?20]} returns \mono{20}.&\\
889 &The function of the C conditional operator \mono{a?b:c}&\\
890 & can be achieved by using \mono{[a?b-c]+c}.&\\
891 \mono{[ ]}& Group expressions &8\\
892 \mono{,}& Separates expressions in list (also used in &7\\
893 &addressing mode resolution, so be careful! &\\
894 \\
895 \bottomrule
896 \end{tabularx}
897 \caption{\label{tab:binaryoperators}Binary Operators}
898 \end{table}
899
900
901 \section{Symbols}
902
903 \renewcommand{\arraystretch}{1.1}
904 \begin{table}[H]
905 \begin{tabularx}{\textwidth}{cl}
906 \toprule
907 \textbf{Symbol} & \textbf{Meaning}\\
908 \hline
909 \\
910 \index{Symbol!...}
911 \index{Symbol!checksum}
912 \mono{...} & Checksum so far (of actually-generated data)\\
913 \mono{..} & Evaluated value in \nameref{pseudoop:dv} directive \\
914 \mono{.} & Current program counter\\
915 \mono{*} & Synonym for `\mono{.}' when not confused as an operator.\\
916 \mono{.name} & Represents a local label name. Local labels may be reused\\
917 & inside \mono{MACRO}s and between \nameref{pseudoop:subroutine} directives, but may not be \\
918 & referenced across \mono{MACRO} or \mono{SUBROUTINE} scope.\\
919 & (as of the beginning of the instruction)\\
920 \mono{name} & Represents a global symbol name. Beginning with an alpha\\
921 &character and containing letters, numbers, or underscores.\\
922 & Symbol definitions may end with a colon, but reference must\\
923 & omit the colon.\\
924 \mono{nnn\$} & Local label, much like `\mono{.name}', except that defining\\
925 & a non-local label has the effect that \mono{SUBROUTINE} has on`\mono{.name}'.\\
926 & They are unique within \mono{MACRO}s, like `\mono{.name}'.\\
927 & Note that `\mono{0\$}' and \mono{00\$} are distinct, as are \mono{8\$} and \mono{010\$}\\
928 & (mainly for compatibility with other assemblers).\\
929 \\
930 \bottomrule
931 \end{tabularx}
932 \caption{\label{tab:symbols}Symbols}
933 \end{table}
934
935
936 \section{Why-Codes}
937
938 \dasm can display the reason (via \nameref{flag:verbosity}) it needs to do another pass. Internally, these reasons are stored in the ``why'' word.
939
940 The list of available reasons include:
941
942 \begin{table}[H]
943 \begin{tabularx}{\textwidth}{cl}
944 \toprule
945 \textbf{Bit} & \textbf{Usage}\\
946 \hline
947 \\
948 \mono{0}& expression in mnemonic not resolved\\
949 \mono{1}& -\\
950 \mono{2}& expression in a \mono{DC} not resolved\\
951 \mono{3}& expression in a \mono{DV} not resolved (probably in \mono{DV}'s \mono{EQM} symbol)\\
952 \mono{4}& expression in a \mono{DV} not resolved (could be in \mono{DV}'s \mono{EQM} symbol)\\
953 \mono{5}& expression in a \mono{DS} not resolved\\
954 \mono{6}& expression in an \mono{ALIGN} not resolved\\
955 \mono{7}& \mono{ALIGN}: Relocatable origin not known (if in \mono{RORG} at the time)\\
956 \mono{8}& \mono{ALIGN}: Normal origin not known (if in \mono{ORG} at the time)\\
957 \mono{9}& \mono{EQU}: expression not resolved\\
958 \mono{10}& \mono{EQU}: value mismatch from previous pass (phase error)\\
959 \mono{11}& \mono{IF}: expression not resolved\\
960 \mono{12}& \mono{REPEAT}: expression not resolved\\
961
962 \mono{13}& a program label has been defined after it has been\\
963 &referenced (forward reference) and thus we need another pass\\
964 \mono{14}& a program label's value is different from that of the\\
965 &previous pass (phase error)\\
966 \\
967 \bottomrule
968 \end{tabularx}
969 %\caption{\label{tab:constants}WHY Codes}
970 \end{table}
971
972 \label{changelog:20200824error}
973 There are three types of error; those that cause the assembly to abort immediately, those that complete the current pass and then abort assembly, and those that allow another assembly pass in the hope that the error will self-correct.
974
975 \begin{savequote}
976 \sffamily
977 ``No symbols where none intended.''
978 \qauthor{Samuel Beckett}
979 \end{savequote}
980
981 \chapter{Symbols and Labels}
982
983 \section{Labels}
984 \index{Labels}
985 \index{Labels!Global}
986
987 \index{Symbols}
988
989 The terms symbols and labels are synonymous. However, common usage is to use ``label'' for a symbol referring to a memory address, and that convention is generally used in this document. Otherwise, it is referred to as a symbol.
990
991 Labels are and symbols assigned addresses or values by \dasm. These values are calculated during the assembly process by resolving the location or value of expressions defining the label. Often this may take multiple assembly passes to resolve.
992
993 Label definitions start at the beginning of a line and are encoded in ASCII; they must start with a letter or \mono{@} or {\_}, and can include letters, numbers, and some symbols.
994
995
996
997
998 \label{changelog:20200823colon}
999 \subsubsection{Colon Usage}
1000 \index{Labels!Use of Colon in}
1001 Label definitions can end with a colon, but the usage of the label must not include the colon. This can be helpful when you are editing your code if you want to search for your label definition \mono{label:} which will return just one result (unless it's a local label, which may be duplicated), or \mono{label} which will return all instances.
1002
1003 \subsubsection{Examples}
1004 \begin{code}
1005 ; Usage of colon in label names
1006 loop: jmp loop ; OK
1007 jmp loop: ; error: Illegal character ':'
1008 \end{code}
1009
1010
1011 \begin{code}[caption=Global Label Definitions]
1012 ; Examples of label/symbol definitions
1013 Label1 ; standard
1014 Label2: ; optional colon
1015 lab3 = %101010 ; life, the universe, and everything
1016 LAB4 SETSTR "Hello" ; allocated as string
1017 .lab6 ; local
1018 lab,{1} ; dynamic, inside macro
1019 lab@ ; some symbols are valid too
1020 labWhoops ; invalid - not in 1st column
1021 lab SET -INFINITY ; SET: initial definition
1022 lab SET 0 ; SET: and re-use!
1023 \end{code}
1024
1025
1026 \section{Local Labels}
1027 \index{Labels!Local}
1028 \label{locallabels}
1029
1030 Local labels begin with a dot ``\mono{.}''. They are local to the scope of the current \nameref{pseudoop:subroutine} directive boundary, and may be re-used in other subroutine scopes. Note that the usage of the term subroutine can be misleading; local labels are local to blocks defined by usage of the directive \nameref{pseudoop:subroutine}, not to code-subroutines.
1031
1032 Usually local labels are used in macros and within actual code subroutines. This is handy where simple names such as `\mono{.loop}' can be re-used many times. It is particularly useful in macros, where global labels are problematic due to the inability to declare a global label more than once.
1033
1034 \subsubsection{Example}
1035 \begin{code}[caption=Scope of Local Labels]
1036 ; Define macro
1037
1038 MAC DO
1039 ; Implicit SUBROUTINE inserted here!
1040 .mac jmp .mac ; OK - local macro label
1041 ENDM
1042
1043 ; elsewhere in the code...
1044
1045 .local jmp .local ; OK - local label
1046 global jmp global ; OK - global label
1047
1048 DO ; use macro
1049
1050 ; implicit new scope has happened
1051 ; after macro instantiated
1052
1053 jmp global ; OK - global scope
1054 jmp .local ; error - outside scope
1055 jmp .mac ; error - outside scope
1056
1057 \end{code}
1058
1059 The example above shows the result of the use of local and global labels, and the effects of implicit \nameref{pseudoop:subroutine} as a result of a macro instance.
1060
1061 \section{Dynamic Labels}
1062 \index{Labels!Dynamic}
1063 \label{todo}
1064
1065 When used in a symbol name, the ``\mono{,}'' operator indicates one or more
1066 arguments that follow should be evaluated, and the resulting values
1067 should be concatenated to the label, to create a dynamic symbol name.
1068
1069 \begin{usage}
1070 symbol,arg1[,argn...]
1071 \end{usage}
1072
1073 String literals in arguments must be specified with quotes around the string
1074 text. Expression operators can also be used, but due to label
1075 parsing constraints, they should not contain spacing.
1076
1077 The concat-eval ``\mono{,}'' operator also works on the expression side
1078 of \mono{EQU/SET} directives, so dynamic labels can be used with opcodes.
1079
1080 \subsubsection{Examples}
1081
1082 \begin{code}
1083 ; define and use a dynamic label
1084 CON,"cat" ; define label
1085 jmp CONcat ; use the generated label
1086 \end{code}
1087
1088 \begin{code}
1089 ; Use a dynamic label inside a macro
1090
1091 N SET 0 ; instance number
1092
1093 MAC dynm ; {1}=base name
1094 {1},"_",N ; define label using {1} and instance #
1095 N SET N+1
1096 ENDM
1097
1098 dynm fna
1099 jmp fna_0 ; OK
1100
1101 dynm fna
1102 jmp fna_1 ; OK
1103
1104 dynm fnb
1105 jmp fnb_2 ; OK
1106 jmp fnb_0 ; ERROR - does not exist
1107 \end{code}
1108
1109 \section{Deprecated Form}
1110 \label{changelog:20200907deprecated}
1111 \dasm currently supports labels defined as per the following...
1112
1113 \begin{usage}
1114 []...^[]...label
1115 \end{usage}
1116
1117 That might look a bit weird because, basically, it is. Essentially, whitespace carat whitespace and then the label name. This is a \textbf{deprecated} format that may not be supported by future versions of \dasm. Do not use.
1118
1119 \begin{code}
1120 ^ weirdLabel ; this is a weird way to define a label
1121 normalLabel ; this is a normal way
1122 \end{code}
1123
1124
1125
1126 \begin{savequote}
1127 \sffamily
1128 ``Success is often the result of taking a misstep in the right direction.''
1129 \qauthor{Al Bernstein}
1130 \end{savequote}
1131
1132 \chapter{Directives}
1133
1134
1135
1136 \label{chapter:pseudoops}
1137 \index{Directives}
1138
1139 Also known as pseudo-ops, directives appear in the source code. They instruct \dasm what to do during assembly. These are distinct from the mnemonics in the source code, which contains the human-readable instructions for the microprocessor itself. Directives include macros, segment definitions, setting the origin/location of code, etc. They are not case-sensitive.
1140
1141 There must be whitespace before a directive. Thus, directives must not appear in the first column of any line. Directives are not case-sensitive, but in this document they are shown in uppercase.
1142
1143 Some directives cannot have labels on the same line - for example, those where there is no possibility of evaluating a label's value because no origin/segment has yet been defined. For directives where a label is illegal, or does not make sense, this is explicitly stated.
1144
1145 If a label is present, then its value will be set to the current \nameref{pseudoop:org}/\nameref{pseudoop:rorg} either before or after
1146 a directive is processed. Most of the time, the label to the left of a
1147 directive is set to the current \nameref{pseudoop:org}/\nameref{pseudoop:rorg}. The following directives' labels are given their value {\bf after} execution of the directive: \nameref{pseudoop:seg}, \nameref{pseudoop:org}, \nameref{pseudoop:rorg}, \nameref{pseudoop:rend}, \nameref{pseudoop:align}.
1148
1149 All directives (and incidentally also the mnemonics) can be prefixed with a dot ``\mono{.}'' or a crosshatch ``\mono{\#}'' for compatibility with other assemblers. So, ``\mono{.IF}'' is the same as ``\mono{IF}'' and ``\mono{\#IF}''. In the case of the dot, this works only because unattached, lone \mono{.FORCE} extensions are meaningless.
1150
1151 \section{Includes}
1152 \subsection{\texttt{INCBIN}}
1153 \label{pseudoop:incbin}
1154 \index{Directives!Includes!\texttt{INCBIN}}
1155
1156 \begin{usage}
1157 INCBIN "filename"
1158 \end{usage}
1159
1160 Include the binary contents of another file literally in the output.\\
1161 \hrule
1162
1163 \subsection{\texttt{INCDIR}}
1164 \label{pseudoop:incdir}
1165 \index{Directives!Includes!\texttt{INCDIR}}
1166
1167 \begin{usage}
1168 INCDIR "directory"
1169 \end{usage}
1170
1171 Add the given directory name to the list of places where
1172 \nameref{pseudoop:include} and \nameref{pseudoop:incbin} search their files. Multiple directories can be added through multiple \nameref{pseudoop:incdir} commands. When the other includes directives look for files, first the names are tried relative to the current directory, if that fails and
1173 the name is not an absolute pathname, the directory list is tried.
1174 You can optionally end the directory name with a ``\mono{/}''.
1175
1176 \index{todo}
1177 {\color{todo}AmigaDOS filename conventions imply that two slashes at the end of a directory indicates the parent directory, and so this does an \nameref{pseudoop:include}~\mono{"/directory"}}
1178
1179 The command-line option \mono{-Idirectory} is equivalent to an \nameref{pseudoop:incdir}~\mono{"directory"} directive placed at the beginning of the source file.
1180
1181 The directory list is not cleared between passes, but each exact directory name is added to the list only once.
1182 \\
1183 \hrule
1184 \subsection{\texttt{INCLUDE}}
1185 \label{pseudoop:include}
1186 \index{Directives!Includes!\texttt{INCLUDE}}
1187
1188
1189 \begin{usage}
1190 INCLUDE "file name"
1191 \end{usage}
1192
1193 Effectively inserts the contents of another file at the point of the \nameref{pseudoop:include} and continues assembling the original as if it were one merged file.
1194
1195 \subsubsection{Example}
1196
1197 \begin{code}[caption=Declaring the Platform]
1198 ; Typical first few lines in an Atari 2600 program...
1199 processor 6502
1200 include "vcs.h"
1201 include "macro.h"
1202 \end{code}\\
1203
1204 \hrule
1205 %-------------------------------------------------------------------------------
1206
1207 \section{Assigments}
1208
1209 \subsection{\texttt{EQU}, \texttt{=}}
1210 \label{pseudoop:equ}
1211 \label{pseudoop:=}
1212 \index{Directives!Assignments!\texttt{EQU}}
1213 \index{Directives!Assignments!\texttt{=}}
1214
1215
1216 \begin{usage}
1217 symbol EQU exp
1218 symbol = exp
1219 \end{usage}
1220
1221 The expression is evaluated and the result assigned to
1222 \mono{symbol}.
1223
1224 \nameref{pseudoop:equ} are equivalent.
1225
1226 You can use the common idiom of ``\mono{.=.+3}'' - in other words, you can assign to ``.'' or ``\text{*}'' directly, instead of using an \nameref{pseudoop:org} or \nameref{pseudoop:rorg} directive.
1227
1228 More formally, a directive of the form ``\mono{.~EQU~exp}'' is
1229 interpreted as if it were written ``\nameref{pseudoop:org} \mono{exp}'' or ``\nameref{pseudoop:rorg} \mono{exp}''.
1230 The \nameref{pseudoop:rorg} is used if a relocatable origin is already in effect,
1231 otherwise \nameref{pseudoop:org} is used. Note that the first example is \textbf{not}
1232 equivalent to ``\nameref{pseudoop:ds} \mono{3}'' when the \nameref{pseudoop:rorg} is in effect.
1233
1234 A symbol can also be defined through the command-line options \nameref{flag:definesymbol}, \nameref{flag:definesymbolF} and \nameref{flag:definesymbolM}.\\
1235
1236 \hrule
1237 \subsection{\texttt{EQM}}
1238 \label{pseudoop:eqm}
1239 \index{Directives!Assignments!\texttt{EQM}}
1240
1241 \begin{usage}
1242 symbol EQM exp
1243 \end{usage}
1244
1245 The string representing the expression is assigned to the
1246 symbol. Occurrences of the label in later expressions causes
1247 the string to be evaluated for each occurrence. Also used in
1248 conjuction with the \nameref{pseudoop:dv} psuedo-op.\\
1249
1250 \hrule
1251 \subsection{\texttt{SET}}
1252 \label{pseudoop:set}
1253 \index{Directives!Assignments!\texttt{SET}}
1254
1255 \begin{usage}
1256 symbol SET exp
1257 \end{usage}
1258
1259
1260 Same as \nameref{pseudoop:equ}, but the symbol may be reassigned later.\\
1261
1262 \subsubsection{Example}
1263
1264 \begin{code}
1265 ; Using SET to do calcualtions
1266 N SET 1
1267 SUM SET 0
1268 REPEAT 10
1269 SUM SET SUM+N
1270 N SET N+1
1271 REPEND
1272 ECHO "Sum of 1 to 10 is", [SUM]d
1273 \end{code}
1274
1275 \begin{outputx}
1276 Sum of 1 to 10 is 55
1277 \end{outputx}
1278
1279
1280 \hrule
1281 \subsection{\texttt{SETSTR}}
1282 \label{pseudoop:setstr}
1283 \index{Directives!Assignments!\texttt{SETSTR}}
1284
1285 \begin{usage}
1286 symbol SETSTR exp
1287 \end{usage}
1288
1289 The expression is converted to a string, and assigned to the
1290 symbol. Typical use-case is within a macro, to allow the
1291 macro to echo or otherwise use the name of an argument.
1292
1293 \subsubsection{Example}
1294
1295 \begin{code}[caption=Use of SETSTR to Display Function Name]
1296 ; Use SETSTR to output a parameter as a string
1297 MAC CALL ; {1} = function name
1298 .FNAME SETSTR {1}
1299 ECHO "This is the function name:", .FNAME
1300 ENDM
1301
1302 CALL HelloWorld ; test it...
1303 \end{code}
1304
1305 \begin{outputx}
1306 This is the function name: HelloWorld
1307 \end{outputx}
1308
1309
1310
1311 \section{Data}
1312 \subsection{\texttt{DC}}
1313 \label{pseudoop:dc}
1314 \index{Directives!Data!\texttt{DC}}
1315
1316 \begin{usage}
1317 DC[{.B|.W|.L|.S}] exp,...
1318 \end{usage}
1319 \label{changelog:20200915endian}
1320 Declare data in the current segment. No output is generated if within a uninitialised \mono{.U} segment. The byte ordering (the endian order) for the selected processor is used for each entry. It is possible to "swap endianness" (the byte order of wide-characters) using \mono{DC.s}
1321
1322 The default size extension (\mono{.B}, \mono{.W}, \mono{.L}) is \mono{.B} (byte).
1323
1324 \subsubsection{Alternates}
1325 \begin{usage}
1326 BYTE exp,...
1327 WORD exp,...
1328 LONG exp,...
1329 \end{usage}
1330
1331 \subsubsection{Examples}
1332
1333 \begin{code}[caption=Data Generation]
1334 ; various ways of defining data...
1335 DC 0,1,2,3
1336 BYTE -1,1,2,3, <Value
1337 .WORD 100,1000,10000, VectorTable
1338 LONG 100000, 50*50*50
1339 dc 'a' ; ERROR - should be 'a
1340 dc.s "unicode" ; endian-swapped character words
1341 \end{code}
1342
1343 \begin{code}[caption=Data Declaration using REPEAT Loop]
1344 ; generate the bytes 0 to 9 inclusive
1345 VAL SET 0
1346 REPEAT 10
1347 .byte VAL
1348 VAL SET VAL + 1
1349 REPEND
1350 \end{code}\\
1351
1352
1353
1354
1355 \hrule
1356 \subsection{\texttt{DS}}
1357 \label{pseudoop:ds}
1358 \index{Directives!Data!\texttt{DS}}
1359
1360 \emph{Not available for the F8 processor - use \nameref{pseudoop:res}}
1361
1362 \begin{usage}
1363 DS[{.B|.W|.L}] exp[,fillvalue]
1364 \end{usage}
1365
1366 Declare space and fill with a fillvalue (if specificed, otherwise default is 0). The optional size extender (\mono{.B}, \mono{.W}, \mono{.L}) defines the data size (1, 2 or 4) bytes. Data is not generated if within an uninitialized segment, but the origin still changes accordingly (this is very useful for defining variables). The number of bytes generated is \mono{exp} $\times$ data size (1, 2, or 4)
1367
1368 The default size extension is a byte.
1369
1370 The fill value is not related to the fill value used by \nameref{pseudoop:org}.
1371
1372
1373
1374
1375 \subsubsection{Examples}
1376
1377 \begin{code}[caption=Declaring Space]
1378 ds 2 ; 2 bytes of default value 0
1379 ds 2,10 ; 2 bytes of value 10
1380 ds 10,2 ; 10 bytes of value 2
1381 ds.w 2 ; 4 bytes (2 words) of default value 0
1382 ds.l 0 ; define no space at all
1383 \end{code}
1384
1385 \begin{code}[caption=Declaring Variables]
1386 ; Declare some zero page variables
1387 ; in an uninitialised segment
1388 SEG.U variables
1389 ORG $80
1390 var1 ds 2 ; 2 bytes @ $80-$81
1391 var2 ds.w 10 ; 20 bytes (10 words) @ $82-$8B
1392 varn ds.w 2 ; 4 bytes (2 longs) @ $8C-$8F
1393 \end{code}\\
1394
1395 \hrule
1396 \subsection{\texttt{DV}}
1397 \label{pseudoop:dv}
1398 \index{Directives!Data!\texttt{DV}}
1399
1400 \begin{usage}
1401 DV[{.B|.W|.L}] eqmlabel exp,...
1402 \end{usage}
1403
1404 This is equivalent to \nameref{pseudoop:dc}, but each \mono{exp} in the list is passed
1405 through the symbolic expression specified by the \mono{eqmlabel}.
1406 The expression is held in a special symbol dotdot '\mono{..}' on each
1407 call to the \mono{eqmlabel}.
1408
1409 See \nameref{pseudoop:eqm}.\\
1410
1411 \hrule
1412
1413 \subsection{\texttt{HEX}}
1414 \label{pseudoop:hex}
1415 \index{Directives!Data!\texttt{HEX}}
1416
1417 \begin{usage}
1418 HEX {hh...}
1419 \end{usage}
1420
1421 This sets down raw hexadecimal data. Whitespace is optional between each \mono{hh} byte.
1422 No expressions are allowed. Note that you do NOT place a ``\$''
1423 in front of the hexadecimal digits. This is a short form for creating
1424 tables compactly. Data is always layed down on a byte-by-byte
1425 basis.
1426
1427 \subsubsection{Example}
1428 \begin{code}
1429 HEX 1A45 45 13254F 3E12
1430 \end{code}
1431
1432 produces the following sequence of decimal values in the binary...
1433
1434 \begin{outputx}
1435 26 69 69 19 37 79 62 18
1436 \end{outputx}
1437
1438 \hrule
1439
1440
1441 \subsection{\texttt{RES}}
1442 \label{changelog:20200906res}
1443 \label{pseudoop:res}
1444
1445 \emph{Not available for 6502 - use \nameref{pseudoop:ds}}
1446
1447 \emph{Since \mono{DS} is an F8 instruction (decrement scratchpad register),
1448 the \mono{DS} directive isn't available anymore if \dasm assembles
1449 F8 code, and this \mono{RES} directive is provided as an alternative.
1450 }
1451
1452
1453 \begin{usage}
1454 RES[{.B|.W|.L}] exp[,fillvalue]
1455 \end{usage}
1456
1457 Declare space and fill with a fillvalue (if specificed, otherwise default is 0). The optional size extender (\mono{.B}, \mono{.W}, \mono{.L}) defines the data size (1, 2 or 4) bytes. Data is not generated if within an uninitialized segment, but the origin still changes accordingly (this is very useful for defining variables). The number of bytes generated is \mono{exp} $\times$ data size (1, 2, or 4)
1458
1459 The default size extension is a byte.
1460
1461 The fill value is not related to the fill value used by \nameref{pseudoop:org}.
1462
1463
1464 \subsubsection{Examples}
1465
1466 \begin{code}[caption=Declaring Space]
1467 res 2 ; 2 bytes of default value 0
1468 res 2,10 ; 2 bytes of value 10
1469 res 10,2 ; 10 bytes of value 2
1470 res.w 2 ; 4 bytes (2 words) of default value 0
1471 res.l 0 ; define no space at all
1472 \end{code}
1473
1474
1475 \section{Conditionals}
1476 \label{conditionals}
1477
1478
1479 Conditionals allow selected selections of code to be assembled.\\
1480
1481
1482 \subsection{\texttt{IFCONST}}
1483 \label{pseudoop:ifconst}
1484 \index{Directives!Conditionals!\texttt{IFCONST}}
1485
1486 \begin{usage}
1487 IFCONST exp
1488 \end{usage}
1489
1490 A useful method is to use \nameref{pseudoop:ifconst} or \nameref{pseudoop:ifnconst} to check for the definition of a symbol and then conditionally assemble code based on the result. This can be especially useful with symbols defined via the command-line.
1491
1492 \subsubsection{Examples}
1493
1494 \begin{code}
1495 IFCONST PI
1496 IF PI=3
1497 ECHO "Are you sure?"
1498 ENDIF
1499 ENDIF
1500 \end{code}\\
1501
1502 \begin{outputx}
1503 > dasm source.asm -DPI=3
1504 Are you sure?
1505 \end{outputx}
1506
1507
1508 Is \mono{TRUE} if the expression result is defined, \mono{FALSE} otherwise
1509 and no error is generated if the expression is undefined.
1510
1511 \subsubsection{Example}
1512 \begin{code}
1513 symbol ; defined!
1514 IFCONST symbol
1515 ECHO "Defined!" ; we'll see this!
1516 ENDIF
1517 \end{code}\\
1518
1519 \hrule
1520 \subsection{\texttt{IFNCONST}}
1521 \label{pseudoop:ifnconst}
1522 \index{Directives!Conditionals!\texttt{IFNCONST}}
1523
1524 \begin{usage}
1525 IFNCONST exp
1526 \end{usage}
1527
1528 \subsubsection{Example}
1529 \begin{code}
1530 IFNCONST symbol
1531 ECHO "Not defined!" ; we'll see this!
1532 ENDIF
1533 \end{code}\\
1534
1535
1536
1537 \hrule
1538 \subsection{\texttt{IF}}
1539 \label{pseudoop:if}
1540 \index{Directives!Conditionals!\texttt{IF}}
1541
1542 \begin{usage}
1543 IF exp
1544 ; block TRUE
1545 [ELSE
1546 ; block FALSE
1547 ]
1548 ENDIF
1549 \end{usage}
1550
1551 Evaluates \mono{exp} and if \mono{TRUE} (\mono{exp} is defined and non-zero) will insert the following block of code.
1552
1553
1554 Neither \nameref{pseudoop:if} nor \nameref{pseudoop:else} will be executed if the expression result
1555 is undefined. In that case, another assembly
1556 pass is performed and phase errors (in the next pass only) will not
1557 be reported unless the verbosity is set to 1 or more.
1558
1559 \subsubsection{Examples}
1560
1561 \nameref{pseudoop:if} is a handy way to comment out large sections of code or text. There is a caveat to this method - the code is still parsed by \dasm while looking for the \nameref{pseudoop:endif}, so this can have some unexpected side-effects if further conditionals are encountered.
1562
1563 \begin{code}
1564 IF 0
1565 ; disabled block that won't assemble
1566 ENDIF
1567 \end{code}\\
1568
1569
1570
1571 Paired with \nameref{pseudoop:endif}, \nameref{pseudoop:else}.\\
1572
1573 \hrule
1574
1575 \subsection{\texttt{ELSE}}
1576 \label{pseudoop:else}
1577 \index{Directives!Conditionals!\texttt{ELSE}}
1578
1579 \begin{usage}
1580 ELSE
1581 \end{usage}
1582
1583 Begin an \nameref{pseudoop:else} block for the current conditional.
1584
1585 If the current conditional is \nameref{pseudoop:if} and \mono{exp} is undefined, the \nameref{pseudoop:else} will not be executed.
1586
1587 Paired with \nameref{pseudoop:if}, \nameref{pseudoop:ifconst}, \nameref{pseudoop:ifnconst}.\\
1588
1589
1590 \hrule
1591
1592
1593 \subsection{\texttt{ENDIF}, \texttt{EIF}}
1594 \label{pseudoop:endif}
1595 \label{pseudoop:eif}
1596 \index{Directives!Conditionals!\texttt{ENDIF}}
1597 \index{Directives!Conditionals!\texttt{EIF}}
1598
1599 \begin{usage}
1600 ENDIF
1601 EIF
1602 \end{usage}
1603
1604 Terminate a conditional block.
1605
1606 \nameref{pseudoop:endif} are equivalent.
1607
1608 Paired with \nameref{pseudoop:if}, \nameref{pseudoop:ifconst}, \nameref{pseudoop:ifnconst}.\\
1609
1610 \hrule
1611
1612
1613 \section{Code Generation}
1614
1615 There are two sets of directives that provide ways to insert meta-blocks of code and/or data. These are the \nameref{pseudoop:repeat}/\nameref{pseudoop:repend} pair, and \nameref{pseudoop:macro}s, which are described in their own chapter.
1616
1617 See \nameref{pseudoop:macro}.
1618
1619 \subsection{\texttt{REPEAT}}
1620 \label{pseudoop:repeat}
1621 \index{Directives!Code Generation!\texttt{REPEAT}}
1622
1623 \begin{usage}
1624 REPEAT exp
1625 ;body...
1626 REPEND
1627 \end{usage}
1628
1629 \mono{exp} copies of the body are inserted at the current location, and assembled.
1630
1631 This looks like a loop, but it isn't. It's a text-insert of \mono{exp} blocks of code, so beware of code bloat when using this construct. \nameref{pseudoop:repeat}/\nameref{pseudoop:repend} can be very useful for data table generation.
1632
1633 If \mono{exp==0}, the body is ignored.
1634
1635 If \mono{exp<0}, a warning ``\mono{REPEAT parameter < 0 (ignored)}'' is output and the body is ignored.
1636
1637 \subsubsection{Example}
1638
1639 \begin{code}[caption=Data Generation Using Nested Repeat Loops]
1640 YV SET 2
1641 REPEAT 2
1642 XV SET 2
1643 REPEAT 4
1644 .byte XV, YV, XV*YV
1645 XV SET XV+1
1646 REPEND
1647 YV SET YV+1
1648 REPEND
1649 \end{code}
1650
1651 The above example generates the following code, which is then assembled:
1652
1653 \begin{code}
1654 .byte 2, 2, 4
1655 .byte 3, 2, 6
1656 .byte 4, 2, 8
1657 .byte 5, 2, 10
1658 .byte 2, 3, 6
1659 .byte 3, 3, 9
1660 .byte 4, 3, 12
1661 .byte 5, 3, 15
1662 \end{code}
1663
1664 Labels within a \nameref{pseudoop:repeat} block should be local labels, preceeded by a
1665 \nameref{pseudoop:subroutine} directive to keep them unique.
1666
1667 \subsubsection{Example}
1668 \begin{code}
1669 ; Use SUBROUTINE to delineate local label usage
1670 VAL SET 0
1671 REPEAT 4
1672 SUBROUTINE
1673 cmp #VAL
1674 bne .reused ; reused local label
1675 ; do something here
1676 jmp .exit
1677 .reused
1678 VAL SET VAL+1
1679 REPEND
1680 .exit
1681 \end{code}
1682
1683 The above example generates 4 blocks of code, each comparing with a specific immediate value and branching to a re-used local label which is made distinct by the use of the \nameref{pseudoop:subroutine} directive.
1684
1685 Paired with \nameref{pseudoop:repend}.\\
1686
1687 \hrule
1688
1689 \subsection{\texttt{REPEND}}
1690 \label{pseudoop:repend}
1691 \index{Directives!Code Generation!\texttt{REPEND}}
1692
1693 \begin{usage}
1694 REPEND
1695 \end{usage}
1696
1697 Bottom or a \nameref{pseudoop:repeat}/\nameref{pseudoop:repend} block. They must be in matched pairs.
1698
1699 Any label to the left of a \nameref{pseudoop:repend} is assigned \textbf{after} the complete text insert for the \nameref{pseudoop:repeat}/\nameref{pseudoop:repend} block has finished.
1700
1701 Paired with \nameref{pseudoop:repeat}.\\
1702
1703
1704 \hrule
1705
1706 \section{Structure}
1707
1708 \subsection{\texttt{ORG}}
1709 \label{pseudoop:org}
1710 \index{Directives!Control!\texttt{ORG}}
1711
1712 \begin{usage}
1713 ORG exp[,fill]
1714 \end{usage}
1715
1716 This directive sets the current origin. You can also set the
1717 global default fill character (a byte value) with this
1718 directive. No filler data are generated until the first
1719 data-generating opcode/directive is encountered after this one.
1720
1721 Sequences like:
1722
1723 \begin{code}
1724 org 0,255
1725 org 100,0
1726 org 200
1727 dc 23
1728 \end{code}
1729
1730 ... will result in 200 zeroes and a 23. This allows you to specify
1731 some \nameref{pseudoop:org}, then change your mind and specify some other (lower
1732 address) \nameref{pseudoop:org} without causing an error (assuming nothing is
1733 generated in-between).
1734
1735 Normally, \nameref{pseudoop:ds} and \nameref{pseudoop:align} are used to generate specific filler
1736 values.
1737
1738 Any label on the \nameref{pseudoop:org} line will be allocated its value after the directive is processed.\\
1739
1740 \hrule
1741
1742
1743
1744 \subsection{\texttt{RORG}}
1745 \label{pseudoop:rorg}
1746 \index{Directives!Control!\texttt{RORG}}
1747
1748 \begin{usage}
1749 RORG exp
1750 \end{usage}
1751
1752 This activates the relocatable origin. All generated
1753 addresses, including `.', although physically placed at the
1754 true origin, will use values from the relocatable origin.
1755 While in effect both the physical origin and relocatable origin
1756 are updated.
1757
1758 The relocatable origin can skip around (no limitations). The
1759 relocatable origin is a function of the segment. That is, you
1760 can still \nameref{pseudoop:seg} to another segment that does not have a
1761 relocatable origin activated, do other (independant) stuff
1762 there, and then switch back to the current segment and continue
1763 where you left off.
1764
1765 Any label on the \nameref{pseudoop:rorg} line will be allocated its value after the directive is processed.
1766
1767
1768 \subsection{\texttt{REND}}
1769 \label{pseudoop:rend}
1770 \index{Directives!Control!\texttt{REND}}
1771
1772 \begin{usage}
1773 REND
1774 \end{usage}
1775
1776
1777 Deactivate the relocatable origin for the current segment.
1778 Generation uses the real origin for reference.
1779
1780 Any label on the \nameref{pseudoop:rend} line will be allocated its value after the directive is processed.\\
1781
1782 \hrule
1783
1784
1785
1786 \subsection{\texttt{SEG}}
1787 \label{pseudoop:seg}
1788 \index{Directives!Control!\texttt{SEG}}
1789
1790 \begin{usage}
1791 SEG[.U] [name]
1792 \end{usage}
1793
1794 This switches to a new segment, creating it if neccessary. If the optional \mono{.U} extension is present, the segment is an \textbf{uninitialised} segment. Segments may be defined in parts; the \mono{.U} is not needed when going back to an already created uninitialized segment, though it makes the code more readable.
1795
1796 Unitialised segments are particularly useful for declaring variable locations without writing data to the binary output. They have no origin restrictions. This is useful for determining the size of a certain assembly sequence without generating code, and for assigning RAM addresses to labels.
1797
1798 An uninitialised segment with a \mono{name} will result in the generation of a warning for a \mono{reference to an unkonwn symbol}. This is harmless, but a good reason not to name uninitialised segments.
1799
1800 For segments which are not uninitialised, the segment name is used when producing the diagnostic output at the end of each pass to indicate the memory usage of the named segments. For uninitialised segments, use of a segment name will generate a ``reference to undefined symbol'' warning that can be ignored.
1801
1802 Any label on the \mono{SEG} line will be allocated its value after the directive is processed.
1803
1804 The following should be considered when generating ROMs:
1805
1806 \begin{itemize}
1807 \item The default fill character when using \nameref{pseudoop:org} (and \nameref{flag:definesymbolF} \mono{-f1} or \mono{-f3}) to
1808 skip forward in segments is 0. This is a \textbf{global} default and affects all segments.
1809 \item The fill value for \nameref{pseudoop:ds} has nothing to do with segment space padding, so don't confuse them!
1810 \end{itemize}
1811
1812 \subsubsection{Example}
1813
1814 \begin{code}[caption=Declaration of Variables]
1815 ; Declaration of zero page variables
1816 SEG.U variables
1817 ORG $80
1818 foo1 ds 1
1819 bar2 ds 10
1820 varn ds 2
1821 \end{code}
1822
1823 In the example shown above, the \mono{variables} segment is unitialised. The zero-page variables (starting at location \mono{\$80}) \mono{foo1}, \mono{bar2}, and \mono{varn} are declared using \mono{DS} directive to ``reserve/allocate'' appropriate amounts of memory. Their addresses are automatically calculated by \dasm. The relevant part of the symbol table is shown below, to make clear that although the segment is unitialised, the labels/variables have correct values.
1824
1825 \begin{outputx}
1826 foo1 0080
1827 bar2 0081
1828 varn 008b
1829 \end{outputx}\\
1830
1831 \hrule
1832
1833 \subsection{\texttt{ALIGN}}
1834 \label{pseudoop:align}
1835 \index{Directives!Control!\texttt{ALIGN}}
1836
1837 \begin{usage}
1838 ALIGN n[,fill]
1839 \end{usage}
1840
1841 Align the current program counter to an n-byte boundry. If the \mono{fill} option is present, then that value will be used to fill the space generated. The default fill value is \mono{0}. The \nameref{pseudoop:align} default value should not be confused with the \nameref{pseudoop:org}
1842 directive's default fill value.
1843
1844 Any label on the \nameref{pseudoop:align} line will be associated its value after the directive is processed.
1845
1846 \subsubsection{Example}
1847
1848 \begin{code}
1849 ; using ALIGN to move to 256-byte page boundary
1850 ORG $1000
1851 DS 10
1852 ; origin now $100A
1853 ALIGN 256
1854 ; origin now $1100
1855 \end{code}\\
1856
1857 \hrule
1858
1859
1860
1861
1862 \section{Control}
1863
1864
1865 \subsection{\texttt{PROCESSOR}}
1866 \label{changelog:20200824processor}
1867 \label{pseudoop:processor}
1868 \index{Directives!Control!\texttt{PROCESSOR}}
1869 \index{Processor}
1870
1871 \begin{usage}
1872 PROCESSOR type
1873 \end{usage}
1874
1875 \dasm needs to know the target microprocessor for which it is assembling the code.
1876
1877 This is indicated via the \nameref{pseudoop:processor} directive, which should
1878 be the first line (other than whitespace and comments) in your source code file.
1879 Only one \nameref{pseudoop:processor} directive may be declared in the entire assembly.
1880
1881
1882 The \nameref{pseudoop:processor} directive appears in the source code before the declaration of code origin, and thus any label present on the same line will remain unresolved at the end of assembly, causing an error.
1883
1884 Thus, do not place a label on the \nameref{pseudoop:processor} line.
1885
1886 \subsubsection{Supported Microprocessors}
1887 \index{Processor!Supported Microprocessors}
1888 \begin{table}[H]
1889 \begin{tabularx}{\textwidth}{llll}
1890 \toprule
1891 \textbf{\mono{type}}&\textbf{Identity}&\textbf{Endian}&\textbf{Byte Order}\\
1892 \hline
1893 \\
1894 \nameref{processor:6502} &MOS Technology 6502 &little-endian &\mono{LSB}, \mono{MSB}\\
1895 \nameref{processor:68HC11} &Motorola 68HC11 &big-endian &\mono{MSB}, \mono{LSB}\\
1896 \nameref{processor:68705} &Motorola 68705 &big-endian &\mono{MSB}, \mono{LSB}\\
1897 \nameref{processor:6803} &Motorola 6803 &big-endian &\mono{MSB}, \mono{LSB}\\
1898 \nameref{processor:6303} &Hitachi HD6303 &big-endian &\mono{MSB}, \mono{LSB}\\
1899 \nameref{processor:f8} &Fairchild F8 &big-endian &\mono{MSB}, \mono{LSB}\\
1900 \nameref{processor:MC68HC908} &Motorola MC68HC908&big-endian& \mono{MSB}, \mono{LSB}\\
1901 \\
1902 \bottomrule
1903 \end{tabularx}
1904 \end{table}
1905
1906 \subsubsection{Example}
1907 \begin{code}
1908 PROCESSOR 6502
1909 \end{code}
1910
1911 \index{Processor!Atari 2600}
1912 For the \mono{6507} microprocessor (as used in the Atari 2600 machine), use ``\nameref{pseudoop:processor} \mono{6502}'' as these two microprocessors are identical except for their addressing range.
1913
1914 Different processor models use different endianness (byte ordering of word values, being little-endian or big-endian). The processor's endiannessdoes not affect the header in the output files (\mono{-f1} and \mono{-f2}), which are always little-endian (\mono{LSB}, \mono{MSB}). The processor byte ordering affects all address, word, andlong values.\\
1915
1916 \hrule
1917
1918 \subsection{\texttt{ECHO}}
1919 \label{pseudoop:echo}
1920 \index{Directives!Control!\texttt{ECHO}}
1921
1922 \begin{usage}
1923 ECHO exp[,exp...]
1924 \end{usage}
1925
1926 The expressions (which may also be strings), are echoed on the
1927 screen and into the list file.\\
1928
1929 \index{Number Format!Decimal Display of}
1930 To output values in decimal use the format \mono{[exp]d}
1931
1932 \subsubsection{Example}
1933
1934 \begin{code}
1935 answer = 42
1936 ECHO "Hex=",answer,"Decimal=",[answer]d
1937 \end{code}
1938
1939 \begin{outputx}
1940 Hex= $2a Decimal= 42
1941 \end{outputx}
1942
1943 \hrule
1944
1945 \subsection{\texttt{SUBROUTINE}}
1946 \label{pseudoop:subroutine}
1947 \index{Directives!Control!\texttt{SUBROUTINE}}
1948
1949 \begin{usage}
1950 SUBROUTINE [name]
1951 \end{usage}
1952
1953 This isn't really a subroutine, but a boundary that resets the scope of \nameref{locallabels}. Those which are defined before the \nameref{pseudoop:subroutine} directive are not visible after it.
1954
1955 \index{Labels!Scope}
1956 \index{Scope!Local Labels}
1957 Local labels must be unique within the scope of the subroutine in which they are defined, and cannot be accessed outside of that scope. Local label names do not need to be unique, provided that they are not duplicated within a single scope. In other words, names can be re-used.
1958
1959 \index{Macros!Scope}
1960 \index{Scope!Macros}
1961 Macros implicitly define a new subroutine scope both at their beginning, and end. Local labels defined inside a macro are not avalable outside it, and local labels defined before a macro usage instance are also no longer visible after the instantiation. Automatic new local label scope boundries occur for each macro level.
1962
1963
1964 \subsubsection{Example}
1965
1966 \begin{code}
1967 Fn10
1968 SUBROUTINE
1969 .loop dex ; 1st definition of .loop
1970 bne .loop ; branches to 1st .loop
1971 .exit rts
1972
1973 Fn20 SUBROUTINE
1974
1975 ; new scope here because of the SUBROUTINE directive
1976 ; previous local labels are no longer reachable
1977
1978 .loop dex ; 2nd definition of .loop
1979 bne .loop ; branches to 2nd .loop
1980
1981 jmp .exit ; ERROR - out of scope
1982
1983 \end{code}
1984
1985 The above example defines two functions (\mono{Fn10}, \mono{Fn20}) which both use the local label \mono{.loop}. The correct label for each is used by the branch, by way of the \nameref{pseudoop:subroutine} directive setting local scope. If the second \nameref{pseudoop:subroutine} directive was removed, the assembler would generate an error because of the duplicate label.
1986
1987 Note that the function name label can be on the same line as the directive, if desired.
1988
1989 \index{Scope!Implicit}
1990 An implicit \nameref{pseudoop:subroutine} scope is in effect when \nameref{macros} are instantiated, so local labels cannot be accessed spanning a macro instantiation.\\
1991
1992 \subsubsection{Example}
1993
1994 \begin{code}
1995 MAC DO
1996 ; body
1997 ENDM
1998
1999 .lab
2000 jmp .lab ; OK
2001
2002 DO ; instantiate macro
2003
2004 jmp .lab ; ERROR
2005 \end{code}
2006
2007 See \nameref{pseudoop:mac}.
2008
2009
2010 \hrule
2011 \subsection{\texttt{ERR}}
2012 \label{pseudoop:err}
2013 \index{Directives!Control!\texttt{ERR}}
2014
2015 \begin{usage}
2016 ERR
2017 \end{usage}
2018
2019 Abort assembly. Useful in conjunction with \nameref{conditionals} to end an essembly if required.
2020
2021 \subsubsection{Example}
2022
2023 \begin{code}
2024 MAC CALL ; function name
2025 IFNCONST {1}
2026 FNAME SETSTR {1}
2027 ECHO FNAME," does not exist!"
2028 ERR
2029 ELSE
2030 jsr {1}
2031 ENDIF
2032 ENDM
2033
2034 test
2035
2036 CALL test ; OK
2037 CALL test2 ; "test2 does not exist!" then halts
2038 \end{code}
2039
2040 \begin{outputx}
2041 test2 does not exist!
2042 source.asm (37): error: ERR pseudo-op encountered
2043 \end{outputx}
2044
2045 \hrule
2046
2047
2048
2049 \subsection{\texttt{LIST}}
2050 \label{pseudoop:list}
2051 \index{Directives!Control!\texttt{LIST ON}}
2052 \index{Directives!Control!\texttt{LIST OFF}}
2053
2054 \begin{usage}
2055 LIST ON|OFF
2056 \end{usage}
2057
2058 Globally turns listing on or off, starting with the current
2059 line.
2060
2061 When you use \nameref{pseudoop:list} the effect is local to the
2062 current macro or included file. For a line to be listed both
2063 the global and local list switches must be on.\\
2064
2065 \hrule
2066
2067
2068 \subsection{\texttt{.FORCE}}
2069 \label{pseudoop:force}
2070 \index{Directives!\texttt{[.FORCE]}}
2071 \index{Addressing!.0 Implied}
2072 \index{Addressing!.0x Implied (0,x)}
2073 \index{Addressing!.0y Implied, (0,y)}
2074 \index{Addressing!.a Absolute}
2075 \index{Addressing!.b Byte}
2076 \index{Addressing!.bx Byte Address Indexed, x}
2077
2078 \begin{usage}
2079 mnemonic[.force]
2080 \end{usage}
2081
2082 FORCE extensions (placed after a mnemonic) are used to force an addressing mode. In some cases,
2083 you can optimize the assembly to take fewer passes by telling it the
2084 addressing mode. Force extensions are also used with \mono{DS},\mono{DC}, and \mono{DV}
2085 to determine the element size.
2086
2087 \textbf{Not all extensions are available for all processor types.}
2088
2089 \begin{table}[H]
2090 \begin{tabularx}{\linewidth}{cl}
2091 \toprule
2092 \textbf{Extension} & \textbf{Function}\\
2093 \hline
2094 \\
2095 \mono{.0} &Implied\\
2096 \mono{.0x} &Implied indexing \mono{(0,x)}\\
2097 \mono{.0y} &Implied indexing \mono{(0,y)}\\
2098 \mono{.a} &Absolute (equivalent to \mono{.e}, \mono{.w})\\
2099 \mono{.b} &\mono{byte} (equivalent to \mono{.d}, \mono{.z})\\
2100 \mono{.bx} &\mono{byte} address indexed \mono{x}\\
2101 \mono{.by} &\mono{byte} address indexed \mono{y}\\
2102 \mono{.d} &Direct (equivalent to \mono{.b}, \mono{.z})\\
2103 \mono{.e} &Extended (equivalent to \mono{.a}, \mono{.w})\\
2104 \mono{.i} &Implied\\
2105 \mono{.ind} &Indirect \mono{word}\\
2106 \mono{.l} &\mono{long word} (4 bytes) (\mono{DS}/\mono{DC}/\mono{DV})\\
2107 \mono{.r} &Relative\\
2108 \mono{.u} &Uninitialized (\nameref{pseudoop:seg})\\
2109 \mono{.w} &\mono{word} address (equivalent to \mono{.a}, \mono{.e})\\
2110 \mono{.wx} &\mono{word} address indexed \mono{x}\\
2111 \mono{.wy} &\mono{word} address indexed \mono{y}\\
2112 \mono{.z} &Zero page (equivalent to \mono{.b}, \mono{.d})\\
2113 \\
2114 \bottomrule
2115 \end{tabularx}
2116 \end{table}
2117
2118
2119 \label{changelog:20200829substitutions}
2120 First character equivalent substitutions:
2121
2122 \begin{table}[H]
2123 \begin{tabularx}{\linewidth}{cccl}
2124 \toprule
2125 \textbf{Orig}&\textbf{Alt}&\textbf{Alt}&\textbf{Meaning}\\
2126 \hline
2127 \\
2128 \mono{b} &\mono{z} &\mono{d} & byte, zeropage, direct\\
2129 \mono{w} &\mono{e} &\mono{a} & word, extended, absolute\\
2130 \bottomrule
2131 \end{tabularx}
2132 \end{table}
2133
2134
2135
2136 \begin{savequote}
2137 \sffamily
2138 ``Everyone is against micro managing but macro managing means you're working at the big picture but don't know the details.''
2139 \qauthor{Henry Mintzberg}
2140 \end{savequote}
2141
2142 \chapter{Macros}
2143 \label{macros}
2144 \index{Macros}
2145
2146 Macros are user-defined \nameref{chapter:pseudoops}, and when used well they can provide extremely powerful code constructs and simplify programming.
2147
2148 A macro is effectively a text-substitution template. Wherever the name of a macro is used, the body of the macro is inserted. During the insertion, parameters passed to the macro may be substituted inside the body as specified by the macro definition.
2149
2150 \includegraphics[width=1cm]{important}Macros automatically generate an implicit \nameref{pseudoop:subroutine} when instantiated, which guarantees distinct local labels for that macro instance.
2151
2152 This can sometimes be inconvenient, as it can ``hide'' local labels in code using the macro, but there is currently no way known to prevent this.
2153
2154 \section{Usage}
2155
2156 \subsection{\texttt{MAC}, \texttt{MACRO}}
2157 \label{pseudoop:mac}
2158 \label{pseudoop:macro}
2159 \index{Macros}
2160 \index{Macros!\texttt{MAC}}
2161 \index{Macros!\texttt{MACRO}}
2162 %\index{Directives!Macros!\texttt{MAC}}
2163 %\index{Directives!Macros!\texttt{MACRO}}
2164
2165 \begin{usage}
2166 ; Declaration
2167 ; parameters available as {1}, {2}, etc.
2168 ; {0} = full instantiation line
2169 MAC name
2170 ; body line 1
2171 ; ...
2172 ; body line n
2173 ENDM
2174 \end{usage}
2175
2176 \begin{usage}
2177 ; Instantiation
2178 name param1, param2, ...
2179 \end{usage}
2180
2181 \nameref{pseudoop:mac} are equivalent.
2182
2183
2184 Source code lines between \nameref{pseudoop:mac} and \nameref{pseudoop:endm} are the macro's body.
2185 You cannot recursively declare a macro. You can, however, recursively
2186 use a macro (reference a macro in a macro).
2187
2188 No label is allowed on the macro declaration line.
2189
2190 The macro name is not case-sensitive, either in declaration or use.
2191
2192 Macros can be redefined, so beware of potential issues related to unexpected usage.
2193
2194 You should always use \nameref{locallabels} (e.g., \mono{.loop}) inside macros which you use
2195 more than once.
2196
2197 Macros are instantiated by using the macro's name (case-insensitive), followed by an optional list of arguments. The body of the macro definition can refer to arguments passed with the format ``\mono{\{\#\}}'', where \mono{\#} is replaced by the argument number. The first argument passed to a macro is therefore \mono{\{1\}}. \mono{\{0\}} represents an exact substitution of the entire instantiation line.
2198
2199 \subsubsection{Examples}
2200
2201 \begin{code}[caption=Vector Table Generation using a MACRO]
2202 ; Generate low/high tables pointing to functions
2203
2204 ; Uses a macro to contain the list of functions,
2205 ; and the parameter to declare low byte or high byte
2206
2207 MAC VECTORS
2208 ; usage: {1} is < or >
2209 .byte {1}Routine1
2210 .byte {1}Routine2
2211 .byte {1}Routine3
2212 ENDM
2213
2214 LoTable VECTORS <
2215 HiTable VECTORS >
2216 \end{code}
2217
2218 In the above example, a list of pointers to functions is generated in two tables (one containing the low addresses of the functions, and the other the high addresses). These two tables are always in-synch (no extra or missing entries) through the single-point definitino in the macro itself.
2219
2220 The two calls to the macro generate the low bytes and the high bytes into two separate tables. This will result in the following code being generated, and then inserted into the source code in place of the macro calls...
2221
2222 \begin{code}[caption=Output]
2223 LoTable
2224 .byte <Routine1
2225 .byte <Routine2
2226 .byte <Routine3
2227 HiTable
2228 .byte >Routine1
2229 .byte >Routine2
2230 .byte >Routine3
2231
2232 \end{code}
2233
2234 \subsubsection{Example}
2235
2236 \begin{code}
2237 ; Inserts a page break if the object would overlap a page
2238
2239 MAC OPTIONAL_PAGEBREAK ; { labelString, size }
2240 IF (>( * + {2} -1 )) > ( >* )
2241 .EARLY_LOCATION SET *
2242 ALIGN 256
2243 ECHO "Page break for", {1}
2244 ECHO "wasted", [* - .EARLY_LOCATION]d, "bytes"
2245 ENDIF
2246 ENDM
2247 \end{code}
2248
2249
2250
2251 Paired with \nameref{pseudoop:endm}.\\
2252
2253 \hrule
2254
2255
2256 \subsection{\texttt{ENDM}}
2257 \label{pseudoop:endm}
2258 %\index{Directives!Macros!\texttt{ENDM}}
2259 \index{Macros!\texttt{ENDM}}
2260
2261 \begin{usage}
2262 ENDM
2263 \end{usage}
2264
2265 End of macro definition.
2266
2267 \textbf{No label is allowed to the left of the directive.}
2268
2269 Paired with \nameref{pseudoop:mac}.\\
2270
2271 \hrule
2272
2273 \subsection{\texttt{MEXIT}}
2274 \index{Macros}
2275 %\index{Directives!Macros!\texttt{MEXIT}}
2276 \index{Macros!\texttt{MEXIT}}
2277
2278 \begin{usage}
2279 MEXIT
2280 \end{usage}
2281
2282 Used in conjuction with conditionals. Exits the current macro level.
2283
2284 See \nameref{conditionals}.
2285
2286