Search code examples
assemblyaddressing-mode

The addressing mode can write like this?


I want use cmp instruction, Whether to set up the following syntax in assembly language? example:

cmp [temp + si],[temp + si+1]

Solution

  • No, you can't do (exactly) this. For almost any instruction on an Intel x86, one of the operands must be a register.

    There is an exception to that: cmpsb compares [ds:si] to [es:di] (or, in 32-bit mode, [ds:esi] to [es:edi], or in 64-bit mode, uses rsi and rdi).

    If you want to use si to point to both operands, however, you'll need to load some other register, then compare:

    mov al, [temp+si]
    cmp al, [temp+si+1]
    

    Another possibility is to load from [si], and increment si:

    lodsb          ;roughly equivalent to mov al, [si] / inc si
    cmp al, [si]
    

    Since this is only able to use si, not temp+si, you'd need to add those together before using this. This is a little shorter, but (at least theoretically) slower on most current processors. To regain the speed, you could also use the more primitive instructions:

    mov al, [si+temp]
    inc si
    cmp al, [si+temp]
    

    Or, since you're using +temp for both instructions, you might gain a little by doing that addition ahead of time:

    add si, temp
    mov al, [si]
    inc si
    cmp al, [si]
    

    One thing to keep in mind though: since you're working with data in memory, this may not make any real difference in speed -- if you do much with memory, the bandwidth to memory is often (usually?) the bottleneck, so optimizing execution of the instructions on the processor itself makes little difference.