Hi all - I'm a first-timer here.... :) 
I'm learning NASM (on Linux). I don't have too many problems with most of the simple commands (mov, add, and so on) but I do have two questions -
a) What do the commands "and", "or" and "xor" do? 
b) Can someone give a simple example (for each) - an example of when these commands would be useful? 
Many thanks in advance - bye for now -
- obsidian   
Posted on 2012-09-04 00:32:32 by obsidian
They are 'binary number operators'... just more math operators, but they do something different, they are specifically for playing with the binary representation of numbers, not the integer, hex,  or other representation.
In asm, we often use 'bit flags', where we can store 8 flags in a single byte.
So try to think in those terms instead of thinking about the number values...

Let's assume we have two input numbers A and B, and we want to perform a binary operation on them..

OR is easiest to explain - each BIT in the result, will be 1, if the corresponding bit in A is 1, OR, if the corresponding BIT in B is 1 ... the only way to make an output bit to be zero, is if BOTH the inputs are zero. So this operator typically 'turns on' bits, because it likes to make a 1 in the results.

OR  10101100
=    11101110

AND is similar, the result will be zero, unless A is 1 AND B is 1 - they have to both be 1, to make a 1 in the result. So this guy turns OFF bits, because it likes to make a zero in the result.

AND  10001000
=      10000000

XOR is also known as EXCLUSIVE OR.
This one is a bit weird, but very useful
If A and B are the same, the result will be 0, if they are different the result will be 1. So this guy likes to FLIPS bits, useful for cheap encryption - if we said that encrypted value C = A XOR B, then we can also say that decrypted value A = B XOR C, and also, decrypted value B = A XOR C :)

        10001000    A
XOR  10011001    B
=      00010001    C

        00010001  C
XOR  10011001  B
=      10001000  A

Please also look up the term 'Truth Table', which will show you even more binary operators, some don't exist in the computing world, such as NOR and NAND.

Posted on 2012-09-04 02:17:26 by Homer
Hi Homer - thanks very much for that, that's very helpful! 

Ok - now the only thing I am wondering about is trying to think of simple examples when these would be useful. In other words -
*  "if you want to do "x", then "and" is useful."
*  "if you want to do "y", then "or" is useful." 
Many thanks! 
- obsidian 
Posted on 2012-09-04 02:52:59 by obsidian
You could use this to make a lights display using your parallel port, some leds, and some 330 ohm resistors, one each
Posted on 2012-09-04 04:47:14 by Homer

You could use this to make a lights display using your parallel port, some leds, and some 330 ohm resistors, one each

Ahh.  Okay then.... Thanks very much for that......  :) 
At the risk of prompting another "Home Improvement"-style reply..... ;) 
What I was meaning was more like - why would you want to apply the "or" operation to a number? What kind of simple "use case" does it help with? 
As a dopey newbie, I can't think of *any* practical, useful reason for "and" (or the other two operators that I mentioned).
All three of them seem like "solutions in search of a problem", as it were.....
- obsidian
Posted on 2012-09-04 05:05:34 by obsidian
If we define some BIT FLAGS (who mean something, for our work at hand)


FLAG1 equ 1
FLAG2 equ 2
FLAG3 equ 4
FLAG4 equ 8
FLAG5 equ 16
FLAG6 equ 32
FLAG7 equ 64
FLAG8 equ 128

Imagine we wanted to store eight BOOLEAN states - true or false.
We can store any or all of the above possible FLAGS in a single byte, by using the OR operator to combine them (which is quite similar to ADD you will notice).

The AND operator can be used to check if one or more flags are turned on.

The following example 32bit code could handle 32 boolean flags at once.

What if we called them something useful? ;)

mov eax, flags
mov edx, FLAG8
or edx, FLAG6
and eax, edx
jz TheyAreNotBothEnabled;

or in c speak
if( flags & (FLAG8 | FLAG6) ==0)
          ^and        ^ or
Posted on 2012-09-05 03:14:28 by Homer

mov eax, flags
mov edx, FLAG8
or edx, FLAG6
and eax, edx
jz TheyAreNotBothEnabled;

Nah... These flags are constants, so you want to or them into a single immediate operand, like this:
mov eax, flags
and eax, (FLAG8 or FLAG6)
jz TheyAreNotBothEnabled;

Or even:
test flags, (FLAG8 or FLAG6)
jz TheyAreNotBothEnabled;

Test performs an and operation and updates the flags, but it does not store the result (much like how cmp performs a sub without storing the result). This means that generally you want to use test instead of and for this sort of code, as the result of the operation is not important, only the flags.
In this case we can use the 'flags' variable directly from memory, since the test instruction will not modify the contents.
Posted on 2012-09-05 03:18:30 by Scali
ok so imagine this example

if (the player is not dead)
(the player has a gun)
(the player has a sharp stick)
(the player has a guitar string)
(there is a zombie nearby)
(the player is facing the right way)
then (target the zombie)

We can eliminate most of this code using a well-crafted single test with boolean flags. One operation, many questions answered.
But only if they are 'yes or no' questions.

There you go.
Posted on 2012-09-05 03:27:16 by Homer
Hi again all - thanks for your replies!
Good stuff!  This now gives me a much better idea of when "and", "or" and "xor" could be useful. Many thanks! 
Bye for now -
- obsidian 
Posted on 2012-09-05 03:33:46 by obsidian