Re: How to make gcc aware of arguments in embedded assembly code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:

__attribute__((naked))
int f(int x, int y)
{
    __asm__ (
        "push %rbp"
        "mov %rsp, %rbp"
        "lea (%rdi, %rsi, 1), %rax\n"
        "pop %rbp"
        "ret\n"
    );
}
-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux