0

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?

Jan
  • 25
  • 6
  • Neither `cmp` nor `mov` takes two memory operands. Load one into a register. Next time consult instruction set reference to check allowed operands. – Jester Mar 21 '22 at 15:43
  • 1
    Yes, many things we would like to do cannot be encoded in one instruction in the machine code language; there are limits on what the hardware has to handle and they are specified by the instruction set architecture. Unlike C, machine code does not support expressions of arbitrary complexity. So, it is assembly programmer's job that if it cannot be done in one instruction, then to use a sequence of instructions. – Erik Eidt Mar 21 '22 at 15:50

0 Answers0