Update:
I tried this code just to cover all possible results.
Each multiplication of elements should be in range [-32768, 32767].
long DotProduct(short int* vec1, short int* vec2, int l)
{
long result;
_asm
{
mov esi, [vec1]
mov edi, [vec2]
mov ecx, [l]
xor eax, eax
loop_start:
mov dx, word ptr[esi]
imul dx, word ptr[edi]
movsx edx, dx
add eax, edx
add esi, 2
add edi, 2
sub ecx, 1
jnz loop_start
mov [result], eax
}
return result;
}
This instruction is invalid: imul edx, word ptr[edi]
imul r32, r/m16, no such thing in intel's manual.
For two-operand form only:
imul r16, r/m16
imul r32, r/m32
If changed to imul edx, dword ptr[edi] instruction is ok but now instead of 2 bytes it takes 4 bytes, so result will be incorrect.
mov dword ptr -14[ebp],eax ; 0018FE28 - vec1 address
mov dword ptr -10[ebp],edx ; 0018FE20 - vec2 address
mov dword ptr -0C[ebp],ebx ; l = 3
_asm
mov esi, dword ptr -14[ebp] ; esi = 0018FE28
mov edi, dword ptr -10[ebp] ; edi = 0018FE20
mov ecx, dword ptr -0C[ebp] ; ecx = 3
xor eax,eax ; eax = 0
00401043_loop: ; 3 | 2 | 1
movzx edx, word ptr [esi] ; edx = 00000001 | 00000002 | 00000003
imul edx, dword ptr [edi] ; edx = 00060005 | 000E000C | 00C00015
add eax, edx ; eax = 00060005 | 00140011 | 00D40026
add esi,00000002 ; esi = 0018FE2A | 0018FE2C | 0018FE2E
add edi,00000002 ; edi = 0018FE22 | 0018FE24 | 0018FE26
sub ecx,00000001 ; ecx = 2 | 1 | 0
jne 00401043
mov dword ptr -04[ebp],eax ; eax = 0x00D40026 = 13893670
If You clear higher word of eax result will be as expected 38
and eax,0ffffh
mov [result],eax
Better use 16-bit version, imul dx, word ptr[edi] and mov [result],eax after loop.
axwhen the function returns, is stored in the result field.imul edx, word ptr[edi]There is no instruction that encodes a multiplication ofedxby a 16-bit word value. Don't know what your assembler makes of this but it is certainly wrong.