Skip to main content
othane
Associate II
April 28, 2010
Question

access R0-R4 in C

  • April 28, 2010
  • 3 replies
  • 865 views
Posted on April 28, 2010 at 23:53

access R0-R4 in C

    This topic has been closed for replies.

    3 replies

    disirio2
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:49

    If you are using GCC it is not difficult nor dangerous, you can enforce a variable into a specific register, you just declare it like this:

    register unsigned int n asm(''r2'');

    You may also need to declare the variable ''volatile'' or the compiler may optimize out assignments to the variable if it is not explicitly used after the assignment.

    GCC is very effective if you need to do inline assembler.

    Giovanni

    ---

    ChibiOS/RT http://chibios.sourceforge.net

    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 13:49

    If you want to exercise this level of control you simply need to write the function in assembler. In C you could cast an address to a function call and also provide it with parameters. This would in effect permit you to define r0-r4, and pc. You could also use assembler inline within your function(s).

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    othane
    othaneAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:49

    Thanks, I would do this but I cannot grantee we will be using GCC where this code is going to run.

    I ended up doing something similar to what clive1 suggested, which is just copy all the 4 parameters into the registers whether they are used by the function or not , which seems to work well enough, eg:

     

    typedef void (*internal_task)(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3);

     

    void SYS_Run(void (*task), uint8_t argc, uint32_t argv[])

     

    {

     

        internal_task t = (internal_task)task;

     

        t(argv[0], argv[1], argv[2], argv[3]);

     

    }