1. ## Comparing Numbers

This may sound a stupid question, but how does a computer actually know if two variables in memory hold the same value. E.g.
Code:
```int a=3, b=3;

if( a == b )
{
// do something.
}```
At a hardware level though, how does the processor know if the two digits are the same? Will the processor "AND" the two values in binary, and check them that way?

2. if a & b == 0
is the same as
if a == b

i think for greater than you see if the high bits are on and for less than you see fi th elow bits are on... something like that.

3. you sure it's a&b? if they're equal a&b is either a or b. wouldn't a ^ b fit better?

4. Right, so to subtract the two values, would the binary numbers pass through a 32 bit adder/subtractor circuit?

5. i think so. the CMP command is exactly like the SUB command, with this difference:
CMP: a-b
SUB: a -= b

almost all commands alter a set of flags in the intel architechture, one of them being the zero flag. so, effectively, the command (a-b) turns the zero flag on if a and b are equal.

you can do many shortcuts with the zero flag. when you look closely at the structure of C, you'll notice that much of its syntax was governed by its similarity to the equivalent assembly command.

a += b; AND ax, bx
if (a) ... OR ax,ax \ JZ else_branch \ ...
a++; INC ax

6. Originally posted by MouseMat
Right, so to subtract the two values, would the binary numbers pass through a 32 bit adder/subtractor circuit?
Dont know how the CPU actually implements it, but the net result, is the product of the subtraction is discarded, and the CPU's flags are altered to reflect the result (IE...zero flag, sign flag etc)...your code then takes the state of these flags to branch the code how you want it....

<EDIT>ygf beat me to it!</edit>

7. I've wondered how instructions like je and jne work. For cmp its simply subtracting and moving a value to a register, but then for instance "je" has to check the value in that register, and if its 0 it jumps or else it doesnt (or whatever the value is to jump...). How does the process handle that compare?

8. bloody forgot to login..."process" should be "processer" .

9. Originally posted by dirkduck
I've wondered how instructions like je and jne work. For cmp its simply subtracting and moving a value to a register, but then for instance "je" has to check the value in that register, and if its 0 it jumps or else it doesnt (or whatever the value is to jump...). How does the process handle that compare?
As I said, the product of the subtraction is lost....but the flags are set...

So say you compare 2 same values, the subtraction sets the zero flag as (x - x = 0). Therefore jz (jump zero)works, but this is the same as je (jump equal) as they both mean the same thing

10. Yeah, but how does jz/je read the flag to "decide" weather or not to jump?

11. Thanks for your replies. I think it's easy to forget sometimes and not fully appreciate what is actually happening behind the scenes of a program at a hardware level. It's amazing to me how the processor works, and should not be taken for granted.

12. Originally posted by dirkduck
Yeah, but how does jz/je read the flag to "decide" weather or not to jump?
The flags are held in a register.......each bit corresponds to a specific flag......these codes just read that register and find if a certain bit is set.......