redhotsonic, on 22 February 2012 - 12:49 PM, said:
MarkeyJester, on 21 February 2012 - 07:23 PM, said:
The use of "even" isn't really for the actual file itself, it's more for the data after it
Chilly Willy, on 22 February 2012 - 02:50 AM, said:
Putting an even right before code starts, or before dc.w/dc.l/ds.w/ds.l variables assures that they will meet that alignment.
So why put an even
after it if it only evens data after the word? For example, how do you know that an even doesn't need to go before "File1Location:"? I've gone through my whole ASM putting evens after the BINCLUDES and after the dc.b's and etc.
If you see code or dc.w/dc.l/ds.w/ds.l, those MUST be on an even address. How do you know they are on an even address? Some cases are simple:
rts
some_var:
dc.b 5
another_function:
movem.l d0-d1,-(sp)
See how you have a single byte stuck between pieces of code? Unless that second bit of code at another_function is aligned, calling it will cause an address errror.
rts
some_var:
dc.b 5
even
another_function:
movem.l d0-d1,-(sp)
That now works. However, some cases are harder...
rts
some_var:
incbin "some_data_file.bin"
another_function:
movem.l d0-d1,-(sp)
Is another_function aligned or not? It depends on how long some_data_file.bin is. If it's an odd length, the code is unaligned again and will fail. But if the block is an even length, you don't need to worry, right? Maybe, but maybe not. Will the block ever change length? Will you remember that this block affects code later on when you change the contents of the block? Will you do something like this?
rts
another_var:
dc.b 11
some_var:
incbin "some_data_file.bin"
another_function:
movem.l d0-d1,-(sp)
Even if the file is an even length, you've changed where it starts to an odd address by adding the byte variable before it. That means since it's an even length, it will end on an odd address and the code is again unaligned. In the case of variables stuck between code segments, use alignment directives no matter what to guarantee the following code segment is aligned. Like this
rts
another_var:
dc.b 11
some_var:
incbin "some_data_file.bin"
even
another_function:
movem.l d0-d1,-(sp)
Another case people forget occurs when code is split over several files. Say file 3 ends like this
dc.b 0,0,0
and say file 4 starts like this
enemy_code:
movem.l d0-d7/a0-a6,-(sp)
You've created an odd alignment without even realizing it. You need to keep in mind how the overall code and data sections are being populated when assembling/compiling to avoid this. For code that starts a file, ALWAYS align it no matter what.
even
enemy_code:
movem.l d0-d7/a0-a6,-(sp)
Now it doesn't matter how file 3 ends, file 4 will start with aligned code. This can also get you when you switch back and forth among sections...
rts
local_var_used_by_last_routine_above:
dc.b 1
.data
level_data:
incbin "level1.bin"
.text
another_function:
tst.l d0
...
rts
.data
global_var:
dc.l 53
I've created TWO potential address errors in the above code! First the code section ends with a byte variable, but when the section resumes later, starts straight in with code. Second, I have an unknown length block of data in the data section that when the data section resumes has a long variable. Both may cause address errors. When you resume a section, you cannot really be certain how that section ended... so align it!
rts
local_var_used_by_last_routine_above:
dc.b 1
.data
level_data:
incbin "level1.bin"
.text
even
another_function:
tst.l d0
...
rts
.data
even
global_var:
dc.l 53