What is the difference between xor bx,bx and mov bx,0?

What would change if I replace <code>xor bx,bx</code> with <code>mov bx,0</code> in an asm code and disassemble it?
Posted on 2009-10-30 10:25:59 by arepo
Well, effectively they both do the same, namely they set the bx register to 0.
Semantically there's a difference though. Xor does a bitwise exclusive or operation on all bits of bx with itself. That's why the result happens to be 0.
Much like using sub bx, bx (somehow xor is more popular on x86, but in 68k circles, they often preferred the sub-variation). The result is 0, but the actual operation is not the same as mov or xor.

Aside from that, if I'm not mistaken, mov bx, 0 is one byte longer than xor bx, bx (or perhaps that was only in 32-bit?).
Posted on 2009-10-30 10:38:10 by Scali
Thank you, It was helpful...

Actually later I wrote mov bx,0 a file and in another xor bx,bx and disassembled both. Result are:

for mov bx,0

jna 0x24
bound di,
xor ,cl

for xor bx,bx

js 0x71
jc 0x24
bound di,
bound di,
Posted on 2009-10-30 10:47:19 by arepo
xor changes flags, mov doesn't.
It matters a weeny bit in optimization. The funky internal handling of "mov" as dereferencing/register-allocation can make it faster in real code. (in modern cpus, internally there are hundreds of registers, instead of just 8; necessary for pipelining/out-of-order-execution ).

Btw, that's some interesting way to display  6633DB  vs 66BB0000
Posted on 2009-10-30 13:14:48 by Ultrano
It was 16-bit disassembly of strings "mov bx,0" and "xor bx,bx". Now the question is: was that a joke?
Posted on 2009-10-30 13:52:11 by baldr
Your disassembly is totally pooched, for whatever reason.

31DB - xor bx, bx
B80000 - mov bx, 0

As you see, one byte shorter. xor alters flags, mov does not. If a "real assembly programmer" (TM) sees "mov", he wonders why you need to preserve flags.

For practical purposes, it makes no difference whatsoever - either will work fine.

If you want assistance with getting a "correct" disassembly, tell us what tools you're using, and exactly what you did. What I did was put the two instructions in one file, assembled it with Nasm's "-f bin" (flat binary output) and just "ndisasm myfile" (defaults are okay for this - usually ndisasm would require an extensive command line - it isn't very bright, but is *is* powerful). Someone can probably help with other tools. It looks to me like you might be disassembling the header of an object file, or something - the numbers don't match the instructions you want, at all!

Don't give up on this. Being able to disassemble code is a very useful skill!


Posted on 2009-10-30 14:30:34 by fbkotler
Thank you all for your answers...

i'm new at assembly so my questions might be a little bit stupid, but we have an assignment in assembly and i have to find some answers before the deadline... :)

after i read your answer i realized what i did was just disassebling strings "mov bx,0" and "xor bx,bx"
what i did was as Frank said "ndisasm myfile"...

on the other had since i checked my question last time i have found a simple hello world code in assembly and then i disassembled it...
There are lots of "add ,al " lines in the disassembled code...
What does that mean?

Again thank you all for your time...
Posted on 2009-10-31 05:29:06 by arepo
You'd better look at assembly source (there are tons of tutorial programs) than plain disassembly if you're not into reverse engineering.

add , al is 00 00 interpreted as 16-bit instruction opcode. ndisasm takes quite straitforward approach for distinguishing code and data (even in auto-sync mode).
Posted on 2009-10-31 06:22:39 by baldr