This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Accessing stack pointer SP from C/C++ code

Hello All,

Is there a way of accessing contents of SP in my C/C++ code? I want to know the address stored in SP for some reason. I know it is possible using assembly. However my observation is that SP is not easily available in C/C++ code (or at least i dont know how to access it and documentation doesnt talk about it, documentation says that it is available in assembler as control register).

I would like to see working something like this,

WORD myStackPointer = SP; or WORD myStackPointer = *SP;

I dont know how to declare it as extern?

Please guide.

Thanks in advance.

  • Hello Dinesh,

    the stack pointer is typically held in register B15. However, the compiler is free to change the register assignments within a function for the benefit of optimization. So, you can probably not rely on B15 being the stack pointer at an arbitrary place within a more complex function.

    To my knowledge, there is no way to access individual registers from C or C++ because the compiler manages the allocation of registers and assumes it can use any. So, the cleanest way to retrieve the stack pointer would be by way of a (C-callable) assembly function, which returns this value. However, here is a trick that might work to retrieve the B15 value from C:

    - Implement a short sub function, and prevent it from being inlined.
    - Exploit the following C calling convention: return value of a function is passed in register A4.
    - Use inline assembly to make register accesses and retrieve B15. For a short function, it is assumed that B15 indeed contains the stack pointer.

    unsigned int *returnSP( int dummy ) // register A4 is used for the first parameter passed to a C function, and also to pass the function's return value
    { // we can assume register A4 is available, i.e. not otherwise used by the compiler for this function implementation
       asm(" MV B15,A4"); // insert an assembly call - note: instruction starts with whitespace (otherwise it would be interpreted as label); the compiler will write out this assembly instruction, and not care about its meaning
       return dummy; // return value passed in register A4
    }

    In calling function:
    myStackPointer = returnSP(0);

    Thanks and best regards,
    Andre.

    P.S.: The above is true for the C6000 DSP family. I'm not sure whether that's the CPU that your question was about?

  • Dinesh, Andre,

    On the 28x the stack pointer is a dedicated register called SP.  I looked through the compiler intrinsics and don't see one to fetch the value in SP.  To do this a c-callable assembly function that returns in the SP is the way to go.

    -Lori 

  • Thanks Andre and Lori.

    I agree with Lori. I still have one question, doesnt have SP any memory address? I couldnt find it in any document.

    Dinesh

  • Dinesh Phatak said:
    doesnt have SP any memory address

    Dinesh,

    No, the C28x core registers, including SP, are not memory mapped.

    -Lori

  • Lori,

    That's all I had needed.

    Thanks for your prompt response.

    Dinesh