0

Using nasm with 32bit assembler code yields an unexpected result for my current attempts to write a loop which basically swaps elements of a vector. Given that ESI and EDI point to two different vectors (storing double values) and ECX the number n describing to swap the first n elements from [ESI] with the first n elements from [EDI] my attempts so far look like this:

; c-call: dswap(int n, double* dx, int unused1, double* dy, int unused2)
_dswap:
    push ebp
    mov ebp, esp
    pusha
    mov esi, [ebp+12] ; dx
    mov edi, [ebp+20] ; dy
    mov ecx, [ebp+8]  ; n
    ; unused1 and unused2 obviously unused for the 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 am getting some unexpected behavior. Calling dswap on (1,2,3) with (4,5,6) and n=3 only swaps the first two elements in both vectors thus making me think what I did wrong here.

2 Answers 2

2

You are dealing with doubles, but you are only multiplying ecx by 4 (the size of a float). Since the size of a double is 8, you should multiply by 8.

Another problem is that you do not decrement ecx before you multiply by it. Assuming n is passed as 3, you will be swapping dx[3] with dy[3] on the first iteration, but that is beyond the ends of the arrays. To fix this, you can decrement ecx before you do the swaps.

Sign up to request clarification or add additional context in comments.

5 Comments

After applying your proposed changes my vectors aren't modified at all. They remain unchanged now.
Since a double is 8 bytes you need 4 32-bit swaps now. 2 is not sufficient.
Okay that will take my some time to figure out how to implement this.
Any particular reason you are writing it in Assembly? If it is because of speed, then it is likely that your version is less efficient than what a compiler would generate.
Our assignment specifically states that this function should be implemented in Assembler. And thanks for the hint with the additional swaps - it works now as intended.
0

Double precision values are usually stored in 8-bytes cells. In your loop youe are assuming a 4-bytes cell, that's why only half of array is swapped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.