This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Calling an Assembly Functon From C (RVMDK)

Hello,

I am struggling to do something that should be very simple: call an assembly function from a C function. Can anyone give me some pointers or an example of how to do this?

I am using uVision 3 with Realview MDK C compiler.

All of the documentation that I am finding points me to use inline assembly commands. However, this won't work, as I need the assembly code to modify the stack pointer.

Here is what I would like the code to:

void ChangeRegion (void)
{
     asm_adjust_sp(0x78000);  //call assembly to change the stack pointer
}

//asm_adjust_spi(Ulong ulAddress)
//1. change stack pointer to 0x4000F000
//2. branch to a the address contained by ulAddress

Any help that you can provide would be greatly appreciated.

Thanks!

Parents
  • Write a C function with the correct prototype.

    void asm_adjust_sp(unsigned n) {
        // some dummy code that accesses the parameter n
        // and have a side effect, so the compiler doesn't
        // throws away the code.
        my_dummy_global_variable = n;
    }
    

    Look at the assembler listing of the function. Cut the relevant parts and place in an assembler file, and then add your specific action code.

    Then add a function prototype in a header file:

    void asm_adjust_sp(unsigned);
    

    But as Tamir notes, modifying the stack pointer is not a simple task. It is way more advanced than calling an assembler function from C. With a moved stack pointer, return addresses and auto variables from the current call tree will no longer be available, so you must think twice about returning to previous calling functions.

    Before starting on this task: Please specify why you think you have to modify the stack pointer from running C code.

Reply
  • Write a C function with the correct prototype.

    void asm_adjust_sp(unsigned n) {
        // some dummy code that accesses the parameter n
        // and have a side effect, so the compiler doesn't
        // throws away the code.
        my_dummy_global_variable = n;
    }
    

    Look at the assembler listing of the function. Cut the relevant parts and place in an assembler file, and then add your specific action code.

    Then add a function prototype in a header file:

    void asm_adjust_sp(unsigned);
    

    But as Tamir notes, modifying the stack pointer is not a simple task. It is way more advanced than calling an assembler function from C. With a moved stack pointer, return addresses and auto variables from the current call tree will no longer be available, so you must think twice about returning to previous calling functions.

    Before starting on this task: Please specify why you think you have to modify the stack pointer from running C code.

Children