I'm writing a bubblesort program in x86 and I'm getting the error:
error A2070: invalid instruction operands
during assembling. It happens at the two lines marked with ; HERE in the code.
.386
.model flat, stdcall
option casemap :none
.data
tempSize dd 4h
arrayLen dd 05h
sortArray dd 12h, 123h, 0ABh, 1h, 12h
.code
start:
push arrayLen
push offset sortArray
call sort
jmp done
sort:
mov ebx, 0h
mov ecx, dword ptr [esp]
mov eax, dword ptr [esp+4]
mul tempSize
next:
cmp dword ptr [ecx], dword ptr [ecx+4] ; HERE
add ecx, 4h
mov ebx, 1h
jle next
mov edx, dword ptr[ecx-4h]
mov dword ptr [ecx-4h], dword ptr [ecx] ; HERE
mov dword ptr [ecx], edx
cmp ecx, eax
jl next
cmp ebx, 1h
je sort
ret 8h
done:
ret
end start
I've been looking at this code for an hour now and got very frustrated as I'm sure it's a trivial mistake that I can't see. I hope this isn't too trivial to be removed from SO, because I don't know where else to ask.
On the cmp line I'm trying to compare the values of subsequent elements of sortArray. ecx contains the pointer to the first element of sortArray. Then dword ptr [ecx] should contain the value of the first element. And cmp dword ptr [ecx], dword ptr [ecx+4] should compare to subsequent values. BUT it fails at assembling. Why?