Thread: Running Function from specific memory location

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    Running Function from specific memory location

    Hi,
    I'll explain what my overall objective is first..
    I would like to be able to store small functions/algorithms in external flash of a BF561 processor. I will then copy these functions into allocated space of main memory and run.

    My problems are.
    How do I copy a function to a specific memory location? I assume I use malloc to create space then copy the address range.

    The hard part is then executing this function. I have complex ideas of creating a function container within the allocated space, then moving the function to a the space within, then i can call the function container. maybe.

    I've assumed you cannot actually control the program counter. As that would make things simple, if a little dangerous.

    thanks for any help,

    Matt

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    These -> ADSP-BF561 | Blackfin Symmetric Multi-Processor for Consumer Multimedia | Blackfin Processors | Embedded Processing and DSP | Analog Devices
    ?

    > How do I copy a function to a specific memory location?
    memcpy I guess.

    > I assume I use malloc to create space then copy the address range.
    Or a fixed array somewhere.
    It probably doesn't matter (*) which approach you take.

    The next trick would be finding out the location and size of your functions. Which tools are you using?
    This is anywhere from being pretty easy to being a PITA.


    > The hard part is then executing this function.
    If all your movable code has the same calling convention (ie, C), then the stub to call it would be a short asm function to
    - pop the current stack frame (of the stub function itself)
    - jump to the start of the moved function.
    This means a normal return from the moved function will return to the caller of the stub.


    Oh, and one more thing, when you generate your movable code, you need to specify that it is position independent code (helpful). If you can't generate PIC, then you would have to "link" each function against a known destination address (again, this is messy).


    (*) one place where it might matter is if the processor is a Harvard machine with separate program and data memory. All of the memory you could normally write to would be data.
    Program memory may be writeable, but there's probably some library needed to do it.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    13
    thanks,

    yeah thats the processor.
    I'll work through your advice tomorrow

    I believe i will eventually have to turn the algorithms into relocatedable object files, then figure out how to execute them.

    Matt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

Tags for this Thread