0

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?

TTEd
  • 309
  • 1
  • 10
  • 1
    Not sure of the syntax but it's something like `MOVE.B 3(SP),D0`, – user207421 Dec 01 '15 at 01:00
  • 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 Dec 01 '15 at 01:03
  • 1
    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 Dec 01 '15 at 03:52

2 Answers2

4

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.

Durandal
  • 19,919
  • 4
  • 36
  • 70
2

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

Graeme
  • 1,643
  • 15
  • 27
  • 1
    The usual phrase is "stacks grow downward". Saying "backward" makes it sound harder to deal with than it is :P. – Peter Cordes Dec 01 '15 at 13:57
  • That's just the way I have always heard it :) either phrase will probably work differently for different people. For me downward would imply a positive address increment. whereas forward and backwards translate simply to positive and negative increments (for me at least). – Graeme Dec 01 '15 at 14:10
  • Hmm if an offset of 2 yields aa(bottom of the stack), would an offset of 1 yield bb? – TTEd Dec 01 '15 at 14:37
  • Yes. 0(sp) is the same as (sp), 1(sp) is like saying sp + 1, and so on. Be careful though that you don't try to read a word or longword from an odd address however :) – Graeme Dec 01 '15 at 14:42
  • @Graeme: It's very common to show memory layout diagrams with high addresses at the top, low addresses at the bottom. e.g. http://nickdesaulniers.github.io/images/stack.png. (from http://nickdesaulniers.github.io/blog/2014/04/18/lets-write-some-x86-64/, found with a google image search for "stack grows downward"). "Backward" sounds to me like you're talking about "compared to normal arrays / buffers", so you get to the stack growing downward by remembering that it's backwards from normal, so it takes an extra step to think about. No clue how you're getting down = positive increment. – Peter Cordes Dec 01 '15 at 15:11
  • I am not disputing that is is a common term, just a different one. Similarly if you do the same Google search for "Stack grows backwards" you will also find a lot of results. – Graeme Dec 01 '15 at 15:14
  • I guess you're used to diagrams with low addresses at the left, and "backward" means right-to-left? My brain hurts from trying to think that way for 2 seconds. Heh, fair enough, I'll check it out next time I want to hurt my brain. :P Anyway, yeah, wasn't trying to say you were wrong, just that it seems really weird to think about that way. I didn't realize it was at all common. – Peter Cordes Dec 01 '15 at 15:16
  • 1
    I am used to looking at memory in disassemblers or dumped output, in those cases the memory address $00 would be at the top and $ff at the bottom. Also when you write code the first line of code assembles at (for argument :) ) $00, the next would assemble at $02 and so on. I guess it stems from reading left to right, top to bottom. For me there is no extra step in thought for backwards, but there is for downwards. As I said earlier this is probably down to the individual and/or how they were taught/learnt. – Graeme Dec 01 '15 at 15:20
  • @Graeme: I see. That does make sense. I agree with your conclusion that the other way sounds weird, whichever one you're used to. :P In a dump of stack memory, I'd just automatically start reading at the last line, same as you, since I know it's a stack. So it works the same in practice. – Peter Cordes Dec 01 '15 at 15:47