Using function pointers to jump to an address in memory?

Comments

8 comments

  • Avatar
    Paul Curtis

    Are you and Adam Koch using CrossWorks then?

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    If you are using a large code memory model then code pointers are thunked, i.e. the 16-bit code pointer points to a 20-bit address indirected to the code.  To understand this, try executing a regular function pointer in low-level mode and see how the code works.

    0
    Comment actions Permalink
  • Avatar
    Rhys Heffernan

    Thanks for the response.  

    So if I tried this on one of the smaller msp430s it should work?  My actual device uses a msp430f248 which has only 48K of flash, the next time I get a chance I will have a try on there and see what happens.

    Thanks a lot for the help, I'm sure you have save me much heartache.
    Rhys Heffernan



    So if I tried this on one of the smaller msp430s it should work?  My actual device uses a msp430f248 which has only 48K of flash, the next time I get a chance I will have a try on there and see what happens.

    Rhys Heffernan

     

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    It would probably work on the smaller variant, yes, as function pointers in small memory model are direct pointers.

    0
    Comment actions Permalink
  • Avatar
    Rhys Heffernan

    Thanks again.  Sorry for the strange post above with the message twice, my computer had a bit of a spaz and deleted my graphics drivers and set my resolution to 800*600 so I couldn't see that I was incorrect and I can't seem to be able to edit my message.

    Also I sent an email replying to your question about Adam Kosh, however I sent it to the norelpy email address (again the resolution meant I did not see the address until after I had sent it).  If you didn't get it let me know and an email address that you would like it sent to.

    0
    Comment actions Permalink
  • Avatar
    Bart Tormans

    So, if i get it correctly, a function pointer on a MSP430X device, actually takes up 6 bytes?

    2 for the 16bit pointer, which points to a 20(32)bit address, stored somewhere else, thus, inderectly pointing to the correct location?

    Where is the real addres stored? Same place as the thunked pointer (local, static, const) ?

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    There are a finite number of entry points to functions, all known to the compiler.  When one of these functions is called, the compiler creates a single instance of a thunk that requires four bytes in code memory.  A function pointer is two bytes and indirects through the thunk.  Therefore, you require two bytes to store a function pointer and pass it around and four bytes (only) per function entry point (and only then if it is used by assigning it to a function pointer).

    0
    Comment actions Permalink
  • Avatar
    Bart Tormans

    Ok. That clears it up perfectly.

    Thanks

    0
    Comment actions Permalink

Please sign in to leave a comment.