如何让内联程序集通过

如何让内联程序集通过

本文介绍了如何让内联程序集通过-O1优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



代码可以通过GCC并正确运行而不需要优化,但是如果我选择-O1优化(也是当运行代码时,程序会产生分段错误。



基本上,函数确实保存上下文并跳转到下一个上下文。

  void __attribute__((noinline))__lwt_dispatch(lwt_context * curr,lwt_context * next)
{
__asm__ __volatile


mov 0xc(%ebp),%eax\\\
\t
mov 0x4(%eax),%ecx\\\
\t
mov (%eax),%edx\\\
\t
mov 0x8(%ebp),%eax\\\
\t
add $ 0x4,%eax\\\
\\ \\ t
mov 0x8(%ebp),%ebx\\\
\t
push%ebp\\\
\t
push%ebx\\\
\ t
mov%esp,(%eax)\\\
\t
movl $ return,(%ebx)\\\
\
mov %ecx,%esp\\\
\t
jmp *%edx\\\
\t
return:pop%ebx \\\
\t
pop%ebp\\\
\t
);


解决方案

感谢您的帮助,解决这个问题。


  1. 通常将此函数编译为单独的.o文件,然后使用O3与其他函数进行优化文件。使用内联汇编的

  2. 比这个函数简单得多。如下所示:



    int foo = 10,bar = 15;
    asm 易变(addl %% ebx,%% eax
    := a(foo)
    :a (foo),b(bar)
    );
    printf(foo + bar =%d\\\
    ,foo);

  3. 另一篇文章帮我弄清楚了标签问题,这里:



I have following dispatch code for my user level thread library.

The code can pass GCC and runs correctly without optimization, but if I choose -O1 optimization (also higher levels), when run the code, program generates segmentation fault.

Basically the function does save context and jump to next context.

void __attribute__ ((noinline)) __lwt_dispatch(lwt_context *curr, lwt_context *next)
{
__asm__ __volatile
    (

    "mov 0xc(%ebp),%eax\n\t"
    "mov 0x4(%eax),%ecx\n\t"
    "mov (%eax),%edx\n\t"
    "mov 0x8(%ebp),%eax\n\t"
    "add $0x4,%eax\n\t"
    "mov 0x8(%ebp),%ebx\n\t"
    "push %ebp\n\t"
    "push %ebx\n\t"
    "mov %esp,(%eax)\n\t"
    "movl $return,(%ebx)\n\t"
    "mov %ecx,%esp\n\t"
    "jmp *%edx\n\t"
    "return: pop %ebx\n\t"
    "pop %ebp\n\t"
    );
}
解决方案

Thanks for help, I figured out some ways to solve it.

  1. Normally compile this function as a separate .o file then use O3 to optimize it with other files.

  2. using inline assembly is much easier and simpler than this function. Like below:

    int foo = 10, bar = 15;asm volatile("addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) );printf("foo+bar=%d\n", foo);

  3. Another post has helped me figuring out labeling problem, see here: Labels in GCC inline assembly

这篇关于如何让内联程序集通过-O1优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:37