IvanDi
IvanDi

Reputation: 147

How to pass function address to Assembler Instructions with C Expression Operands

With gcc/clang for ARM cortex M, is there a way to pass a function address as a constant in Assembler Instructions with C Expression Operands ? More precisely I'd like to load R12 with function address (stored in the memory): ldr R12, =func within a C function, foe example like this one

// __attribute__((naked)) 
int loader(int fn)
{
__asm ("ldr R12, =%0"::??? (fn):"r12");
// ... then  SVC #0, and the R0 is the return value 
}

The question is what exactly I have to put for the Input Operand?

EDIT: Thanks for the comments! Actually I need to re-implement the KEIL's __svc_indirect(0) which loads R12 with function address and passes up to four arguments in R0..R3 (see __svc_indirect

Upvotes: 1

Views: 1115

Answers (1)

fuz
fuz

Reputation: 93107

Use an i constraint and manually prepend the = character:

__asm ("ldr r12, =%0" :: "i"(fn) : "r12");

Note that the inline assembly statement is still incorrect for other reasons, some of which were outlined in the comments on your question. Also consider using a register-constrainted variable for this sort of thing:

register int fn asm("r12");
__asm ("" :: "r"(fn));

Upvotes: 4

Related Questions