This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler internal variables

Hello all,

In my application I need to access to the value that Stack is initialized, in general C51 does it in the startup code:

MOV SP,#?STACK-1

What I need is to be able to use later on in my code the value "?STACK-1" and copy it to other variable.

Thanks for your help in advance!
Alfredo M.

  • SP is a register you can access by name.

    #include "reg52.h"
    
    unsigned char sp;
    
    void main(void)
    {
        sp = SP;
        for (;;);
    }

  • Hi, thanks for your reply.

    Sorry, maybe I did not explain well my question...
    As you know the stack is changed after initialization from 0x07 to something else depending of the number of variables that we have in our project, I need to know this first value given for the stack and be able to used it later on in my project.
    For example is sp is initialized to 0x42 (in startup routine) after power up, I need to put this value (0x42) in some variables later on in my proyect. Do you know the way to do so?

    Thanks,
    Alfredo.

  • "the stack is changed after initialization ... to something else depending of the number of variables that we have in our project"

    Err, no!

    The stack has nothing to do with the number of variables in your project!

    If you don't understand this, you are probably far better off leaving the Stack Pointer alone!

    If you mess with the SP without a thorough understanding of precisely what it's doing, you are almost certain to really mess-up your application Big Time!!

    What exactly is it that you are trying to achieve here?

  • "For example is sp is initialized to 0x42 (in startup routine) after power up, I need to put this value (0x42) in some variables later on in my proyect."

    Yep, I'm with you so far (although I have no idea why one would want to do this). What I told you before still stands. main() is LJMP'ed to by my runtime startup (maybe yours doesnt'), so this works:

    sp = SP;
    If you think your runtime startup LCALL's main() then:

    sp = SP - 2;

  • hi,

    What I need is to be able to use later on in my code the value "?STACK-1" and copy it to other variable.

    Then modify startup.a51 slightly. Just edit this part of its code:

    ?STACK          SEGMENT   IDATA
    
                    RSEG    ?STACK
                    DS      1
    
    upto:
    ?STACK          SEGMENT   IDATA
    
                    RSEG    ?STACK
    STACK:          DS      1
    PUBLIC STACK
    

    Then in your c:
    extern idata STACK;
    
    void main(void)
    {
       void *sp;
    
       sp = &STACK;
    
       while(1);
    }
    

    Be careful: register SP does not point to stack space. For example, if SP contains 0x07 then stack is placed from 0x08 and above.
    As well: think twice before hardcoding over stack and registers!

    Regards,
    Oleg

  • of course, you may replace void reference to needed data type, like this:

    void main(void)
    {
       char idata *sp;
    
      sp = (char *)&STACK;
    //...
    }
    
    This saves some space to keep pointer variable.

    Regadrs,
    Oleg

  • Hello,
    First of all thanks to everybody for your help, I think this deserves a deeper explanation of what I am trying to do, which is not easy but so far goes fine, I have a bootloader receiving data (application program) by serial port using INTERRUPTION, that's my bootloader, an int routine (there is more but no worth to comment). This int routine is performing different funtions as soon as datas are received:

    - Charging application program and jumping to it to execute.
    - Making software reset.
    - Charging a new received program while other application one is running and replace it.
    - Charging program from serial eeprom.
    …. Many other things.

    Therefore my int routine has to be executable at anytime.That is the reason whenever I jump to application I can not leave application to initialize the pointer in the way that overwrite my variables used in the serial isr, (an obj file of the bootloader is included in the application adding only the global definitions) but there is not startup routine in the application cause the definition is already in the obj file of the bootloader and because I can not change the variable used in the serial isr, since I have not startup in the application I have to initialize the stack pointer by myself and leave it at the same point were the bootloader initialized it !! (here is the point ! ). Do you still follow me? Now you know why I did not explain it from the beggining.

    Just an answer to Neil, as far I understood, the stack pointer grows up to higher address, therefore it has to be initialized following the variables in our program therefore as more you have higher will be the value where the pointer is initialized.

    About the answer of Oleg I will try as soon as possible.

    Regards,
    Alfredo.

  • "Do you still follow me? Now you know why I did not explain it from the beggining."

    It didn't need all that explanation. You asked how to get the value of the stack pointer. At entry to main(), SP contains the result of the MOV SP,#?STACK-1. You can read and write SP. Simple. You don't even need access to the stack area's name. But do it however you want.

  • Hi,
    The things is that you missundertood me, what I need is NOT the value of SP, but the value to which it is initiated at the very begging in the startup code, which is not a variable but a constant value called "#?STACK-1" in the startup file.

    Thanks for you effort,
    Alfredo.

  • Ok,

    Finaly I got it!! thanks a lot everybody for your help especially to Oleg, it worked in the way he said.

    Alfredo M.

  • "The things is that you missundertood me..."

    No, the thing is that you are not able to understand such a simple thing. OK, I'll be the first to say "you don't get it" and "I don't particularly care anymore" -- and neither do you since you are on your way now!

  • You dont have to be so upset,

    You mean to keep the value in a varible an use it later, isnt it? but it should be possible to do it without memory usage since it is only a constant definition.

    I am sorry if I am so stupid and we dont understand, lets stop here since the forum is not a e-mail account for msgs.

    Anyway thanks for your help one more time.