EASy68K  
It is currently Fri Oct 18, 2019 12:08 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Stack issue
PostPosted: Thu Feb 23, 2012 12:55 am 
Offline

Joined: Wed Feb 22, 2012 2:17 am
Posts: 8
Location: toronto
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      


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2012 1:45 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1102
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.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2012 3:56 pm 
Offline

Joined: Wed Feb 22, 2012 2:17 am
Posts: 8
Location: toronto
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2012 4:25 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2012 3:14 am 
Offline

Joined: Wed Feb 22, 2012 2:17 am
Posts: 8
Location: toronto
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...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2012 1:52 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1102
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

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 29, 2012 12:58 am 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group