assemblyx86x86-16tasm

How many ways to set a register to zero?


I'm curious how many ways are there to set a register to zero in x86 assembly. Using one instruction. Someone told me that he managed to find at least 10 ways to do it.

The ones I can think of are:

xor ax,ax
mov ax, 0
and ax, 0

Solution

  • There are a lot of possibility how to mov 0 in to ax under IA32...

        lea eax, [0]
        mov eax, 0FFFF0000h         //All constants form 0..0FFFFh << 16
        shr  ax, 16                 //All constants form 16..31
        shl eax, 16                 //All constants form 16..31
    

    And perhaps the most strange... :)

    @movzx:
        movzx eax, byte ptr[@movzx + 6]   //Because the last byte of this instruction is 0
    

    and also in 32-bit mode (longer instruction puts the final (most-significant) address byte later)...

      @movzx:
        movzx ax, byte ptr[@movzx + 7]
    

    Edit:

    And for 16 bit x86 cpu mode, not tested...:

        lea  ax, [0]
    

    and...

      @movzx:
        movzx ax, byte ptr cs:[@movzx + 7]   //Check if 7 is right offset
    

    The cs: prefix is optional in case that the ds segment register is not equal to cs segment register.