EASy68K
http://www.easy68k.com/EASy68Kforum/

Stack issue
http://www.easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=1078
Page 1 of 1

Author:  etw3 [ Thu Feb 23, 2012 12:55 am ]
Post subject:  Stack issue

For this code if run as is i get
d2=1028
d3=f0f
d4=eee

and if you take out the comments i get
d2=f0f
d3=eee
d4=aaa


Now, if I Change the line in the procedure to movem.l (sp)+,d2-d5
I get
d2=1028
d3=f0f
d4=eee
d5=aaa

why the difference?

Code:
   ORG   $1000
START:   
   clr.l   d2
   clr.l   d3
   clr.l   d4
   
   move.l   #$f0f,d2
   move.l   #$eee,d3
   move.l   #$aaa,d4
   movem.l   d2-d4,-(sp)
   clr.l   d2
   clr.l   d3
   clr.l   d4
   
   ;movem.l (sp)+,d2-d4
   ;jmp   endprog

   jsr    QUAD   
endprog:
   MOVE.B   #9,D0
   TRAP   #15      
**********************************   
QUAD:
   movem.l   (sp)+,d2-d4
   rts
**********************************   
   END   START      

Author:  profkelly [ Thu Feb 23, 2012 1:45 pm ]
Post subject: 

The code as is has an error.

d2-d4 registers are pushed onto the stack.
jsr QUAD pushes the return address onto the stack.
The QUAD subroutine tries to load d2-d4 from the stack which results in the return address being loaded in D2, $F0F into D3 and $EEE into D4. The rts instruction pops $AAA from the stack and sets the PC to $AAA.

Author:  etw3 [ Thu Feb 23, 2012 3:56 pm ]
Post subject: 

profkelly wrote:
The code as is has an error.

d2-d4 registers are pushed onto the stack.
jsr QUAD pushes the return address onto the stack.
The QUAD subroutine tries to load d2-d4 from the stack which results in the return address being loaded in D2, $F0F into D3 and $EEE into D4. The rts instruction pops $AAA from the stack and sets the PC to $AAA.


so the first line in the subroutine should always advance the stack pointer...
add.l #4,sp

Author:  clive [ Thu Feb 23, 2012 4:25 pm ]
Post subject: 

etw3 wrote:
so the first line in the subroutine should always advance the stack pointer...
add.l #4,sp


Well not really, you'll bust the stack.

If you have to do it this way, you'd want to pop the return address off the stack first, then pull your registers, then push back the return address. Otherwise the RTS will break.

This however is a bizarre calling convention, what is it that you really want to achieve?

Normally you'd push parameters onto the stack, and then access them via A6, when you use LINK/UNLK to create a local stack frame, or directly relative to A7/SP.

The other method is to pass parameters in registers. The subroutine could push/pop the registers it trashes, or the caller could save the registers it wants to preserve.

Author:  etw3 [ Tue Feb 28, 2012 3:14 am ]
Post subject: 

clive wrote:
etw3 wrote:
so the first line in the subroutine should always advance the stack pointer...
add.l #4,sp


If you have to do it this way, you'd want to pop the return address off the stack first, then pull your registers, then push back the return address. Otherwise the RTS will break.


So although I did pop off/on the pc as you suggested (and it makes sense).
Not doing so did not break the rts...

Author:  profkelly [ Tue Feb 28, 2012 1:52 pm ]
Post subject: 

The contents of the stack may be viewed by selecting "view/stack" from the simulator menu. Here is an example from the above code after the subroutine has been called. The aqua highlight indicates the current location of the stack pointer.

Image

Author:  clive [ Wed Feb 29, 2012 12:58 am ]
Post subject: 

etw3 wrote:
Not doing so did not break the rts...

Try stepping through the code, the RTS will take you off to the wrong/unexpected address.

The RTS is equivalent to MOVE.L (SP)+,PC so if you trash the stack it'll start executing some random code, or fault because the address is odd.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/