How many ways to set a register to zero?

前端 未结 8 1076
有刺的猬
有刺的猬 2020-12-02 17:00

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.

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 17:58

    This thread is old but a few other examples. Simple ones:

    xor eax,eax
    
    sub eax,eax
    
    and eax,0
    
    lea eax,[0] ; it doesn't look "natural" in the binary
    

    More complex combinations:

    ; flip all those 1111... bits to 0000
    or  eax,-1  ;  eax = 0FFFFFFFFh
    not eax     ; ~eax = 0
    
    ; XOR EAX,-1 works the same as NOT EAX instruction in this case, flipping 1 bits to 0
    or  eax,-1  ;  eax = 0FFFFFFFFh
    xor eax,-1  ; ~eax = 0
    
    ; -1 + 1 = 0
    or  eax,-1 ;  eax = 0FFFFFFFFh or signed int = -1
    inc eax    ;++eax = 0
    

提交回复
热议问题