Tuesday, 15 July 2014

c - Incorrect iteration of loop in Assembler -



c - Incorrect iteration of loop in Assembler -

using nasm 32bit assembler code yields unexpected result current attempts write loop swaps elements of vector. given esi , edi point 2 different vectors (storing double values) , ecx number n describing swap first n elements [esi] first n elements [edi] attempts far this:

; c-call: dswap(int n, double* dx, int unused1, double* dy, int unused2) _dswap: force ebp mov ebp, esp pusha mov esi, [ebp+12] ; dx mov edi, [ebp+20] ; dy mov ecx, [ebp+8] ; n ; unused1 , unused2 unused moment mainloop: cmp ecx, 0 je exitfunction mov eax, [esi + 4 * ecx] mov ebx, [edi + 4 * ecx] mov [esi + 4 * ecx], ebx mov [edi + 4 * ecx], eax dec ecx jmp mainloop exitfunction: popa mov esp, ebp pop ebp ret

i getting unexpected behavior. calling dswap on (1,2,3) (4,5,6) , n=3 swaps first 2 elements in both vectors making me think did wrong here.

you dealing doubles, multiplying ecx 4 (the size of float). since size of double 8, should multiply 8.

another problem not decrement ecx before multiply it. assuming n passed 3, swapping dx[3] dy[3] on first iteration, beyond ends of arrays. prepare this, can decrement ecx before swaps.

c arrays loops assembly nasm

No comments:

Post a Comment