Thread: saving an address in two bytes

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    saving an address in two bytes

    Hi everyone I need to assign the address of my function to the
    first two bytes of my stackPointer.

    How do I do this?
    Code:
    void function foo()
    {
    }
    
    stackPointer = acWorkspaces + (WORKSPACE*idx);
    
    stackPointer = low bytes of address function foo
    
    (stackPointer - 1) = high bytes of address function foo.
    any help appreciated,

    Ted.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    StackPointer = (&Myfunc & 0x0000FFFF);
    StackPointer - 1 = (&MyFunc & 0xFFFF0000) >> 16;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Hi everyone I need to assign the address of my function to the
    > first two bytes of my stackPointer.
    Why?

    For one thing, this is extremely dependent on your platform.

    Presumably, you're wanting to call foo() at some point following all this jiggery pokery. Something which is also beset with implementation details.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Hmm that didn't work

    Code:
     	 StackPointer = (&Myfunc & 0x0000FFFF);
    StackPointer - 1 = (&MyFunc & 0xFFFF0000) >> 16;
    I'm probably not using it correctly, I forgot to say.. the adress is only 16 bits long, so I tried the following:

    Code:
        stackPointer = ((currProc->pc) & 0x00FF)
        (stackPointer - 1) = ((currProc->pc) & 0xFF00) >> 8;
    
    pc is a function pointer so I'm thinking the way I'm using it is correct. no?
    
    typedef struct pcb_struct 
    {
     ..
        void        (*pc)(void); // function pointer
       ..
    } ProcCtrlBlock;
    I get this error though:

    os.c:247: invalid operands to binary &
    os.c:248: invalid operands to binary &

    Ted

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    So why not just assign a function pointer then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    As Salem noted, this is messy. Endianness will play havoc with this code if you decide to port it to a little endian system.
    Besides that I am still unsure why you are trying to do this?
    You're getting that error because the bitwise operator "&" expects both integral operands.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Quote Originally Posted by Salem View Post
    So why not just assign a function pointer then?
    Yes, but how? the address has a size of two bytes. If I assign it to one byte, it will only store part of the address?

    Besides that I am still unsure why you are trying to do this?
    I'd have to write paragraphs to explain what I'm trying to do, and I don't have the time. In short, I need to build an operating system, and I'm trying to get the stuff in the right place for an assembly instruction.

    Ted

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by afflictedd2 View Post
    Yes, but how? the address has a size of two bytes. If I assign it to one byte, it will only store part of the address?
    How many bytes of storage does a pointer variable take-up as per your compiler?

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Quote Originally Posted by itCbitC View Post
    How many bytes of storage does a pointer variable take-up as per your compiler?
    I'm not sure what command I need to execute to get that information for you?
    I'm using the m6811-elf-gcc compiler. I was told it is really primitive and the sizeof an int is like 16 bits.

    Ted.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by afflictedd2 View Post
    I'm not sure what command I need to execute to get that information for you?
    I'm using the m6811-elf-gcc compiler. I was told it is really primitive and the sizeof an int is like 16 bits.

    Ted.
    As a pointer variable contains the address of an object it doesn't matter whether it is the address of a function or a simple object type like an int or char.
    To store the address of your function in the stack pointer you can as well do
    Code:
    void (*pc) (void); 
    char *StackPointer;
    StackPointer = (char *) pc;

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    How is like this:
    Code:
    void foo ( int a );
    
    // later on
    void (*fnptr)( int );
    
    // then assign it
    fnptr = foo;
    
    // then call it
    fnptr( 123 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Quote Originally Posted by Salem View Post
    How is like this:
    Code:
    void foo ( int a );
    
    // later on
    void (*fnptr)( int );
    
    // then assign it
    fnptr = foo;
    
    // then call it
    fnptr( 123 );
    That's not what I'm looking for though. I don't want a pointer to a function. I need to store the address of that function in the bytes (stackpointer) and (stackpointer -1).

    Ted

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Back to my original question - why?

    Are you trying to call a function by mucking about with the return address of the current function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    First of all, you can't access the real stack pointer on the 68HC11 using C.
    The 68HC11 uses a special register "SP" and to access it you have to drop down to the assembly level.
    And what is the type of your StackPointer variable i.e. is it a pointer to a char or ant int?
    Also, are you trying to simulate the hardware stack pointer SP in software? Still can't make sense of what you're trying to do.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Quote Originally Posted by itCbitC View Post
    First of all, you can't access the real stack pointer on the 68HC11 using C.
    The 68HC11 uses a special register "SP" and to access it you have to drop down to the assembly level.
    And what is the type of your StackPointer variable i.e. is it a pointer to a char or ant int?
    Also, are you trying to simulate the hardware stack pointer SP in software? Still can't make sense of what you're trying to do.
    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.
    Last edited by afflictedd2; 02-16-2010 at 06:25 PM.

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