Welcome, Guest. Please login or register.

Author Topic: Stack related program crash  (Read 966 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline peroxidechickenTopic starter

  • Full Member
  • ***
  • Join Date: Apr 2002
  • Posts: 170
    • Show only replies by peroxidechicken
Stack related program crash
« on: June 18, 2004, 09:21:00 PM »
I'm writing a program that stores all of it's variables and some other data starting at the low end (closer to address 0) of stack space and continuing for what I believe to be less than 1000 bytes.  First, the initialization code checks the task structure to make sure the normal 4KB or more is there, then clears the desired space before using it.  This was fine until I added a few more variables recently and started to experience crashes after the terminating rts instruction.  I remedied this by tidying up/getting rid of the few variables the program no longer uses.  But this program is far from done and soon enough, I'm going to need extra variables again.  

I took this approach to variable storage thinking that all the stack space lower than the current stack pointer is up for grabs - can anyone set me straight?  
H2O2
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Stack related program crash
« Reply #1 on: June 18, 2004, 11:42:53 PM »
It is available, but only after you write the new stack poiner value to sp.

example:
Code: [Select]

 ; allocate 256 bytes from stack
 lea (-256,sp),a3

 ; can't poke the memory at a3 yet, since any task switch
 ; would write registers & system context to stack overwriting
 ; our data!

 move.l a3,sp
 ; now the memory at a3 ... a3+256 can be used freely. Any
 ; context switch will use memory before our data, and thus
 ; it won't be erased.

 ;....

 ; pop our temp storage off stack
 lea (256,sp),sp
 rts


If you're running low on standard 4k stack, just AllocMem/AllocVec some storage for the variables.
 

Offline peroxidechickenTopic starter

  • Full Member
  • ***
  • Join Date: Apr 2002
  • Posts: 170
    • Show only replies by peroxidechicken
Re: Stack related program crash
« Reply #2 on: June 20, 2004, 01:56:28 AM »
Thanks Piru - I can see now that I wasn't being system friendly in the way I stored my variables.  

But I'm sure I didn't exceed the low end of stack space and I also didn't overwrite the stack pointer.  I understand how my variables might become corrupted, what I don't understand is how the operating system lost any data it needed to terminate my task.  
H2O2
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show only replies by PiR
Re: Stack related program crash
« Reply #3 on: June 21, 2004, 08:01:30 PM »
Hi

Had a thought of observing the contents of the stack under the debugger, especially the longword used as address by the RTS instruction?

This might help. ;-)
Good luck
 

Offline peroxidechickenTopic starter

  • Full Member
  • ***
  • Join Date: Apr 2002
  • Posts: 170
    • Show only replies by peroxidechicken
Re: Stack related program crash
« Reply #4 on: June 22, 2004, 01:36:20 AM »
Hi there PiR.  I tried that and that address wasn't being changed/overwritten.  I didn't bother to see if the memory area pointed to by that address was being changed - I know this sounds like famous last words, but there's no way my program would be modifying memory like that.  

Anyway, I went and did what Piru suggested - not only does it work but it made my variable initialization code smaller too!  
H2O2