Home > Software engineering >  How to put an inline assembly jump to another function inside an IF condition?
How to put an inline assembly jump to another function inside an IF condition?

Time:10-07

I'm trying to jump to a fun2 function in assembly, because in C I haven't found a way to pass all variadic parameters to fun2 via fun1 without using a define like #define fun1 (...) fun2 (__ VA_ARGS__)

The problem is that if I move the jump instruction inside an IF condition, the code doesn't work, I'm working on an ARMv7 with a GCC compiler.

void fun2(char *format, ...){
   va_list arg_list;
   va_start(arg_list, format);
   vprintf(format, arg_list);
   va_end(arg_list);
}

//Works
void fun1(char *format, ...){
   __asm__("ldr pc,=fun2");
}

bool my_condition;
//Doesn't work
void fun1(char *format, ...){
   if(my_condition) {
      __asm__("ldr pc,=fun2");
   }
}

int main(){
   fun1("test: %d", 100);
}

CodePudding user response:

I tried to write an assembly solution, which seems to work (I see the log on the UART port), but I noticed that this code only works if I use the R2 register, if I use any other register like R3, this code doesn't work anymore I wonder if this solution works just for luck

----Code modified according to suggestions---

file1.c

__attribute__((section(".text.fun"))) void fun2(char *format, ...) {
   va_list arg_list;
   va_start(arg_list, format);
   vprintf(format, arg_list);
   va_end(arg_list);
   
   /*...*/
}

file2.c

uint8_t *enable_trace = &(config.enable_trace);
__attribute__((naked, section(".text.fun"))) void fun1(char *format, ...) {
    __asm__(
        "ldr R12, =enable_trace    \n\
        ldr R12, [R12]             \n\
        ldrb R12, [R12]            \n\
        cmp R12, #1                \n\
        beq fun2                   \n\
        bx lr");
}

int main(){
   fun1("test: %d", 100);
}

Compiler: Snapdragon LLVM ARM Compiler 4.0.3 (based on llvm.org 4.0 )

ARM Cortex A7

  • Related