简体   繁体   English

为什么从 AT&T 切换到 Intel 语法会使本教程使用 GAS 出现段错误?

[英]Why does switching from AT&T to Intel syntax make this tutorial segfault using GAS?

I'm working through some of the tutorials on http://www.ibm.com/developerworks/linux/library/l-gas-nasm/index.html to familiarize myself with x86/x64.我正在阅读http://www.ibm.com/developerworks/linux/library/l-gas-nasm/index.html上的一些教程,以熟悉 x86/x64。 This tutorial code compiles and runs without a hiccup using the provided code, which uses AT&T syntax:本教程代码使用提供的代码编译并运行,不会出现问题,该代码使用 AT&T 语法:

.global main
.text
main:                               # This is called by C library's startup code
    mov     $message, %rdi          # First integer (or pointer) parameter in %edi
    call    puts                    # puts("Hello, World")
    ret                             # Return to C library code
message:
    .asciz "Hello, World"           # asciz puts a 0x00 byte at the end

However, when I convert this code to Intel syntax, I get a "Segmentation fault" error.但是,当我将此代码转换为 Intel 语法时,出现“分段错误”错误。

.intel_syntax noprefix
.global main
.text
main:                               # This is called by C library's startup code
    mov     rdi, message            # First integer (or pointer) parameter in %edi
    call    puts                    # puts("Hello, World")
    ret                             # Return to C library code
message:
    .asciz "Hello, World"           # asciz puts a 0x00 byte at the end

I'm not familiar with x86, so perhaps I'm missing something.我不熟悉 x86,所以也许我错过了一些东西。 Any ideas?有任何想法吗?

In AT&T syntax, mov $message, %rdi , the $ means immediate , meaning the address of message .在 AT&T 语法mov $message, %rdi$表示immediate ,表示message的地址。

In GAS's Intel syntax, mov rdi, message means absolute addressing, meaning the content at message .在 GAS 的 Intel 语法mov rdi, message表示绝对寻址,表示message处的内容 To get the actual address of message , you need to supply the offset keyword: mov rdi, offset message .要获取message的实际地址,您需要提供offset关键字: mov rdi, offset message

Disassebly of the two binaries shows the difference:两个二进制文件的可拆卸显示了差异:

AT&T:美国电话电报公司:

0000000000000000 <main>:
0:   48 c7 c7 00 00 00 00    mov    $0x0,%rdi

Intel:英特尔:

0000000000000000 <main>:
0:   48 8b 3c 25 00 00 00 00    mov    0x0,%rdi
     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM