Category:68000 Assembly: Difference between revisions

Content deleted Content added
Puppydrum64 (talk | contribs)
Puppydrum64 (talk | contribs)
m →‎The Stack: removed incorrect information
Line 112: Line 112:


====The Stack====
====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! If a byte is popped, the zeroes are placed on the left side of the lower word of the data register, and the actual byte goes in the right side.
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>:
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
<lang 68000devpac>MOVE.W D0,-(SP) ;push #$3344 onto the stack
ROL.W #8,D0
MOVE.B (SP)+,D0 ;pop them in the order #$44 #$33.</lang>
MOVE.B (SP)+,D0 ;pop them in the order #$44 #$33.</lang>