嵌入汇编操纵64位寄存器便携式C ++ [英] Embed assembler to manipulate 64-bit registers in portable C++
问题描述
我在C(嵌入C ++)的简单(但性能的关键)算法来处理数据缓冲区...算法自然使用64位大端寄存器值 - 我想优化这种使用汇编获得对进位标志和BSWAP直接访问,因此,避免操纵64位在时间值一个字节。
I have a simple (but performance critical) algorithm in C (embedded in C++) to manipulate a data buffer... the algorithm 'naturally' uses 64-bit big-endian register values - and I'd like to optimise this using assembler to gain direct access to the carry flag and BSWAP and, hence, avoid having to manipulate the 64-bit values one byte at a time.
我要解决是OS /编译器之间移植 - 微创支持GNU G ++和Visual C ++ - 分别Linux和Windows之间。对于这两个平台,很明显,我假设支持X86-64指令集的处理器。
I want the solution to be portable between OS/Compilers - minimally supporting GNU g++ and Visual C++ - and between Linux and Windows respectively. For both platforms, obviously, I'm assuming a processor that supports the x86-64 instruction set.
我发现这个文件有关MSVC / Windows的内联汇编,并通过谷歌几个片段,详细说明g ++的不兼容的语法。我承认,我可能需要在每一个方言分别实现此功能。我一直没能找到关于语法/设施足够详细的文档来解决这个发展。
I've found this document about inline assembler for MSVC/Windows, and several fragments via Google detailing an incompatible syntax for g++. I accept that I might need to implement this functionality separately in each dialect. I've not been able to find sufficiently detailed documentation on syntax/facilities to tackle this development.
我正在寻找的是明确的文件,详细说明提供给我的设施 - 都与MS和GNU工具集。虽然我在很多年前写了一些32位汇编,我生锈 - 我从一个简洁的文件,详细说明设施需装配水平中受益
What I'm looking for is clear documentation detailing the facilities available to me - both with MS and GNU tool sets. While I wrote some 32-bit assembler many years ago, I'm rusty - I'd benefit from a concise document detailing facilities are available at an assembly level.
一个更复杂的是,我想编译使用Visual C ++防爆preSS版2010的窗户......我承认,这是一个32位编译器 - 但是,我想知道,是有可能嵌入64位汇编成其可执行文件?我只关心在我打算手工code中的部分约64位性能。
A further complication is that I'd like to compile for windows using the Visual C++ Express Edition 2010... I recognise that this is a 32-bit compiler - but, I wondered, is it possible to embed 64-bit assembly into its executables? I only care about 64-bit performance in the section I plan to hand-code.
谁能提供任何指针(请原谅双关语)?
Can anyone offer any pointers (please pardon the pun...)?
推荐答案
只是为了给你摆在你道路上的障碍味道,这里是一个简单的内联汇编程序的功能,在两种方言。首先,用Borland C ++ Builder的版本(我觉得这个编译MSVC ++下太):
Just to give you a taste of the obstacles that lie in your path, here is a simple inline assembler function, in two dialects. First, the Borland C++ Builder version (I think this compiles under MSVC++ too):
int BNASM_AddScalar (DWORD* result, DWORD x)
{
int carry = 0 ;
__asm
{
mov ebx,result
xor eax,eax
mov ecx,x
add [ebx],ecx
adc carry,eax // Return the carry flag
}
return carry ;
}
现在的G ++版本:
int BNASM_AddScalar (DWORD* result, DWORD x)
{
int carry = 0 ;
asm volatile (
" addl %%ecx,(%%edx)\n"
" adcl $0,%%eax\n" // Return the carry flag
: "+a"(carry) // Output (and input): carry in eax
: "d"(result), "c"(x) // Input: result in edx and x in ecx
) ;
return carry ;
}
正如你所看到的,差异是主要的。还有就是他们周围没有办法。这些都是从我写了一个32位环境中的大整数运算库。
As you can see, the differences are major. And there is no way around them. These are from a large integer arithmetic library that I wrote for a 32-bit environment.
对于在32位可执行文件中嵌入64位指令,我认为这是禁止的。据我所知,32位可执行文件在32位模式下运行时,任何64位指令只产生一个陷阱。
As for embedding 64-bit instructions in a 32-bit executable, I think this is forbidden. As I understand it, a 32-bit executable runs in 32-bit mode, any 64-bit instruction just generates a trap.
这篇关于嵌入汇编操纵64位寄存器便携式C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!