我很习惯 Intel 格式的内联汇编。有谁知道如何在下面的代码中将两条 AT&T 行转换为 Intel 格式?它基本上是将局部变量的地址加载到寄存器中。
int main(int argc, const char *argv[]){
float x1[256];
float x2[256];
for(int x=0; x<256; ++x){
x1[x] = x;
x2[x] = 0.5f;
}
asm("movq %0, %%rax"::"r"(&x1[0])); // how to convert to Intel format?
asm("movq %0, %%rbx"::"r"(&x2[0])); // how to convert to Intel format?
asm(".intel_syntax noprefix\n"
"mov rcx, 32\n"
"re:\n"
"vmovups ymm0, [rax]\n"
"vmovups ymm1, [rbx]\n"
"vaddps ymm0, ymm0, ymm1\n"
"vmovups [rax], ymm0\n"
"add rax, 32\n"
"add rbx, 32\n"
"loopnz re"
);
}
mov eax, [var_a]
具体来说,在 32 位模式下编译时允许使用加载堆栈上的局部变量。例如,
// a32.cpp
#include <stdint.h>
extern "C" void f(){
int32_t a=123;
asm(".intel_syntax noprefix\n"
"mov eax, [a]"
);
}
它编译得很好:
xuancong@ubuntu:~$ rm -f a32.so && g++-7 -mavx -fPIC -masm=intel -shared -o a32.so -m32 a32.cpp && ls -al a32.so
-rwxr-xr-x 1 501 dialout 6580 Aug 28 09:26 a32.so
但是,在 64 位模式下编译时不允许使用相同的语法:
// a64.cpp
#include <stdint.h>
extern "C" void f(){
int64_t a=123;
asm(".intel_syntax noprefix\n"
"mov rax, [a]"
);
}
它不编译:
xuancong@ubuntu:~$ rm -f a64.so && g++-7 -mavx -fPIC -masm=intel -shared -o a64.so -m64 a64.cpp && ls -al a64.so
/usr/bin/ld: /tmp/cclPNMoq.o: relocation R_X86_64_32S against undefined symbol `a' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
那么有什么方法可以在不使用的情况下完成这项工作,因为可以通过或不破坏其他寄存器input:output:clobber
直接访问简单的局部变量或函数参数?mov rax, [rsp+##]
mov rax, [rbp+##]