Quoting constructs: Difference between revisions

Content added Content deleted
m (→‎{{header|Lua}}: corrected output)
Line 16: Line 16:


GraphicsData: incbin "C:\game\gfx\tilemap.chr" ;a file containing the game's graphics</lang>
GraphicsData: incbin "C:\game\gfx\tilemap.chr" ;a file containing the game's graphics</lang>

Most 6502 assemblers use Motorola syntax, which uses the following conventions:
* A value with no prefix is interpreted as a base 10 (decimal) number. $ represents hexadecimal and % represents binary. Single or double quotes represent ASCII.
* Unlike immediate values in instructions, quoted data does NOT begin with a #. For example, <code>dw $1234</code> represents the literal value 0x1234, not "the value stored at memory address 0x1234."
* Multiple values can be put on the same line, separated by commas. <code>DB</code> only needs to be before the first data value on that line. Or, you can put each value on its own line. Both are valid and have the same end result when the code is assembled.

6502 Assembly uses <code>db</code> or <code>byte</code> for 8-bit data and <code>dw</code> or <code>word</code> for 16-bit data. 16-bit values are written by the programmer in big-endian, but stored little-endian. For example, the following two data blocks are equivalent. You can write it either way, but the end result is the same.
<lang 6502asm>dw $ABCD
db $CD,$AB</lang>

Most assemblers support "C-like" operators, and there are a few additional ones:
* < or LOW() means "The low byte of." For example, <code><$3456</code> evaluates to 56.
* > or HIGH() means "The high byte of." For example, <code><$78AB</code> evaluates to 78.
These two operators are most frequently used with labeled memory addresses, like so:

<lang 6502asm>lookup_table_lo:
byte <Table00,<Table01,<Table02
lookup_table_hi:
byte >Table00,>Table01,>Table02</lang>


=={{header|68000 Assembly}}==
Formatting is largely dependent on the assembler and the syntax. Generally speaking, assemblers that use Motorola syntax follow these conventions:
* A value with no prefix is interpreted as a base 10 (decimal) number. $ represents hexadecimal and % represents binary. Single or double quotes represent ASCII.
* Unlike immediate values in instructions, quoted data does NOT begin with a #. For example, <code>DC.L $12345678</code> represents the literal value 0x12345678, not "the value stored at memory address 0x12345678."
* The length of the data must be specified, and if the value given is smaller than that size, it will get padded to the left with zeroes. If the value is too big to fit in the specified size, you'll get a compiler error and the assembly is cancelled.
* Multiple values can be put on the same line, separated by commas. <code>DC._</code> only needs to be before the first data value on that line. Or, you can put each value on its own line. Both are valid and have the same end result when the code is assembled. You should always follow byte data with EVEN which will add an extra byte of padding if the total number of bytes before it was odd. This is necessary for your code to comply with the CPU's alignment rules.

<lang 68000devpac>ByteData:
DC.B $01,$02,$03,$04,$05
even
WordData:
DC.W $01,$02
DC.W $03,$04
;the above was the same as DC.W $0001,$0002,$0003,$0004
LongData:
DC.L $00000001,$00000002,$00000004,$00000008

MyString:
DC.B "Hello World!",0 ;a null terminator will not be automatically placed.
even</lang>

<code>DS._</code> represents a sequence of space. The number after it specifies how many bytes/words/longs' worth of zeroes to place. Some assemblers support values besides zero, others do not.
<lang 68000devpac>DS.B 8 ;8 bytes, each equals 0
DS.W 16 ;16 words, each equals zero
DS.L 20 ;20 longs, each equals zero</lang>

In addition to constants, a label can also be specified. If a label is defined with an <code>EQU</code> statement, the label will be replaced with the assigned value during the assembly process.

<lang 68000devpac>ScreenSize equ $1200

MOVE.W (MyData),D0

MyData
DC.W ScreenSize</lang>

Code labels, on the other hand, get replaced with the memory address they point to. This can be used to make a lookup table of various data areas, functions, etc. Since there is no "24-bit" data constant directive, you'll have to use <code>DC.L</code> for code labels. The top byte will always be zero in this case.

<lang 68000devpac>Printstring:
;insert your code here

FunctionTable:
DC.L PrintString ;represents the address of the function "PrintString"</lang>

Most constants can be derived from compile-time expressions, which are useful for explaining what the data actually means. The expressions are evaluated during the assembly process, and the resulting object code will have these calculations already completed, so your program doesn't have to waste time doing them. Most "C-like" operators are supported, but as always the exact syntax depends on your assembler. Parentheses will aid the assembler in getting these correct, but sometimes it still doesn't do what you expect.

<lang 68000devpac>DC.B $0200>>3 ;evaluates to $0040. As long as the final result fits within the designated storage size, you're good.
DC.W 4+5 ;evaluates to $0009
DC.W (40*30)-1 ;evaluates to $1199
DC.L MyFunction+4 ;evaluates to the address of MyFunction, plus 4.</lang>

We can use this technique to get the length of a region of data, which the assembler can calculate for us.
<lang 68000devpac>TilemapCollision:
DC.B $11,$11,$11,$11,$11,$11,$11,$11,$11,$11
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $11,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $11,$11,$11,$11,$11,$11,$11,$11,$11,$11
TilemapCollisionEnd:

MOVE.W #(TilemapCollisionEnd-TilemapCollision)-1,D0
;gets the length of this region of memory, minus 1, into D0.
; Again, even though the "operands" of this expression are longs,
; their difference fits in 16 bits and that's all that matters.</lang>


For quoting binary data in another file, you can use the <code>incbin</code> directive to embed it directly in your source code. This is handy for graphics data and music.


=={{header|BQN}}==
=={{header|BQN}}==