无法将.data中的变量移动到Mac x86 Assembly中进行注册 [英] Unable to move variables in .data to registers with Mac x86 Assembly
问题描述
我已经用AT& T语法编写了一小段程序集,并且当前在.data
部分中声明了三个变量.但是,当我尝试将这些变量中的任何一个移至诸如%eax
的寄存器时,会出现来自gcc
的错误.代码和错误消息如下:
I have written a small piece of assembly with AT&T syntax and have currently declared three variables in the .data
section. However, when I attempt to move any of those variables to a register, such as %eax
, an error from gcc
is raised. The code and error message is below:
.data
x:.int 14
y:.int 4
str: .string "some string\n"
.globl _main
_main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl x, %eax; #attempting to move the value of x to %eax;
leave
ret
引发的错误是:
call_function.s:14:3:错误:在64位模式下不支持32位绝对寻址
call_function.s:14:3: error: 32-bit absolute addressing is not supported in 64-bit mode
movl x,%eax;
movl x, %eax;
^
我还尝试过先在x
前面添加$
字符来移动值,但是会出现clang
错误:
I have also tried moving the value by first adding the $
character in front of x
, however, a clang
error is raised:
clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
有人知道如何将存储在x
中的值成功地移到%eax
吗?我在Mac OSX上使用x86程序集,并使用gcc
进行编译.
Does anyone know how the value stored in x
can be successfully moved to %eax
? I am using x86 assembly on Mac OSX and compiling with gcc
.
推荐答案
相对于RIP的寻址模式是在MacOS上寻址静态数据的唯一好选择;图像基址大于2 ^ 32,因此即使在与位置相关的代码中也不可用32位绝对地址(与x86-64 Linux不同). RIP相对于静态数据的寻址与位置无关,因此即使在与位置无关的可执行文件(ASLR)和库中也可以使用.
A RIP-relative addressing mode is the only good option for addressing static data on MacOS; the image base address is above 2^32 so 32-bit absolute addresses aren't usable even in position-dependent code (unlike x86-64 Linux). RIP-relative addressing of static data is position-independent, so it works even in position-independent executables (ASLR) and libraries.
movl x(%rip), %eax
是相对于RIP的AT& T语法.
movl x(%rip), %eax
is the AT&T syntax for RIP-relative.
mov eax, dword ptr [rip+x]
.
mov eax, dword ptr [rip+x]
in GAS .intel_syntax noprefix
.
或者,要将符号的地址保存到寄存器中,请 lea x(%rip), %rdi
Or, to get the address of a symbol into a register, lea x(%rip), %rdi
NASM语法:mov eax, [rel x]
,或使用default rel
,因此[x]
是RIP相对的.
NASM syntax: mov eax, [rel x]
, or use default rel
so [x]
is RIP-relative.
请参见 Mach-O 64位格式不支持32位绝对地址. NASM Accessing Array (NASM访问数组),了解有关您在OS X上可以执行的操作的更多背景信息,例如movabs x, %eax
是可能的,因为目标寄存器是AL/AX/EAX/RAX. (64位绝对地址,但不要这样做,因为它比RIP相对负载大且不快.)
See Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array for more background on what you can do on OS X, e.g. movabs x, %eax
would be possible because the destination register is AL/AX/EAX/RAX. (64-bit absolute address, but don't do that because it's larger and not faster than a RIP-relative load.)
另请参见 http://felixcloutier.com/x86/MOV.html .
这篇关于无法将.data中的变量移动到Mac x86 Assembly中进行注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!