我将一个内联汇编函数从Visual Studio 2013中转换为MASM汇编,但是在获取返回值时遇到了问题。
以下是C调用者和汇编函数原型:
extern "C" void AbsMax(__m128d* samples, int len, __m128d* pResult);
__m128d AbsMax(__m128d* samples, int len)
{
__m128d absMax = { 0, 0 };
AbsMax(samples, len, &absMax);
return absMax;
}
还有汇编函数:
.686 ;Target processor. Use instructions for Pentium class machines
.xmm
.model flat, c ;Use the flat memory model. Use C calling conventions
.code ;Indicates the start of a code segment.
AbsMax proc samples:PTR DWORD, len:DWORD, result:PTR XMMWORD
;; Load up registers. xmm0 is min, xmm1 is max. L is Ch0, H is Ch1.
mov ecx, [len]
shl ecx, 4
mov esi, [samples]
lea esi, [esi+ecx]
neg ecx
pxor xmm0, xmm0
pxor xmm1, xmm1
ALIGN 16
_loop:
movaps xmm2, [esi+ecx]
add ecx, 16
minpd xmm0, xmm2
maxpd xmm1, xmm2
jne _loop
;; Store larger of -min and max for each channel. xmm2 is -min.
pxor xmm2, xmm2
subpd xmm2, xmm0
maxpd xmm1, xmm2
movaps [result], xmm1 ; <=== access violation here
xor eax, eax
xor ebx, ebx
ret
AbsMax ENDP
END
据我了解,MASM的约定是,通常通过EAX寄存器返回返回值。然而,由于我试图返回一个128位的值,所以我认为使用out参数是正确的方法。正如您在汇编清单中看到的,分配out参数(
movaps [result]
)导致访问冲突(Access violation reading location 0x00000000)。我已经在调试器中验证了result的地址,它看起来很好。我做错了什么?
__m128d
被定义为具有适当对齐方式的__declspec,并且我在调试器中仔细检查了地址。 - jaket