We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am attempting to do something that seemed simple, but it is turning into a mess. I want to write a stack underflow monitor in main that both checks the stack pointer to insure it is at the bottom, and checks a byte under the stack bottom for a pattern (to better insure that the stack never went under when it was outside of main).
I've come to find that you cannot compile the following statement:
unsigned char * data StackPtr _at_ 0x81; (because it is an sfr address)
It seems like this should be the same thing as: sfr SP = 0x81; ..but with the added benefit of being able to dereference StackPtr to get at what it is pointing at.
The plan was to enter main at power-up, get the address SP is pointing at, stuff a pattern in there like 0xAA, then increment the SP. Later, when it returns to main, the test verfies the SP is still pointing at the SP++ address, and the original SP pattern is still 0xAA.
Assembler seems my only way out.
if((SP != (uint8_t)&stackbot[0]) || (*(uint8_t data *)(SP-1) != 0xAAu))
That fixed it! Thanks for the help.
I screwed this up badly. I should have incremented SP, then put in the 0xAA pattern. Then the SP-1 down below should have simply been 'SP'.
The stack pointer always increments before something gets pushed onto the stack.
(I inadvertently stuffed 0xAA into a variable, instead of onto the actual stack bottom) Someday someone will also want to do this, and it would be easier if it were posted correctly.