Category:68000 Assembly: Difference between revisions

no edit summary
No edit summary
Line 122:
 
Depending on the hardware, certain traps are built-in to perform certain tasks, such as reading a keyboard or mouse, or are user-defined. To create your own trap routine, you'll need to first write the routine, then store its address in the corresponding trap number. Trap 0 is stored at $000080, Trap 1 at $000084, and so on. The overflow trap is stored at address $00001C.
 
==Alignment==
The 68000 can only read or write words and longs at even addresses. Doing so at an odd address will result in the CPU crashing. (Note that reading byte data will not cause a crash regardless of whether it's located at an odd or even address.) This isn't usually a problem, but it can be if the programmer is not careful with the way their data is organized. Consider the following example:
<lang 68000devpac>TestData:
DC.B $02
DC.W $0345
 
LEA TestData,A0 ;load effective address of TestData into A0.
MOVE.B (A0)+,D0 ;load $02 into D0, increment A0 by 1
MOVE.W (A0)+,D1 ;this crashes the CPU since A0 is now odd</lang>
 
How was it known that the address was odd at the second instruction? Simple. All instructions take an even number of bytes to encode. So there are only a few ways improper alignment can occur:
* An odd value is loaded into an address register.
* An addition or subtraction done to the value in an address register resulted in an odd memory address.
* An indirect read at byte length was performed using pre-decrement or post-increment, at an even address. (e.g. <code>MOVE.B (A0)+,D0</code> or <code>MOVE.B -(A0),D0</code> where A0 contained an even memory address.)
 
If the programmer is smart with the way they encode byte-length data they can avoid this problem entirely with little effort.
 
One way is to separate byte-length data into its own table.
<lang 68000devpac>ByteData:
DC.B $20,$40,$60,$80
 
WordData:
DC.W $1000,$2000,$3000,$4000</lang>
 
Another way is to pad the data with an extra byte, so that there is an even number of entries in the table. This allows the programmer to do a "dummy read" (i.e. reading with post-increment with the sole purpose of incrementing the pointer, with the value read being of zero interest.) This becomes impractical with large data tables, so the <code>EVEN</code> directive can be placed after a series of bytes. If the byte count is odd, <code>EVEN</code> will pad the data with an extra byte. If it's already even, the <code>EVEN</code> command is ignored. This saves you the trouble of having to count a long series of bytes without worrying about wasting space.
 
<lang 68000devpac>MyString: "HELLO WORLD 12345678900000",0
EVEN ;some assemblers require this to be on its own line</lang>
 
 
 
==Citations==
1,489

edits