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

RE: PUSH and POP in Keil C

First and foremost, understand you make no contribution to this forum by replying to every thread with 'read the manual'. If you do not wish to answer the question, simply dont answer it.


To answer the question, KEIL C has the commands (probably for good reason) :
_push_
_pop_

defined in <intrins.h>

Remember push/pop can SAVE or RESTORE any special function register.

Parents Reply Children
  • "Remember push/pop can SAVE or RESTORE any special function register."

    Also remember that you have absolutely no control whatsoever of what else the compiler may be PUSHing and POPping, nor when it might be doing it!

    Therefore, to insert abitrary PUSHes and POPs into 'C' source code must be regarded as dangerous in the extreme!! :-0

    See http://www.keil.com/forum/docs/thread7162.asp#msg33283

  • A local variable often makes a good alternative to push'ing and pop'ing from C. For example:

    void MyFunc (void)
        { // accesses slow devices
        U8 oldCkcon = CKCON;
        CKCON = SlowCkcon;
    
        // slow accesses here
    
        CKCON = oldCkcon;  // normal speed
        }
    

    When using this style, it helps to remember the somewhat obscure feature of C that you can declare unnamed local blocks of code:

    void MyFunc (void)
        {
        // some variables
    
        // some code that runs fast
    
        { // slow access region
        U8 oldCkcon = CKCON;
        CKCON = SlowCkcon;
    
        // slow accesses here
    
        CKCON = oldCkcon;
        } // end of slow access
    
        // more code here that runs fast
        }
    

    This makes a nice visual marker for the critical region, as well as limiting the scope of the variable only to where it's needed.

    Unfortunately, the C51 overlay analysis does not optimize handling of such local blocks within a function. All the variables declaring within such are apparently promoted outside to the "real" function. So, if I wrote three blocks like this in a row within one function, I'd wind up with 3 U8s reserved by that function, even though only one of them is used at a time, and the storage could be safely reused. On the bright side, these critical regions map 1:1 directly to functions more often that not.