Merge pull request #237 from continue-lines

Allow to continue lines

Signed-off-by: Antonio Niño Díaz <antonio_nd@outlook.com>
This commit is contained in:
Antonio Niño Díaz
2018-02-26 21:55:19 +00:00
5 changed files with 314 additions and 72 deletions

View File

@@ -34,8 +34,12 @@ Note: All arithmetic/logic operations that use register
is assumed it's register <b class="Sy" title="Sy">A</b>. The following two is assumed it's register <b class="Sy" title="Sy">A</b>. The following two
lines have the same effect: lines have the same effect:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">OR A,B</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<div class="D1"><code class="Li">OR B</code></div> <pre class="Li">
OR A,B
OR B
</pre>
</div>
<h1 class="Sh" title="Sh" id="LEGEND"><a class="selflink" href="#LEGEND">LEGEND</a></h1> <h1 class="Sh" title="Sh" id="LEGEND"><a class="selflink" href="#LEGEND">LEGEND</a></h1>
List of abbreviations used in this document. List of abbreviations used in this document.
<dl class="Bl-tag"> <dl class="Bl-tag">
@@ -1689,7 +1693,7 @@ Flags: See <a class="Sx" title="Sx" href="#XOR_A,r8">XOR A,r8</a>
<a class="Lk" title="Lk" href="https://github.com/rednex/rgbds">https://github.com/rednex/rgbds</a>.</div> <a class="Lk" title="Lk" href="https://github.com/rednex/rgbds">https://github.com/rednex/rgbds</a>.</div>
<table class="foot"> <table class="foot">
<tr> <tr>
<td class="foot-date">January 26, 2018</td> <td class="foot-date">February 23, 2018</td>
<td class="foot-os">RGBDS Manual</td> <td class="foot-os">RGBDS Manual</td>
</tr> </tr>
</table> </table>

View File

@@ -38,7 +38,11 @@ The syntax is line&#x2010;based, just as in any other assembler, meaning that
<div class="Pp"></div> <div class="Pp"></div>
Example: Example:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">John: ld a,87 ;Weee</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
John: ld a,87 ;Weee
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
All pseudo&#x2010;ops, mnemonics and registers (reserved keywords) are All pseudo&#x2010;ops, mnemonics and registers (reserved keywords) are
case&#x2010;insensitive and all labels are case&#x2010;sensitive. case&#x2010;insensitive and all labels are case&#x2010;sensitive.
@@ -47,13 +51,40 @@ There are two syntaxes for comments. In both cases, a comment ends at the end of
the line. The most common one is: anything that follows a semicolon the line. The most common one is: anything that follows a semicolon
&quot;;&quot; (that isn't inside a string) is a comment. There is another &quot;;&quot; (that isn't inside a string) is a comment. There is another
format: anything that follows a &quot;*&quot; that is placed right at the format: anything that follows a &quot;*&quot; that is placed right at the
start of a line is a comment. start of a line is a comment. The assembler removes all comments from the code
before doing anything else.
<div class="Pp"></div>
Sometimes lines can be too long and it may be necessary to split them. The
syntax to do so is the following one:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
DB 1, 2, 3, 4 \
5, 6, 7, 8
</pre>
</div>
<div class="Pp"></div>
This works anywhere in the code except inside of strings. To split strings it is
needed to use
<div>&#x00A0;</div>
like this:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
DB STRCAT(&quot;Hello &quot;, \
&quot;world!&quot;)
</pre>
</div>
<h2 class="Ss" title="Ss" id="Sections"><a class="selflink" href="#Sections">Sections</a></h2> <h2 class="Ss" title="Ss" id="Sections"><a class="selflink" href="#Sections">Sections</a></h2>
Before you can start writing code, you must define a section. This tells the Before you can start writing code, you must define a section. This tells the
assembler what kind of information follows and, if it is code, where to put assembler what kind of information follows and, if it is code, where to put
it. it.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION &quot;CoolStuff&quot;,ROMX</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
SECTION &quot;CoolStuff&quot;,ROMX
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
This switches to the section called &quot;CoolStuff&quot; (or creates it if it This switches to the section called &quot;CoolStuff&quot; (or creates it if it
doesn't already exist) and it defines it as a code section. All sections doesn't already exist) and it defines it as a code section. All sections
@@ -139,25 +170,38 @@ If a section is defined with no indications, it is a floating section. The
obligation to follow any specific rules. The following example defines a obligation to follow any specific rules. The following example defines a
section that can be placed anywhere in any ROMX bank: section that can be placed anywhere in any ROMX bank:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION &quot;CoolStuff&quot;,ROMX</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
SECTION &quot;CoolStuff&quot;,ROMX
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
If it is needed, the following syntax can be used to fix the base address of the If it is needed, the following syntax can be used to fix the base address of the
section: section:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION <div class="Bd" style="margin-left: 5.00ex;">
&quot;CoolStuff&quot;,ROMX[$4567]</code></div> <pre class="Li">
SECTION &quot;CoolStuff&quot;,ROMX[$4567]
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
It won't, however, fix the bank number, which is left to the linker. If you also It won't, however, fix the bank number, which is left to the linker. If you also
want to specify the bank you can do: want to specify the bank you can do:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION <div class="Bd" style="margin-left: 5.00ex;">
&quot;CoolStuff&quot;,ROMX[$4567],BANK[3]</code></div> <pre class="Li">
SECTION &quot;CoolStuff&quot;,ROMX[$4567],BANK[3]
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
And if you only want to force the section into a certain bank, and not it's And if you only want to force the section into a certain bank, and not it's
position within the bank, that's also possible: position within the bank, that's also possible:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION <div class="Bd" style="margin-left: 5.00ex;">
&quot;CoolStuff&quot;,ROMX,BANK[7]</code></div> <pre class="Li">
SECTION &quot;CoolStuff&quot;,ROMX,BANK[7]
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
In addition, you can specify byte alignment for a section. This ensures that the In addition, you can specify byte alignment for a section. This ensures that the
section starts at a memory address where the given number of least-significant section starts at a memory address where the given number of least-significant
@@ -167,11 +211,13 @@ In addition, you can specify byte alignment for a section. This ensures that the
needed to align the start of an array to 256 bytes to optimize the code that needed to align the start of an array to 256 bytes to optimize the code that
accesses it. accesses it.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">SECTION &quot;OAM Data&quot;,WRAM0,ALIGN[8]; <div class="Bd" style="margin-left: 5.00ex;">
align to 256 bytes</code></div> <pre class="Li">
<div class="Pp"></div> SECTION &quot;OAM Data&quot;,WRAM0,ALIGN[8] ; align to 256 bytes
<div class="D1"><code class="Li">SECTION &quot;VRAM
Data&quot;,ROMX,BANK[2],ALIGN[4]; align to 16 bytes</code></div> SECTION &quot;VRAM Data&quot;,ROMX,BANK[2],ALIGN[4] ; align to 16 bytes
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
HINT: If you think this is a lot of typing for doing a simple HINT: If you think this is a lot of typing for doing a simple
<b class="Ic" title="Ic">ORG</b> type thing you can quite easily write an <b class="Ic" title="Ic">ORG</b> type thing you can quite easily write an
@@ -260,8 +306,12 @@ ThisWillBeExported.too::
EQUates are constant symbols. They can, for example, be used for things such EQUates are constant symbols. They can, for example, be used for things such
as bit-definitions of hardware registers. as bit-definitions of hardware registers.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">EXIT_OK EQU $00</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<div class="D1"><code class="Li">EXIT_FAILURE EQU $01</code></div> <pre class="Li">
EXIT_OK EQU $00
EXIT_FAILURE EQU $01
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
Note that a colon (:) following the label-name is not allowed. EQUates Note that a colon (:) following the label-name is not allowed. EQUates
cannot be exported and imported. They don't change their value during the cannot be exported and imported. They don't change their value during the
@@ -285,7 +335,11 @@ COUNT SET ARRAY_SIZE+COUNT
be exported and imported. Alternatively you can use = as a synonym for be exported and imported. Alternatively you can use = as a synonym for
SET. SET.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">COUNT = 2</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
COUNT = 2
</pre>
</div>
</dd> </dd>
<dt class="It-hang"><b class="Sy" title="Sy">RSSET</b>, <dt class="It-hang"><b class="Sy" title="Sy">RSSET</b>,
<b class="Sy" title="Sy">RSRESET</b>, <b class="Sy" title="Sy">RB</b>, <b class="Sy" title="Sy">RSRESET</b>, <b class="Sy" title="Sy">RB</b>,
@@ -378,10 +432,10 @@ str_SIZEOF = 259
<div class="Bd" style="margin-left: 5.00ex;"> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li"> <pre class="Li">
COUNTREG EQUS &quot;[hl+]&quot; COUNTREG EQUS &quot;[hl+]&quot;
ld a,COUNTREG ld a,COUNTREG
PLAYER_NAME EQUS &quot;\&quot;John\&quot;&quot; PLAYER_NAME EQUS &quot;\&quot;John\&quot;&quot;
db PLAYER_NAME db PLAYER_NAME
</pre> </pre>
</div> </div>
<div class="Pp"></div> <div class="Pp"></div>
@@ -390,13 +444,20 @@ db PLAYER_NAME
<div class="Pp"></div> <div class="Pp"></div>
This will be interpreted as: This will be interpreted as:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">ld a,[hl+]</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<div class="D1"><code class="Li">db &quot;John&quot;</code></div> <pre class="Li">
ld a,[hl+]
db &quot;John&quot;
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
String-symbols can also be used to define small one-line macros: String-symbols can also be used to define small one-line macros:
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">PUSHA EQUS &quot;push af\npush bc\npush <div class="Bd" style="margin-left: 5.00ex;">
de\npush hl\n&quot;</code></div> <pre class="Li">
PUSHA EQUS &quot;push af\npush bc\npush de\npush hl\n&quot;
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
Note that a colon (:) following the label-name is not allowed. String Note that a colon (:) following the label-name is not allowed. String
equates can't be exported or imported. equates can't be exported or imported.
@@ -510,7 +571,11 @@ LoopyMacro: MACRO
address and the second being a bytecount. The macro will then reset all address and the second being a bytecount. The macro will then reset all
bytes in this range. bytes in this range.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">LoopyMacro MyVars,54</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
LoopyMacro MyVars,54
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
Arguments are passed as string equates. There's no need to enclose them in Arguments are passed as string equates. There's no need to enclose them in
quotes. An expression will not be evaluated first but passed directly. quotes. An expression will not be evaluated first but passed directly.
@@ -525,6 +590,21 @@ LoopyMacro: MACRO
use the first 9 like this. If you want to use the rest, you need to use use the first 9 like this. If you want to use the rest, you need to use
the keyword <b class="Ic" title="Ic">SHIFT</b>. the keyword <b class="Ic" title="Ic">SHIFT</b>.
<div class="Pp"></div> <div class="Pp"></div>
Line continuations work as usual inside macros or lists of arguments of
macros. Strings, however, are a bit trickier. The following example shows
how to use strings as arguments for a macro:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
PrintMacro : MACRO
PRINTT \1
ENDM
PrintMacro STRCAT(\&quot;Hello\&quot;\, \
\&quot; world\\n\&quot;)
</pre>
</div>
<div class="Pp"></div>
<b class="Ic" title="Ic">SHIFT</b> is a special command only available in <b class="Ic" title="Ic">SHIFT</b> is a special command only available in
macros. Very useful in REPT-blocks. It will &quot;shift&quot; the macros. Very useful in REPT-blocks. It will &quot;shift&quot; the
arguments by one &quot;to the left&quot;. <b class="Ic" title="Ic">\1</b> arguments by one &quot;to the left&quot;. <b class="Ic" title="Ic">\1</b>
@@ -710,8 +790,11 @@ The following symbols are defined by the assembler:
<b class="Ic" title="Ic">DB</b> defines a list of bytes that will be stored in <b class="Ic" title="Ic">DB</b> defines a list of bytes that will be stored in
the final image. Ideal for tables and text (which is not zero-terminated). the final image. Ideal for tables and text (which is not zero-terminated).
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">DB 1,2,3,4,&quot;This is a <div class="Bd" style="margin-left: 5.00ex;">
string&quot;</code></div> <pre class="Li">
DB 1,2,3,4,&quot;This is a string&quot;
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
Alternatively, you can use <b class="Ic" title="Ic">DW</b> to store a list of Alternatively, you can use <b class="Ic" title="Ic">DW</b> to store a list of
words (16-bits) or <b class="Ic" title="Ic">DL</b> to store a list of words (16-bits) or <b class="Ic" title="Ic">DL</b> to store a list of
@@ -738,8 +821,11 @@ You can also use <b class="Ic" title="Ic">DB</b>,
<b class="Ic" title="Ic">DW</b> and <b class="Ic" title="Ic">DL</b> without <b class="Ic" title="Ic">DW</b> and <b class="Ic" title="Ic">DL</b> without
any arguments instead. any arguments instead.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">DS str_SIZEOF ;allocate str_SIZEOF <div class="Bd" style="margin-left: 5.00ex;">
bytes</code></div> <pre class="Li">
DS str_SIZEOF ;allocate str_SIZEOF bytes
</pre>
</div>
<h2 class="Ss" title="Ss" id="Including_binary_files"><a class="selflink" href="#Including_binary_files">Including <h2 class="Ss" title="Ss" id="Including_binary_files"><a class="selflink" href="#Including_binary_files">Including
binary files</a></h2> binary files</a></h2>
You probably have some graphics you'd like to include. Use You probably have some graphics you'd like to include. Use
@@ -747,17 +833,23 @@ You probably have some graphics you'd like to include. Use
the file isn't found in the current directory, the include-path list passed to the file isn't found in the current directory, the include-path list passed to
the linker on the command line will be searched. the linker on the command line will be searched.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">INCBIN &quot;titlepic.bin&quot;</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<div class="D1"><code class="Li">INCBIN &quot;sprites/hero.bin&quot;&#x00A0;; <pre class="Li">
UNIX</code></div> INCBIN &quot;titlepic.bin&quot;
<div class="D1"><code class="Li">INCBIN &quot;sprites\\hero.bin&quot;&#x00A0;; INCBIN &quot;sprites/hero.bin&quot;&#x00A0;; UNIX
Windows</code></div> INCBIN &quot;sprites\\hero.bin&quot;&#x00A0;; Windows
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
You can also include only part of a file with You can also include only part of a file with
<b class="Ic" title="Ic">INCBIN</b>. The example below includes 256 bytes from <b class="Ic" title="Ic">INCBIN</b>. The example below includes 256 bytes from
data.bin starting from byte 78. data.bin starting from byte 78.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">INCBIN &quot;data.bin&quot;,78,256</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
INCBIN &quot;data.bin&quot;,78,256
</pre>
</div>
<h2 class="Ss" title="Ss" id="Unions"><a class="selflink" href="#Unions">Unions</a></h2> <h2 class="Ss" title="Ss" id="Unions"><a class="selflink" href="#Unions">Unions</a></h2>
Unions allow multiple memory allocations to share the same space in memory, like Unions allow multiple memory allocations to share the same space in memory, like
unions in C. This allows you to easily reuse memory for different purposes, unions in C. This allows you to easily reuse memory for different purposes,
@@ -872,7 +964,11 @@ Use <b class="Ic" title="Ic">INCLUDE</b> to process another assembler-file and
<b class="Ic" title="Ic">INCLUDE</b> calls infinitely (or until you run out of <b class="Ic" title="Ic">INCLUDE</b> calls infinitely (or until you run out of
memory, whichever comes first). memory, whichever comes first).
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">INCLUDE &quot;irq.inc&quot;</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
INCLUDE &quot;irq.inc&quot;
</pre>
</div>
<h2 class="Ss" title="Ss" id="Conditional_assembling"><a class="selflink" href="#Conditional_assembling">Conditional <h2 class="Ss" title="Ss" id="Conditional_assembling"><a class="selflink" href="#Conditional_assembling">Conditional
assembling</a></h2> assembling</a></h2>
The four commands <b class="Ic" title="Ic">IF</b>, The four commands <b class="Ic" title="Ic">IF</b>,
@@ -929,7 +1025,11 @@ The last one, Gameboy graphics, is quite interesting and useful. The values are
actually pixel values and it converts the &#x201C;chunky&#x201D; data to actually pixel values and it converts the &#x201C;chunky&#x201D; data to
&#x201C;planar&#x201D; data as used in the Gameboy. &#x201C;planar&#x201D; data as used in the Gameboy.
<div class="Pp"></div> <div class="Pp"></div>
<div class="D1"><code class="Li">DW `01012323</code></div> <div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
DW `01012323
</pre>
</div>
<div class="Pp"></div> <div class="Pp"></div>
Admittedly, an expression with just a single number is quite boring. To spice Admittedly, an expression with just a single number is quite boring. To spice
things up a bit there are a few operators you can use to perform calculations things up a bit there are a few operators you can use to perform calculations
@@ -1465,7 +1565,7 @@ The options that OPT can modify are currently: <b class="Sy" title="Sy">b</b>,
<a class="Lk" title="Lk" href="https://github.com/rednex/rgbds">https://github.com/rednex/rgbds</a>.</div> <a class="Lk" title="Lk" href="https://github.com/rednex/rgbds">https://github.com/rednex/rgbds</a>.</div>
<table class="foot"> <table class="foot">
<tr> <tr>
<td class="foot-date">February 24, 2018</td> <td class="foot-date">February 26, 2018</td>
<td class="foot-os">RGBDS Manual</td> <td class="foot-os">RGBDS Manual</td>
</tr> </tr>
</table> </table>

View File

@@ -638,6 +638,44 @@ scanagain:
} }
} }
/* Check for line continuation character */
if (*pLexBuffer == '\\') {
/*
* Look for line continuation character after a series of
* spaces. This is also useful for files that use Windows line
* endings: "\r\n" is replaced by " \n" before the lexer has the
* opportunity to see it.
*/
if (pLexBuffer[1] == ' ') {
pLexBuffer += 2;
while (1) {
if (*pLexBuffer == ' ') {
pLexBuffer++;
} else if (*pLexBuffer == '\n') {
pLexBuffer++;
nLineNo += 1;
goto scanagain;
} else {
errx(1, "Expected a new line after the continuation character.");
}
}
}
/* Line continuation character */
if (pLexBuffer[1] == '\n') {
pLexBuffer += 2;
nLineNo += 1;
goto scanagain;
}
/*
* If there isn't a newline character or a space, ignore the
* character '\'. It will eventually be handled by other
* functions like PutMacroArg().
*/
}
/* /*
* Try to match an identifier, macro argument (e.g. \1), * Try to match an identifier, macro argument (e.g. \1),
* or numeric literal. * or numeric literal.
@@ -725,6 +763,9 @@ static uint32_t yylex_MACROARGS(void)
case '\\': case '\\':
ch = '\\'; ch = '\\';
break; break;
case '"':
ch = '\"';
break;
case ',': case ',':
ch = ','; ch = ',';
break; break;
@@ -734,6 +775,32 @@ static uint32_t yylex_MACROARGS(void)
case '}': case '}':
ch = '}'; ch = '}';
break; break;
case ' ':
/*
* Look for line continuation character after a
* series of spaces. This is also useful for
* files that use Windows line endings: "\r\n"
* is replaced by " \n" before the lexer has the
* opportunity to see it.
*/
while (1) {
if (*pLexBuffer == ' ') {
pLexBuffer++;
} else if (*pLexBuffer == '\n') {
pLexBuffer++;
nLineNo += 1;
ch = 0;
break;
} else {
errx(1, "Expected a new line after the continuation character.");
}
}
break;
case '\n':
/* Line continuation character */
nLineNo += 1;
ch = 0;
break;
default: default:
maxLength = MAXSTRLEN - index; maxLength = MAXSTRLEN - index;
length = CopyMacroArg(&yylval.tzString[index], length = CopyMacroArg(&yylval.tzString[index],

View File

@@ -5,7 +5,7 @@
.\" .\"
.\" SPDX-License-Identifier: MIT .\" SPDX-License-Identifier: MIT
.\" .\"
.Dd February 24, 2018 .Dd February 26, 2018
.Dt RGBASM 5 .Dt RGBASM 5
.Os RGBDS Manual .Os RGBDS Manual
.Sh NAME .Sh NAME
@@ -26,7 +26,9 @@ one instruction or pseudoop per line:
.Pp .Pp
Example: Example:
.Pp .Pp
.Dl John: ld a,87 ;Weee .Bd -literal -offset indent
John: ld a,87 ;Weee
.Ed
.Pp .Pp
All pseudoops, mnemonics and registers (reserved keywords) are caseinsensitive All pseudoops, mnemonics and registers (reserved keywords) are caseinsensitive
and all labels are casesensitive. and all labels are casesensitive.
@@ -35,13 +37,35 @@ There are two syntaxes for comments. In both cases, a comment ends at the end of
the line. The most common one is: anything that follows a semicolon the line. The most common one is: anything that follows a semicolon
\[dq]\&;\[dq] (that isn't inside a string) is a comment. There is another \[dq]\&;\[dq] (that isn't inside a string) is a comment. There is another
format: anything that follows a \[dq]*\[dq] that is placed right at the start of format: anything that follows a \[dq]*\[dq] that is placed right at the start of
a line is a comment. a line is a comment. The assembler removes all comments from the code before
doing anything else.
.Pp
Sometimes lines can be too long and it may be necessary to split them. The
syntax to do so is the following one:
.Pp
.Bd -literal -offset indent
DB 1, 2, 3, 4 \[rs]
5, 6, 7, 8
.Ed
.Pp
This works anywhere in the code except inside of strings. To split strings it is
needed to use
.It STRCAT
like this:
.Pp
.Bd -literal -offset indent
DB STRCAT("Hello ", \[rs]
"world!")
.Ed
.Pp
.Ss Sections .Ss Sections
Before you can start writing code, you must define a section. Before you can start writing code, you must define a section.
This tells the assembler what kind of information follows and, if it is code, This tells the assembler what kind of information follows and, if it is code,
where to put it. where to put it.
.Pp .Pp
.Dl SECTION \[dq]CoolStuff\[dq],ROMX .Bd -literal -offset indent
SECTION \[dq]CoolStuff\[dq],ROMX
.Ed
.Pp .Pp
This switches to the section called "CoolStuff" (or creates it if it doesn't This switches to the section called "CoolStuff" (or creates it if it doesn't
already exist) and it defines it as a code section. already exist) and it defines it as a code section.
@@ -128,22 +152,30 @@ obligation to follow any specific rules.
The following example defines a section that can be placed anywhere in any ROMX The following example defines a section that can be placed anywhere in any ROMX
bank: bank:
.Pp .Pp
.Dl SECTION \[dq]CoolStuff\[dq],ROMX .Bd -literal -offset indent
SECTION \[dq]CoolStuff\[dq],ROMX
.Ed
.Pp .Pp
If it is needed, the following syntax can be used to fix the base address of the If it is needed, the following syntax can be used to fix the base address of the
section: section:
.Pp .Pp
.Dl SECTION \[dq]CoolStuff\[dq],ROMX[$4567] .Bd -literal -offset indent
SECTION \[dq]CoolStuff\[dq],ROMX[$4567]
.Ed
.Pp .Pp
It won't, however, fix the bank number, which is left to the linker. It won't, however, fix the bank number, which is left to the linker.
If you also want to specify the bank you can do: If you also want to specify the bank you can do:
.Pp .Pp
.Dl SECTION \[dq]CoolStuff\[dq],ROMX[$4567],BANK[3] .Bd -literal -offset indent
SECTION \[dq]CoolStuff\[dq],ROMX[$4567],BANK[3]
.Ed
.Pp .Pp
And if you only want to force the section into a certain bank, and not it's And if you only want to force the section into a certain bank, and not it's
position within the bank, that's also possible: position within the bank, that's also possible:
.Pp .Pp
.Dl SECTION \[dq]CoolStuff\[dq],ROMX,BANK[7] .Bd -literal -offset indent
SECTION \[dq]CoolStuff\[dq],ROMX,BANK[7]
.Ed
.Pp .Pp
In addition, you can specify byte alignment for a section. In addition, you can specify byte alignment for a section.
This ensures that the section starts at a memory address where the given number This ensures that the section starts at a memory address where the given number
@@ -155,9 +187,11 @@ However, if an alignment is specified, the base address must be left unassigned.
This can be useful when using DMA to copy data or when it is needed to align the This can be useful when using DMA to copy data or when it is needed to align the
start of an array to 256 bytes to optimize the code that accesses it. start of an array to 256 bytes to optimize the code that accesses it.
.Pp .Pp
.Dl SECTION \[dq]OAM Data\[dq],WRAM0,ALIGN[8] ; align to 256 bytes .Bd -literal -offset indent
.Pp SECTION \[dq]OAM Data\[dq],WRAM0,ALIGN[8] ; align to 256 bytes
.Dl SECTION \[dq]VRAM Data\[dq],ROMX,BANK[2],ALIGN[4] ; align to 16 bytes
SECTION \[dq]VRAM Data\[dq],ROMX,BANK[2],ALIGN[4] ; align to 16 bytes
.Ed
.Pp .Pp
HINT: If you think this is a lot of typing for doing a simple HINT: If you think this is a lot of typing for doing a simple
.Ic ORG .Ic ORG
@@ -255,8 +289,10 @@ EQUates are constant symbols.
They can, for example, be used for things such as bit-definitions of hardware They can, for example, be used for things such as bit-definitions of hardware
registers. registers.
.Pp .Pp
.Dl EXIT_OK EQU $00 .Bd -literal -offset indent
.Dl EXIT_FAILURE EQU $01 EXIT_OK EQU $00
EXIT_FAILURE EQU $01
.Ed
.Pp .Pp
Note that a colon (:) following the label-name is not allowed. Note that a colon (:) following the label-name is not allowed.
EQUates cannot be exported and imported. EQUates cannot be exported and imported.
@@ -278,7 +314,9 @@ Note that a colon (:) following the label-name is not allowed.
SETs cannot be exported and imported. SETs cannot be exported and imported.
Alternatively you can use = as a synonym for SET. Alternatively you can use = as a synonym for SET.
.Pp .Pp
.Dl COUNT = 2 .Bd -literal -offset indent
COUNT = 2
.Ed
.Pp .Pp
.It Sy RSSET , RSRESET , RB , RW .It Sy RSSET , RSRESET , RB , RW
.Pp .Pp
@@ -330,10 +368,10 @@ If you are familiar with C you can think of it as the same as #define.
.Pp .Pp
.Bd -literal -offset indent .Bd -literal -offset indent
COUNTREG EQUS "[hl+]" COUNTREG EQUS "[hl+]"
ld a,COUNTREG ld a,COUNTREG
PLAYER_NAME EQUS \[dq]\[rs]\[dq]John\[rs]\[dq]\[dq] PLAYER_NAME EQUS \[dq]\[rs]\[dq]John\[rs]\[dq]\[dq]
db PLAYER_NAME db PLAYER_NAME
.Ed .Ed
.Pp .Pp
Note that : following the label-name is not allowed, and that strings must be Note that : following the label-name is not allowed, and that strings must be
@@ -341,12 +379,16 @@ quoted to be useful.
.Pp .Pp
This will be interpreted as: This will be interpreted as:
.Pp .Pp
.Dl ld a,[hl+] .Bd -literal -offset indent
.Dl db \[dq]John\[dq] ld a,[hl+]
db \[dq]John\[dq]
.Ed
.Pp .Pp
String-symbols can also be used to define small one-line macros: String-symbols can also be used to define small one-line macros:
.Pp .Pp
.Dl PUSHA EQUS \[dq]push af\[rs]npush bc\[rs]npush de\[rs]npush hl\[rs]n\[dq] .Bd -literal -offset indent
PUSHA EQUS \[dq]push af\[rs]npush bc\[rs]npush de\[rs]npush hl\[rs]n\[dq]
.Ed
.Pp .Pp
Note that a colon (:) following the label-name is not allowed. Note that a colon (:) following the label-name is not allowed.
String equates can't be exported or imported. String equates can't be exported or imported.
@@ -459,7 +501,9 @@ Now I can call the macro specifying two arguments.
The first being the address and the second being a bytecount. The first being the address and the second being a bytecount.
The macro will then reset all bytes in this range. The macro will then reset all bytes in this range.
.Pp .Pp
.Dl LoopyMacro MyVars,54 .Bd -literal -offset indent
LoopyMacro MyVars,54
.Ed
.Pp .Pp
Arguments are passed as string equates. Arguments are passed as string equates.
There's no need to enclose them in quotes. There's no need to enclose them in quotes.
@@ -479,6 +523,19 @@ In reality, up to 256 arguments can be passed to a macro, but you can only use
the first 9 like this. If you want to use the rest, you need to use the keyword the first 9 like this. If you want to use the rest, you need to use the keyword
.Ic SHIFT . .Ic SHIFT .
.Pp .Pp
Line continuations work as usual inside macros or lists of arguments of macros.
Strings, however, are a bit trickier. The following example shows how to use
strings as arguments for a macro:
.Pp
.Bd -literal -offset indent
PrintMacro : MACRO
PRINTT \[rs]1
ENDM
PrintMacro STRCAT(\[rs]\[dq]Hello\[rs]\[dq]\[rs], \[rs]
\[rs]\[dq] world\[rs]\[rs]n\[rs]\[dq])
.Ed
.Pp
.Ic SHIFT .Ic SHIFT
is a special command only available in macros. is a special command only available in macros.
Very useful in REPT-blocks. Very useful in REPT-blocks.
@@ -565,7 +622,9 @@ The following symbols are defined by the assembler:
defines a list of bytes that will be stored in the final image. defines a list of bytes that will be stored in the final image.
Ideal for tables and text (which is not zero-terminated). Ideal for tables and text (which is not zero-terminated).
.Pp .Pp
.Dl DB 1,2,3,4,\[dq]This is a string\[dq] .Bd -literal -offset indent
DB 1,2,3,4,\[dq]This is a string\[dq]
.Ed
.Pp .Pp
Alternatively, you can use Alternatively, you can use
.Ic DW .Ic DW
@@ -609,7 +668,9 @@ and
.Ic DL .Ic DL
without any arguments instead. without any arguments instead.
.Pp .Pp
.Dl DS str_SIZEOF ;allocate str_SIZEOF bytes .Bd -literal -offset indent
DS str_SIZEOF ;allocate str_SIZEOF bytes
.Ed
.Pp .Pp
.Ss Including binary files .Ss Including binary files
You probably have some graphics you'd like to include. You probably have some graphics you'd like to include.
@@ -619,15 +680,19 @@ to include a raw binary file as it is.
If the file isn't found in the current directory, the include-path list passed If the file isn't found in the current directory, the include-path list passed
to the linker on the command line will be searched. to the linker on the command line will be searched.
.Pp .Pp
.Dl INCBIN \[dq]titlepic.bin\[dq] .Bd -literal -offset indent
.Dl INCBIN \[dq]sprites/hero.bin\[dq]\ ; UNIX INCBIN \[dq]titlepic.bin\[dq]
.Dl INCBIN \[dq]sprites\[rs]\[rs]hero.bin\[dq]\ ; Windows INCBIN \[dq]sprites/hero.bin\[dq]\ ; UNIX
INCBIN \[dq]sprites\[rs]\[rs]hero.bin\[dq]\ ; Windows
.Ed
.Pp .Pp
You can also include only part of a file with You can also include only part of a file with
.Ic INCBIN . .Ic INCBIN .
The example below includes 256 bytes from data.bin starting from byte 78. The example below includes 256 bytes from data.bin starting from byte 78.
.Pp .Pp
.Dl INCBIN \[dq]data.bin\[dq],78,256 .Bd -literal -offset indent
INCBIN \[dq]data.bin\[dq],78,256
.Ed
.Ss Unions .Ss Unions
Unions allow multiple memory allocations to share the same space in memory, Unions allow multiple memory allocations to share the same space in memory,
like unions in C. like unions in C.
@@ -755,7 +820,9 @@ You may nest
.Ic INCLUDE .Ic INCLUDE
calls infinitely (or until you run out of memory, whichever comes first). calls infinitely (or until you run out of memory, whichever comes first).
.Pp .Pp
.Dl INCLUDE \[dq]irq.inc\[dq] .Bd -literal -offset indent
INCLUDE \[dq]irq.inc\[dq]
.Ed
.Pp .Pp
.Ss Conditional assembling .Ss Conditional assembling
The four commands The four commands
@@ -831,7 +898,9 @@ The last one, Gameboy graphics, is quite interesting and useful.
The values are actually pixel values and it converts the The values are actually pixel values and it converts the
.Do chunky Dc data to Do planar Dc data as used in the Gameboy. .Do chunky Dc data to Do planar Dc data as used in the Gameboy.
.Pp .Pp
.Dl DW \`01012323 .Bd -literal -offset indent
DW \`01012323
.Ed
.Pp .Pp
Admittedly, an expression with just a single number is quite boring. Admittedly, an expression with just a single number is quite boring.
To spice things up a bit there are a few operators you can use to perform To spice things up a bit there are a few operators you can use to perform

View File

@@ -5,7 +5,7 @@
.\" .\"
.\" SPDX-License-Identifier: MIT .\" SPDX-License-Identifier: MIT
.\" .\"
.Dd January 26, 2018 .Dd February 23, 2018
.Dt GBZ80 7 .Dt GBZ80 7
.Os RGBDS Manual .Os RGBDS Manual
.Sh NAME .Sh NAME
@@ -24,8 +24,10 @@ as destination can omit the destination as it is assumed it's register
.Sy A . .Sy A .
The following two lines have the same effect: The following two lines have the same effect:
.Pp .Pp
.Dl OR A,B .Bd -literal -offset indent
.Dl OR B OR A,B
OR B
.Ed
.Pp .Pp
.Sh LEGEND .Sh LEGEND
List of abbreviations used in this document. List of abbreviations used in this document.