简体   繁体   English

为什么这个简单的汇编程序可以使用 AT&T 语法而不是 Intel 语法?

[英]Why does this simple assembly program work in AT&T syntax but not Intel syntax?

What's wrong with this code (Running on x86_64 Linux)?这段代码有什么问题(在 x86_64 Linux 上运行)?

.intel_syntax
.text
.globl _start

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, 14
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

.data
msg:
    .ascii "Hello, world!\n"

When I run it:当我运行它时:

$ clang -o hello_intel hello_intel.s  -nostdlib  && ./hello_intel

No output.没有输出。 Let's strace it:让我们跟踪它:

$ strace ./hello_intel
execve("./hello_intel", ["./hello_intel"], [/* 96 vars */]) = 0
write(1, 0x77202c6f6c6c6548, 14)        = -1 EFAULT (Bad address)
exit(0)                                 = ?
+++ exited with 0 +++

It's dereferencing msg instead of using its location.它正在取消引用msg而不是使用它的位置。 Why?为什么?

If I use AT&T syntax instead...如果我改用 AT&T 语法...

.text
.globl _start

_start:
    mov $1, %rax
    mov $1, %rdi
    mov $msg, %rsi
    mov $14, %rdx
    syscall

    mov $60, %rax
    mov $0, %rdi
    syscall

.data
msg:
    .ascii "Hello, world!\n"

...it works fine: ...它工作正常:

$ clang -o hello_att hello_att.s  -nostdlib && ./hello_att
Hello, world!

What's the difference between these two?这两者有什么区别?

Here's the working one:这是工作之一:

$ objdump -d hello_att -s -M intel

hello_att:     file format elf64-x86-64

Contents of section .text:
 4000e8 48c7c001 00000048 c7c70100 000048c7  H......H......H.
 4000f8 c6160160 0048c7c2 0e000000 0f0548c7  ...`.H........H.
 400108 c03c0000 0048c7c7 00000000 0f05      .<...H........  
Contents of section .data:
 600116 48656c6c 6f2c2077 6f726c64 210a      Hello, world!.  

Disassembly of section .text:

00000000004000e8 <_start>:
  4000e8:   48 c7 c0 01 00 00 00    mov    rax,0x1
  4000ef:   48 c7 c7 01 00 00 00    mov    rdi,0x1
  4000f6:   48 c7 c6 16 01 60 00    mov    rsi,0x600116
  4000fd:   48 c7 c2 0e 00 00 00    mov    rdx,0xe
  400104:   0f 05                   syscall 
  400106:   48 c7 c0 3c 00 00 00    mov    rax,0x3c
  40010d:   48 c7 c7 00 00 00 00    mov    rdi,0x0
  400114:   0f 05                   syscall 

And here's the broken one:这是破碎的:

$ objdump -d hello_intel -s -M intel

hello_intel:     file format elf64-x86-64

Contents of section .text:
 4000e8 48c7c001 00000048 c7c70100 0000488b  H......H......H.
 4000f8 34251701 600048c7 c20e0000 000f0548  4%..`.H........H
 400108 c7c03c00 000048c7 c7000000 000f05    ..<...H........ 
Contents of section .data:
 600117 48656c6c 6f2c2077 6f726c64 210a      Hello, world!.  

Disassembly of section .text:

00000000004000e8 <_start>:
  4000e8:   48 c7 c0 01 00 00 00    mov    rax,0x1
  4000ef:   48 c7 c7 01 00 00 00    mov    rdi,0x1
  4000f6:   48 8b 34 25 17 01 60    mov    rsi,QWORD PTR ds:0x600117
  4000fd:   00 
  4000fe:   48 c7 c2 0e 00 00 00    mov    rdx,0xe
  400105:   0f 05                   syscall 
  400107:   48 c7 c0 3c 00 00 00    mov    rax,0x3c
  40010e:   48 c7 c7 00 00 00 00    mov    rdi,0x0
  400115:   0f 05                   syscall 

So the important difference here is 0x600116 vs QWORD PTR ds:0x600117 , which indeed looks like the difference between a pointer and dereferencing a pointer.所以这里的重要区别是0x600116QWORD PTR ds:0x600117 ,这确实看起来像是指针和取消引用指针之间的区别。

So how do you not dereference the pointer in the Intel syntax code?那么如何不解引用 Intel 语法代码中的指针呢?

Here's code that works in GCC:这是适用于 GCC 的代码:

.intel_syntax noprefix
.text
.globl _start

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, offset msg
    mov rdx, 14
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

.data
msg:
    .ascii "Hello, world!\n"

Both the noprefix and the offset had to be added.必须添加noprefixoffset Sadly this does not work with clang :可悲的是,这不适用于 clang

hello_intel.s:8:24: error: unknown token in expression
    mov rsi, offset msg
                       ^

However, you can work around the issue by using lea instead of mov :但是,您可以通过使用lea而不是mov来解决这个问题:

lea rsi, [msg+rip]

This works in both clang and gcc.这适用于 clang 和 gcc。 (And works in position-independent code). (并在与位置无关的代码中工作)。 It's the standard way to put static addresses in registers.这是将静态地址放入寄存器的标准方法。

mov esi, imm32 is a minor optimization over RIP-relative LEA for position-dependent, but mov rsi, sign_extended_imm32 is the same code size as LEA. mov esi, imm32是对 RIP 相对 LEA 的次要优化,用于位置相关,但mov rsi, sign_extended_imm32与 LEA 的代码大小相同。 It's apparently not possible in Clang's .intel_syntax , even though clang emits offset msg when compiling: How to get `mov rdx, symbol` to move symbol value and not value at symbol's address in clang intel-syntax?在 Clang 的.intel_syntax显然是不可能的,即使 clang 在编译时发出offset msgHow to get `mov rdx, symbol` 来移动符号值而不是在 clang intel-syntax 中符号地址的值?

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

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