News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Role of XOR

Started by parker, March 19, 2012, 06:02:19 AM

Previous topic - Next topic

parker

I don't fully undersdand the role of XOR BX,BX on this code.
                org 100h
mov dx, offset buffer
mov ah, 0ah
int 21h
jmp print
buffer db 10,?, 10 dup(' ')
print:
xor bx, bx
mov bl, buffer[1]
mov buffer[bx+2], '$'
mov dx, offset buffer + 2
mov ah, 9
int 21h
ret
I admire the effort of everyone who tend to share their wisdom without any kind in return. Thank you guyz...!

MichaelW

XORing a register with itself is one way of zeroing it. It came into common use originally because on the 8086/8088 it was slightly faster than:

MOV reg, 0
eschew obfuscation

dedndave

smaller, too   :P
31DB    xor     bx,bx
BB0000  mov     bx,0

even moreso with 32-bit code

by the way...
        sub     bx,bx
is another way to 0 the register - you'll see that from time-to-time, as well

mineiro

xor is a very flexible instruction.
You can do a 'not' using xor: "xor reg,0ffffh"
you can do a "not" only in specific bits: "xor reg,8080h"
you can do an "and" (sub) or "or" (add) if you know the values:
        xor ax,ax      ;zero it
        xor ax,1234h  ;add,or
        xor ax,1000h    ;sub,and
        xor ax,0ffffh   ;not
you can exchange values:
        xor al,al
        xor al,[si]
        xor al,[di]
        xor [si],al
        xor [di],al

FARMER-OAK

You can also use xor to change instructions in your program code.
For instance you can change "inc ax,1"  to "dec ax,1" by xoring with the proper value at the proper address

Example :xor byt ptr [address of where the dec instruction is],8
The second time this instruction is executed it will change from inc to dec
So the rythym would be dec, inc,dec,inc...
This type of recursive programming is fairly safe if you use even values for your loops and all loop values are multiples of each other.
Unless the program is interrupted it should still be the original when its done.
It makes for some interesting fun.
:8)

FORTRANS

Hi,

   XOR is the logical operation of eXclusive OR.  You can see
that operation as an even / odd indicator or a binary add with
no carries.  Bitwise, the XOR produces the following:

  1 XOR 1 =>  0
  1 XOR 0 =>  1
  0 XOR 1 =>  1
  0 XOR 0 =>  0

So something XORed with itself always produces zero as  MichaelW
said.  As Dave says, it is smaller than a MOV.  And back in the day
of the 8088, it was faster (only due to its being smaller).  Using XOR
rather than SUB is a matter of taste, probably due to it prompting
more questions from beginners.

Cheers,

Steve N.

Rockphorr

today intel recomends to use xor to set up to zero.
Strike while the iron is hot - Бей утюгом, пока он горячий