xchg, and , or ,not
home

.. XCHG swaps two values

Where do we store it? How big is it? Size mismatch?

XCHG exchanges the values of two operands. At least one operand must be a register. No immediate operands are permitted. var1 DW 1000h var2 DW 2000h --------------- xchg ax, bx ; exchange 16-bit regs xchg ah, al ; exchange 8-bit regs xchg var1, Cx ; exchange mem, reg xchg ax, bx ; exchange 32-bit regs NO! xchg var1,var2 ; error: two memory operands ===

.. AND , OR , XOR , NOT Instructions

AND   performs bitwise AND on each pair of bits in the 2 operands
===
   0  0  |  0
   0  1  |  0
   1  0  |  0
   1  1  |  1

   mov al, 1101_0110b
   mov bl, 0101_0100b
   AND al, bl
        -> 0101_0100b

OR performs bitwise OR on each pair of bits in the 2 operands == 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 1 mov al, 1101_0110b mov bl, 0101_0100b OR al, bl -> 1101_0110b
XOR performs bitwise XOR on each pair of bits in the 2 operands === 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0 mov al, 1101_0110b mov bl, 0101_0100b XOR al, bl -> 1000_0010b
NOT performs bitwise NOT on each bit in the operand === 0 | 1 1 | 0 mov al, 1101_0110b NOT al -> 0010_1001b

.. NEG - negate a value

NEG instruction
===============
   Negates - Takes 2's complement.
   Operand can be a register or memory operand.

    valB DB -1
    valW DW +32767
    ===============
    MOV al, valB ; AL = -1 (FF)
    NEG al       ; AL = +1 (01)
    NEG valW     ; valW = -32767
	  
	===========================
	MOV al, 1
	NEG al ; al now contains -1 (ff)

	MOV al, 255
	NEG al (takes 2's complement of 255 (also -1) ) -> 01h
	
	MOV al, 129
	NEG al (takes 2's complement of 129 (also -127) ) -> 7fh	



Suppose AX contains 32,768 (8000h) and we apply NEG to it. Will the result be valid? MOV ax, -32768 NEG ax

.. XORXORXOR swap

XOR XOR XOR
===========
Standard swapping algorithms require the use of a temporary storage variable. 
Using the XOR swap algorithm, however, no temporary storage is needed. 
The algorithm is as follows:

   X := X XOR Y
   Y := X XOR Y
   X := X XOR Y

  example:
  --------
   x = 1100, y = 0101
   X := X XOR Y  => x is 1001
   Y := X XOR Y  => y is 1100
   X := X XOR Y  => x is 0101

XOR swap algorithm