Thread: saving an address in two bytes

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by afflictedd2 View Post
    stackPointer is:
    unsigned char* stackPointer;

    Yep, I'm dropping at the assembly level.
    I need to load the stack once I return from interrupt, and that's why I need to put the address of the function in the first two bytes, in the program counter of the stack.
    Here's a picture of the stack:
    http://www.owlnet.rice.edu/~elec201/...ges/img190.gif

    Ted.
    Gotcha! and here's howto store the address of the function byte-wise
    Code:
    #include <stdio.h>
    
    void f(void) {}
    
    int main(int argc, char *argv[])
    {
        void (*pf)(void);
        unsigned char *sp, t;
    
        pf = f;
    
        /* store lo byte */
        t = (unsigned) pf & 0xff;
        sp = (unsigned char *) t;
    
        /* store hi byte */
        t = (unsigned) pf >> 8;
        sp = (unsigned char *) t;
    }

  2. #17
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You should be able to simply use
    Code:
    *(unsigned*)(stackPointer-1) = (unsigned)your_function;
    providing stackPointer points to "SP before interrupt" from your picture.
    If the stack pointer points to "SP after interrupt" use (stackPointer+8)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. microprocessor
    By BEN10 in forum General Discussions
    Replies: 16
    Last Post: 09-15-2009, 03:17 PM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  3. padding
    By R.Stiltskin in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2009, 04:24 PM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  5. IPv6 multicast example code
    By Sang-drax in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-25-2005, 09:26 AM