Writing a Stack Underflow with the C51 compiler

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.

Parents
  • Just use SP by name. I'm not at a system with C51 installed at the moment, but something like:

    unsigned char start_SP = SP;
    
    *(unsigned char*)SP = 0xAA;
    ++SP;
    
    /* Do stuff */
    
    if ((SP - 1) != start_SP) {
        /* SP isn't pointing where it should be. */
    } else if (*(unsigned char*)(SP - 1) != 0xAA) {
        /* SP underflowed or something else happened. */
    }
    

Reply
  • Just use SP by name. I'm not at a system with C51 installed at the moment, but something like:

    unsigned char start_SP = SP;
    
    *(unsigned char*)SP = 0xAA;
    ++SP;
    
    /* Do stuff */
    
    if ((SP - 1) != start_SP) {
        /* SP isn't pointing where it should be. */
    } else if (*(unsigned char*)(SP - 1) != 0xAA) {
        /* SP underflowed or something else happened. */
    }
    

Children
More questions in this forum