0
votes

I'm asked the following question:

Three  bytes  are  pushed  onto  the  runtime  stack.  Copy  the  third  
byte  from  the  runtime  stack  to D0 without changing the stack pointer

So I have a stack that looks like this:

|   |
|   |
|cc | <-- SP points to cc
|bb |
|aa |

I'm not sure how I would copy the value of cc, into register D0. I know I can pop it off the stack like this ... MOVE.B (SP)+,D0, but this would change the stack pointer to point to bb

Also what's the difference between a user stack and a run time stack? For instance if I'm asked to pop a byte from a user stack(A6) but then push it into the run time stack, how would I do that? Any ideas?

2
Not sure of the syntax but it's something like MOVE.B 3(SP),D0, - user207421
The 3 in front is a pre-increment of 3, but the stack pointer already points to what I want to copy?So isn't that copying the value 3 spaces above cc, which is nothing?Your syntax is fine, but I'm not sure about the value '3' - TTEd
Then it's just (SP), isn't it? NB `3(SP)' isn't a 'pre-increment of 3', it is an indexed indirect access, 3 plus the value of SP. - user207421

2 Answers

4
votes

This is a trick question, and not answerable without knowing by what means the bytes got onto the stack, specifically because there is an oddity when using the address register with predecrement -(An) mode when An=A7=SP. If for example the bytes were pushed like that:

move.b #$aa,-(sp)
move.b #$bb,-(sp)
move.b #$cc,-(sp)

|$??| +5
|$aa| +4
|$??| +3
|$bb| +2
|$??| +1
|$cc| <--- SP points here

The stack pointer decreases by two for each move. The 68k treats this differently from other address registers because an odd stack pointer address would trigger an address exception (for the MC68000 at least) on the next attempt to push a word or long word (either explicitly or implicitly by a subroutine call) due to a word/long access on an odd address. This is documented in the family reference manual, page 2-7: http://www.freescale.com/files/archives/doc/ref_manual/M68000PRM.pdf

If the 3 bytes however were part of a larger entity, e.g. a longword they would be placed on consecutive addresses:

move.l #$ccbbaa??,-(sp)

|$??| +3
|$aa| +2
|$bb| +1
|$cc| <--- SP points here

So the answer is something like "move.b 4(sp),d0" if they were pushed as bytes, but "move.b 2(sp),d0" if they were part of a larger entity.

2
votes

You need to use an offset on the stack pointer. Also remember stacks go backwards (typically :) ) so

move.b (sp), d0

would yield $cc in your example

move.b 2(sp), d0

will get you $aa into d0

Hope that helps