GCC使用asm块错误地内联函数

时间:2015-05-13 21:11:42

标签: gcc assembly x86 inline-assembly

在将代码从Watcom移植到GCC的过程中,我注意到错误生成的函数,并且无法弄清楚它为什么会发生。这是最小的例子:

#include <stdio.h>

bool InstallExceptionHandler(int exceptionNo, void (*handler)())
{
    bool result;
    asm volatile (
        "mov  ax, 0x203     \n"
        "mov  cx, cs        \n"
        "int  0x31          \n"
        "sbb  eax, eax      \n"
        "not  eax           \n"
        : "=eax" (result)
        : "ebx" (exceptionNo), "edx" (handler)
        : "cc", "cx"
    );
    return result;
}

void Exception13Handler(){}

main()
{
    if (!InstallExceptionHandler(13, Exception13Handler)) {
        printf("Success!");
        return 0;
    } else {
        printf("Failure.");
        return 1;
    }
}

在第63行,函数InstallExceptionHandler()被内联,但剩下的就是asm块。缺少设置输入寄存器edx和ebx的代码。如果函数被赋予__attribute __((noinline))并且发出实际调用,则生成正确的代码。这是编译器中的一些错误还是我的代码无效?

48                      .globl  _main
50                  _main:
51                  LFB15:
52                      .cfi_startproc
53 0000 55              push    ebp
54                      .cfi_def_cfa_offset 8
55                      .cfi_offset 5, -8
56 0001 89E5            mov ebp, esp
57                      .cfi_def_cfa_register 5
58 0003 83E4F0          and esp, -16
59 0006 83EC10          sub esp, 16
60 0009 E8000000        call    ___main
60      00
61                  /APP
62                   # 15 "b.cpp" 1
63 000e 66B80302        mov  ax, 0x203     
64 0012 668CC9      mov  cx, cs        
65 0015 CD31        int  0x31          
66 0017 19C0        sbb  eax, eax      
67 0019 F7D0        not  eax      

命令行:gcc -O3 -m32 -masm = intel -Wa,-adhlns = b.lst b.cpp, gcc -v输出: 使用内置规格。 COLLECT_GCC =克++ COLLECT_LTO_WRAPPER = F:/ MinGW的/ bin中/../的libexec / GCC /的mingw32 / 4.8.1 / LTO-wrapper.exe 目标:mingw32 配置为:../ gcc-4.8.1/configure --prefix = / mingw --host = mingw32 --build = mingw32 --without-pic  --enable-shared --enable-static --with-gnu-ld --enable -lto --enable-libssp --disable-multilib --ena ble-languages = c,c ++,fortran,objc,obj-c ++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32 -registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp = / usr / src / pkg / gm p-5.1.2-1-mingw32-src / bld --with-mpc = / usr / src / pkg / mpc-1.0.1-1-mingw32-src / bld --with-mpfr = --with-sy stem-zlib --with-gnu-as --enable-decimal-float = yes --enable-libgomp --enable-threads --with-libiconv -prefix = / mingw32 --with-libintl-prefix = / mingw --disable-bootstrap LDFLAGS = -s CFLAGS = -D_USE_32BIT_TIM E_T 线程模型:win32 gcc版本4.8.1(GCC)

1 个答案:

答案 0 :(得分:1)

约束不是寄存器名称,通过指定约束&#34; ebx&#34;您允许编译器使用带符号的32位常量,RBX / EBX / BX / BL寄存器或任何SSE寄存器。 GCC选择使用常量,并将%1替换为常量。

要使用您希望的寄存器,您需要约束abd

相关问题