6 ms·
What's XOR AX,AX of the 20th century?
by omginternets 7y ago
What's XOR AX,AX of the 20th century?
- fourthark 7y agoShorter (in assembled bytes) and faster than MOV AX,0 https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and https://stackoverflow.com/questions/33666617/what-is-the-bes...
- Doctor_Fegg 7y agoGoes back to the 8080, in fact.
- root670 7y agoFastest way to zero a register.
- csirob 7y agoWhat is a 'register'? /s :)
- nemo1618 7y agoxor'ing a value with itself produces 0, so XOR AX,AX is equivalent to "AX = 0". It's a common trick in assembly code because it's faster (and/or maybe more compact, can't remember) than the more obvious MOV AX,$0.
- deleted 7y ago[deleted]
- navaati 7y agoThis is assembly. This instruction is doing an boolean "Exclusive Or" of a register named "AX" (think of a register like a hardware variable) with itself into itself. This always leave the register containing 0, because 0 XOR 0 = 0 and 1 XOR 1 = 0. And for some weird reason, doing that was faster than just doing MOV AX, 0 (which is literally "Move 0 into AX", so "AX = 0" in more familiar syntax). Edit: Oh man, my fellow HN'ers, as I did, all jumped on the occasion to show off :)
- omginternets 7y agoAh! Wow, that is surprising. Why was it so much faster?
- jedimastert 7y agoThere's a bunch of overhead associated with reading a constant from the program and moving that to the register. Math operations, on the other hand, are very simple to do because it's baked right into the CPU, only requiring one instruction and a few cycles
- murderfs 7y agoNo, the reason is that `xor ax, ax` has a shorter encoding, because it only takes 3 bits to encode a register on 32-bit x86, and far more bits are required for a useful immediate move. It would be ridiculous to have a short encoding for `mov rN, #imm` where the immediate has to be in the range 0-7.
- tomtomtom777 7y ago> It would be ridiculous to have a short encoding for `mov rN, #imm` where the immediate has to be in the range 0-7. Such instruction didn't exist but I wouldn't call it ridiculous. It would actually be quite useful as setting a register to 3 is far more common than setting a register to 7911.
- murderfs 7y agoIf you're going to do something like that, you might as well steal from the register numbering bits instead. (many architectures have done this: Intel has lots of short-form encodings for "op {al, ax, eax, rax} #imm", and ARM has Thumb, which basically chops the register count in half for all instructions to shorten their encoding)
- vidarh 7y agoM68000 has a MOVEQ #imm, Dn Since its instructions are always multiples of 16bit though, the immediate value is 8 bit, so slightly more useful, but the most common use of it was certainly setting a register to 0
- jedimastert 7y agoThe op "XOR" the the "xor" operation between two things and stores the result in the first operand. "XOR"ing a number with itself always results in 0, which is then stored in the operand itself. This operation was almost always faster than moving a constant (like 0) into a register, because of all of the overhead of making the constant and reading and writing.