Thread: Comparing Numbers

  1. #1
    MouseMat
    Guest

    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. #2
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    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. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    you sure it's a&b? if they're equal a&b is either a or b. wouldn't a ^ b fit better?

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

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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. #7
    dirkduck
    Guest
    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. #8
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    bloody forgot to login..."process" should be "processer" .

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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. #10
    dirkduck
    Guest
    Yeah, but how does jz/je read the flag to "decide" weather or not to jump?

  11. #11
    MouseMat
    Guest
    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. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing two numbers in M86k assembly
    By Memloop in forum Tech Board
    Replies: 4
    Last Post: 03-30-2009, 05:24 PM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM