Due to some curiosity, I was trying to use some assembly with my Arduino MEGA 2560. I am not able to invoke a function from a c++ function pointer array.
When I uncomment the call function, func_a
runs. However, when I almost reproduce the assembly, it doesnt work.
Example:
extern "C" void __attribute__ ((used, noinline, noreturn)) func_a();
typedef void (*volatile func_ptr)();
func_ptr tasks[MAX_TASKS] = {&func_a};
extern "C"
void __attribute__ ((used, noinline)) call(func_ptr *ptr) {
(*ptr)();
}
void __attribute__ ((noreturn, used)) setup() {
// call(tasks);
asm volatile (
"lds r26, (tasks)\n"
"lds r27, (tasks + 1)\n"
"ld r30, X+\n"
"ld r31, X\n"
"eijmp\n"
);
}
Generated assembly recovered from avr-objdump
000001c6 <setup>:
1c6: a0 91 00 02 lds r26, 0x0200 ; 0x800200 <tasks>
1ca: b0 91 01 02 lds r27, 0x0201 ; 0x800201 <tasks+0x1>
1ce: ed 91 ld r30, X+
1d0: fc 91 ld r31, X
1d2: 19 94 eijmp
1d4: 08 95 ret
000001d6 <call>:
1d6: dc 01 movw r26, r24
1d8: ed 91 ld r30, X+
1da: fc 91 ld r31, X
1dc: 19 94 eijmp
An specific resources I should refer for things like this?
If it matters, I'm using platformio on a linux system for compilation.