On Fri, 2021-04-30 at 03:12 +0800, Xi Ruoyao wrote: > On Thu, 2021-04-29 at 19:30 +0100, Jonathan Wakely via Gcc-help wrote: > > On Thu, 29 Apr 2021, 18:55 Peng Yu via Gcc-help, < > > gcc-help@xxxxxxxxxxx> > > wrote: > > > > > Hi, > > > > > > I have the following embedded assembly code which works fine without > > > optimization. But it fails when the optimization is enabled. How to > > > make gcc aware of the fact that the assembly code takes arguments? > > The problem is at -O2 and above -fomit-frame-pointer is enabled, so %rbp > is used as a general purpose register. Then pop %rbp does not make > sense. > > > Did you check the manual? > > > > https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html > > If the OP wants to work with "inline" (that's how we call it, not > "embedded") assembly, the extended asm should be definitely used. But > if the wants to write some function with just assembly, try this: > > __attribute__((naked)) > int f(int x, int y) > { > __asm__ ( > "lea (%rdi, %rsi, 1), %rax\n" > "ret\n" > ); > } > > Or the "unoptimized" version: I lost some '\n' in the previous post. I mean: __attribute__((naked)) int f(int x, int y) { __asm__ ( "push %rbp\n" "mov %rsp, %rbp\n" "lea (%rdi, %rsi, 1), %rax\n" "pop %rbp\n" "ret\n" ); } -- Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University