且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

段错误的指令MOVQ?

更新时间:2022-10-15 16:33:19

原来,它已经太长时间,因为我已经写了ASM,我忘记了与 $ 在AT& T公司的语法。我发现提醒这里时仔细检查AT& T公司语法。​​

  ASM(MOVQ $ 100000000,RCX%;
        startofloop:;
        子$为0x1,%RCX;
        JNE startofloop;);

Consider the following short program.

int main(){                      
    asm("movq 0x5F5E100, %rcx;"  
            "startofloop: ; "    
            "sub 0x1, %rcx; "    
            "jne startofloop; ");
}                                

This program compiles fine, but when it is run, it segfaults on the initial movq instruction.

I must be missing something obvious, but I hope someone here can point it out for me.

I am running on Debian 8, with kernel 3.16.0-4-amd64, in case that is relevant.


For future reference, this is what the compiler generated.

main:                                                                  
.LFB0:                                                                 
    .cfi_startproc                                                     
    pushq   %rbp                                                       
    .cfi_def_cfa_offset 16                                             
    .cfi_offset 6, -16                                                 
    movq    %rsp, %rbp                                                 
    .cfi_def_cfa_register 6                                            
#APP                                                                   
# 2 "asm_fail.c" 1                                                     
    movq 0x5F5E100, %rcx;startofloop: ; sub 0x1, %rcx; jne startofloop;
# 0 "" 2                                                               
#NO_APP                                                                

It turns out that it has been too long since I have written asm, and I had forgotten that one must preface immediate values with $ in AT&T syntax. I found the reminder here when double-checking AT&T syntax.

asm("movq $100000000, %rcx;"
        "startofloop: ; "
        "sub $0x1, %rcx; "
        "jne startofloop; ");