Use an assembly function in C without call and ret and store the return address in register

Viewed 229

I wrote a function in assembly and now I want to use it in C but my function is not for 'call' and 'ret' and the return address will be store in a register and in the function, we just jmp to that location (register) and there is no 'ret' so for executing this function in C, I just need to make a label for the function return address and load that label memory address into the 'rdx' and jmp to that assembly function, and then it's done so I designed this code

unsigned
calling_asm_func() {
    unsigned res;
    __asm__ __volatile__("jmp\tmy_asm_func" : "=a" (res) : "d" (&& result) : "rcx", "rdi", "rsi", "r8", "r9", "r10", "r11", "memory");
    result:
    return res;
}

it looks fine and it's ok but the generated assembly code is wrong !!!!!!!!

calling_asm_func:
.L2:
  mov edx, OFFSET FLAT:.L2
  jmp my_asm_func
  ret

.L2 Must be above of ret as you can see it in the C code what is the problem? How can I fix it?

It must be something like this:

calling_asm_func:
  mov edx, OFFSET FLAT:.L2
  jmpmy_asm_func
.L2:
  ret

See this on GodBolt with GCC 10.2

Also, it's funny ! With -O1 option, it's OK and there is no problem !!!!!!! But the problem is with -O2 and -O3 !!!

0 Answers
Related