Category:68000 Assembly: Difference between revisions

Line 63:
<lang 68000devpac>MOVE.W (6,A0,D0),D0 ;The word at A0+D0+6 is read, then loaded into D0.</lang>
A very important note is that when using this method with words and longs, the resulting address <b><i>must be even!</i></b> Otherwise the CPU will crash. For <code>MOVE.B</code> it doesn't matter.
 
====Effective Address====
A calculated offset can be saved to an address register with the <code>LEA</code> command, which stands for "Load Effective Address." [[x86 Assembly]] also has this command, and it serves the same purpose. The syntax for it can be a bit misleading depending on your assembler.
 
<lang 68000devpac>LEA myData,A0 ;load the effective address of myData into A0
LEA (4,A0),A1 ;load into A1 the effective address A0+4. This looks like a dereference operation but it is not!
MOVE.W (A1),D1 ;dereference A1, loading the value it points to into D1.</lang>
 
This can get confusing, especially if you have tables of pointers. Just remember that <code>LEA</code> cannot dereference an address.
 
If you don't want to store the effective address in an address register, you can use <code>PEA</code> (push effective address) to put it onto the stack instead.
<lang 68000devpac>LEA myData,A0 ;load the effective address of myData into A0
PEA (4,A0) ;store the effective address of A0+4 onto the stack.</lang>
 
====The Stack====
The 68000's stack is commonly referred to as <code>SP</code> but it is also address register <code>A7</code>. This register is handled differently than the other address registers when pushing bytes onto the stack. A byte value pushed onto the stack will be padded to the <b>right</b> with zeroes. The stack needs to pad byte-length data so that it can stay word-aligned at all times. Otherwise the CPU would crash as soon as you tried to use the stack for anything other than a byte!
 
<lang 68000devpac>MOVE.B #$FF,-(SP) ;push #$FF then #$00 onto the stack, in that order.
MOVE.B (SP)+,D0 ;The values are popped in the order #$00 #$FF.</lang>
 
You can abuse this property of the stack to quickly swap bytes around. Suppose you had a number like <code>#$11223344</code> stored in <code>D0</code> and you wanted to change it to <code>#$11224433</code>:
 
<lang 68000devpac>
MOVE.W D0,-(SP) ;push #$3344 onto the stack
MOVE.B (SP)+,D0 ;pop them in the order #$44 #$33.</lang>
 
===Length===
1,489

edits