Jump to content

Include a file: Difference between revisions

m
Line 72:
There are two different directives for including files: <code>include</code> and <code>incbin</code>. <code>include</code> is for assembly code that will be converted to machine code that the computer can run.
<code>incbin</code> is for binary data such as graphics, which the assembler will convert as-is to binary and does not attempt to translate it into machine code.
 
Unless you're using a linker to arrange your various files into an executable, the location of your <code>include</code> and <code>incbin</code> directives ''imply the memory location of the code or data you're including.'' For example:
 
<lang 68000devpac>org $00000000
;512 bytes containing anything really
include "myFile.asm" ;assuming no org or align directives are in this file,
;its starting memory location is guaranteed to be $00000200 (512 in decimal)</lang>
 
This can become a problem if you have data that is not intended to be executed directly after a block of code, with nothing preventing "fallthrough." For example:
 
<lang 68000devpac>foo:
LEA myData,A0
MOVE.W (A0)+,D0
 
MyData:
include "dataTables.inc" ;a list of defined data constants that aren't supposed to be executed as instructions</lang>
 
Since there was no control flow instruction stopping the program counter from reaching the included file, ''the bytes stored in it will be executed as though they were genuine Motorola 68000 CPU instructions, even if they're not.'' The CPU cannot tell the difference between data and instructions, and thus the programmer must ensure that the program counter never reaches anything that wasn't meant to be executed. The easiest fix for this would be:
<lang 68000devpac>foo:
LEA myData,A0
MOVE.W (A0)+,D0
RTS ;return from subroutine, preventing the CPU from executing the data below.
 
MyData:
include "dataTables.inc" ;a list of defined data constants that aren't supposed to be executed as instructions</lang>
 
=={{header|AArch64 Assembly}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.