【发布时间】:2018-08-12 00:09:38
【问题描述】:
我正在处理Xeno Kovah's example in slide 18 of Intermediate Assembly。他正在将 Visual Studios 与 Intel Assembly 内联使用。我已经尝试将其适应 GCC,如下所示。我正在使用-masm=intel -fPIC进行编译
#include <stdio.h>
int main(){
unsigned int maxBasicCPUID;
char vendorString[13];
char * vendorStringPtr = (char *)vendorString; //Move the address into its own register
//because it makes the asm syntax easier
//First we will check whether we can even use CPUID
//Such a check is actually more complicated than it seems (OMITED FROM SLIDES)
__asm (
"mov edi, vendorStringPtr;" //Get the base address of the char[] into a register
"mov eax, 0;" //We're going to do CPUID with input of 0
"cpuid;" //As stated, the instruction doesn't have any operands
//Get back the results which are now stored in eax, ebx, ecx, edx
//and will have values as specified by the manual
"mov maxBasicCPUID, eax;"
"mov [edi], ebx;" //We order which register we put into which address
"mov [edi+4], edx;" //so that they all end up forming a human readable string
"mov [edi+8], ecx;"
);
vendorString[12] = 0;
printf("maxBasicCPUID = %#x, vendorString = %s\n", maxBasicCPUID, vendorString);
return 0xb45eba11;
}
我不确定我做错了什么,但我收到以下错误
/usr/bin/ld: /tmp/ccSapgOG.o: relocation R_X86_64_32S against undefined symbol `vendorStringPtr' 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
【问题讨论】:
-
使用 GCC,您需要使用 extended inline assembly 模板将数组和变量传递给程序集
-
您正在使用 32 位寄存器作为指针,但仍在为 x86-64 编译。但更重要的是,MSVC 风格的内联汇编与 GNU C 风格完全不同。 What is the difference between
__asmand__asm__?
标签: gcc assembly x86 inline-assembly