汇编新人,求汇编大神赐教!想把之前Windows 32位的C++程序升级成64位,但64位不再支持内联汇编,所以想把C++中的全部内联的汇编放到一个独立汇编文件中,然后导出函数给C++调用。本人熟悉C++,但汇编只是了解,大学时只学过单片机汇编,现在都忘记了,最近又大概学了下32和64位汇编,感觉要实现的个很费力,现在已经能导出一些简单的函数给C++调用,但想搞些复杂的就不是编译不过就是运行崩溃。像怎么把多个寄存器的值或多个计算结果传给C++用,如获取CPUID、循环位移等函数的实现。
extern "C"
{
void MY_CPUID(DWORD dwVEax, DWORD &dwDEax, DWORD &dwDEbx, DWORD &dwDEcx, DWORD &dwDEdx);
}
struct cpuinfo
{
DWORD eax;
DWORD ebx;
DWORD ecx;
DWORD edx;
};
cpuinfo cpuid(DWORD veax)
{
// 因为嵌入式的汇编代码不能识别 类成员变量
// 所以定义四个临时变量作为过渡
DWORD deax=0;
DWORD debx=0;
DWORD decx=0;
DWORD dedx=0;
__asm
{
mov eax, veax;//将输入参数移入eax
cpuid;//执行cpuid
mov deax, eax;//以下四行代码把寄存器中的变量存入临时变量
mov debx, ebx
mov decx, ecx
mov dedx, edx
}
cpuinfo info;
info.eax = deax; // 把临时变量中的内容放入类成员变量
info.ebx = debx;
info.ecx = decx;
info.edx = dedx;
return info;
}
PUBLIC MY_CPUID
MY_CPUID PROC
mov QWORD PTR [rsp+32], r9
mov QWORD PTR [rsp+24], r8
mov QWORD PTR [rsp+16], rdx
mov DWORD PTR [rsp+8], ecx
sub rsp, 40
mov eax, ecx;//将输入参数移入eax
cpuid;//执行cpuid
mov DWORD PTR [rdx], eax;//以下四行代码把寄存器中的变量存入临时变量
mov DWORD PTR [r8], ebx
mov DWORD PTR [r9], ecx
mov rax, QWORD PTR [rsp+32]
mov DWORD PTR [rax], edx
add rsp, 40
ret
MY_CPUID ENDP
bool encryption_rotate::encrypt( const char * in_buffer,unsigned in_len,const char ** out_buffer,unsigned &out_len,const void * key)
{
alloc_buffer(in_len);
unsigned long count = in_len/sizeof(unsigned long);
const unsigned long * in_buf = (const unsigned long *)in_buffer;
const unsigned long * out_buf= (const unsigned long *)_encrypt_buffer;
const unsigned char * key_buf= (const unsigned char *)((key_info*)key)->_key_data;
// C++没有循环移位指令,故用内联汇编实现,为了速度更快你可以加上双流水线处理
_asm
{
MOV EBX,0
MOV ECX,count
START:
JCXZ DONE ; for ( int i=count;i!=0;--i )
MOV EDX,in_buf ; value = *in_buf
MOV EAX,dword ptr[EDX]
MOV EDX,key_buf ; cl = key_buf[ebx%4]
XOR EAX,dword ptr [EDX] ; value ^= _key_data
PUSH ECX
MOV CL,byte ptr [EDX+EBX]
ROL EAX,CL ; value << cl
POP ECX
INC EBX
AND EBX,3H ; ebx = (ebx+1)%4
MOV EDX,out_buf ; *out_buf = value
MOV dword ptr [EDX],EAX
ADD in_buf,TYPE in_buf ; ++in_buf
ADD out_buf,TYPE out_buf ; ++out_buf
LOOP START
DONE:
};
memcpy((void *)out_buf,(void *)in_buf,in_len%sizeof(unsigned long));
out_len = in_len;
*out_buffer = _encrypt_buffer;
return true;
}