C ++内联汇编 [英] C++ to inline assembly

查看:97
本文介绍了C ++内联汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试时,我在B3和B4上遇到访问错误.我在哪里出错?
程序会编译,但不会在汇编代码上进行调试.

when debugging i get access error on B3 and B4.Where am i going wrong?
The programe compiles but does not debug on the assembly codes.

void tom::add(void* ptr)
{ 
	
int* B2 = (int *)ptr;
int* B3;
int* B4;
#If tomC++
for(j = 0; j < 16; j += 4)
{
		
/// 1st stage ADD.
int f0 = (int)(B2[j]+ B2[j+3]);
int f3 = (int)(B2[j]- B2[j+3]);
int f1 = (int)(B2[j+1] + B2[j+2]);
int f2 = (int)(B2[j+1] – B2[j+2]);

/// 2nd stage ADD.
B2[j]		= (short)(f0 + f1);
B2[j+2]	= (short)(f0 - f1);
B2[j+1]	= (short)(f2 + (f3 << 1));
B2[j+3]	= (short)(f3 - (f2 << 1));
			
}//end for j...
#else
__asm
{
mov eax,B2
mov ecx,B3
movq mm0, [eax]
movq mm1, [eax+8]
movq mm2, [eax+16]
movq mm3, [eax+24]
paddw mm0, mm3 // mm0 + mm3
paddw mm1, mm2 // mm1 + mm2
movq [ecx], mm0
movq [ecx+8], mm1
movq mm0, [eax]
movq mm1, [eax+8]
psubw mm1, mm2 // mm1 - mm2
psubw mm0, mm3 // mm0 - mm3
movq [ecx+16], mm1
movq [ecx+24], mm0
//
mov eax,B3
mov ecx,B4
movq mm0,[eax]
movq mm1,[eax+8]
movq mm2,[eax+16]
movq mm3,[eax+24]
paddw mm0, mm1// mm0 + mm1
psllw mm3,1 //mm3<<1
paddw mm2, mm3 // mm2 + mm3<<1
movq [ecx], mm0
movq [ecx+8], mm2
movq mm0,[eax]
movq mm1, [eax+8]
psubw mm0, mm1// mm0 - mm1
movq mm2, [eax+16]
movq mm3, [eax+24]
psllw mm2,1 //mm2<<1
psubw mm3, mm2 // mm3 - mm2<<1
movq [ecx+16], mm0
movq [ecx+24], mm3
emms
}
#endif
}

推荐答案

主要错误是您声明了两个指向整数的指针:int* B3int* B4,但从未分配有效的地址.给出了内存保护错误,因为您试图写入随机地址!

另一个问题是变量的对齐方式:要与SSE2操作码一起使用,您的变量应使用__declspec(aligned(16)):
声明
The main error is that you have declared two pointers to integer: int* B3 and int* B4 but you have never assigned them a valid address. The memory protection fault is given because you are trying to write to random addresses!

Another issue is the alignment of the variables: to be used with SSE2 opcodes, your variables should be declared with __declspec(aligned(16)):

__declspec(aligned(16)) int* B2 = (int *)ptr;
__declspec(aligned(16)) int* B3 = (int *)malloc(16);
__declspec(aligned(16)) int* B4 = (int *)malloc(16);


您不能在汇编作用域块中使用这些变量.只需将它们放在cpp部分中即可.我猜想混合cpp和程序集的构造出了问题.使装配零件仅是简单装配.

http://msdn.microsoft.com/en-us/library/5sds75we.aspx [ ^ ]

祝你好运!
You cannot use these variables in the assembly scope block. Simply put them in the cpp part. I guess the construction of mixing cpp and assembly is what''s going wrong. Make the assembly part simply assembly only.

http://msdn.microsoft.com/en-us/library/5sds75we.aspx[^]

Good luck!


这篇关于C ++内联汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆