+1  A: 

All CPUs have ALUs. I won't post Wikipedia links as you are good enough to find by yourself ;) but I can teach you a bit about CPU's internal.

What you basically need to know is that all CPUs have comparison instructions, ie BEQ "Branch-on-Equal" which is used to implement "if" instructions and jumps if A == B (there is another instruction for A < B, etc). When CPU reads this instruction and the operands, it loads them in the two ALU input buses and sets a proper ALU code that represents the comparison operation. The ALU is built to compare the numbers via hardware and output a proper result on its output side, or setting a status bit on the CPU.

Hardware comparison of numbers is easy, and almost all academic student have once designed a comparison (from 4 to 8 bits, but same logic applies to 32 and 64), you can find schematics anywhere

djechelon
I think I made a mistake in presenting my question. I meant the what is the implementation logic of instruction like CMP,GTE,LT.
RIPUNJAY TRIPATHI
The same. Operands are fired to ALU and the return value is set into status bits
djechelon
+5  A: 

Subtract B from A and see if the N (negative) flag or the MSb of the result is set.

Ignacio Vazquez-Abrams
You got my problem. What if A is negative and B is positive ?
RIPUNJAY TRIPATHI
Ignoring wraparound, a negative number minus a positive number is still a negative number. But registers are usually unsigned anyways; if signed operations are desired then the compiler/libc will have to do extra work to ensure the correct results.
Ignacio Vazquez-Abrams
+4  A: 

In x86 assembly, the CMP instruction is often used to compare two integers.

For instance, if you have integers in EAX and EBX respectively, and do CMP EAX,EBX, the values of the sign (S) flag and the zero (Z) flag will tell you which number was largest:

EAX > EBX:  Z=0, S=0
EAX == EBX: Z=1, S=0
EAX < EBX:  Z=0, S=1

The same result can be obtained by using the SUB instruction, which also stores the difference between the numbers in the destination register.

norheim.se
Addition: The `cmp` instruction is not alone, after that you need to use `j* label` commands that transfer control to the target if flags have some specified values. For example, `ja` (jump-if-above) after `cmp eax,ebx` makes jump if unsigned value of `eax` greater than unsigned value of `ebx`, `jg` (jump-if-greater) makes jump if signed value of `eax` greater than signed value of `ebx`, `je` (jump-if-equal) and `jz` (jump-if-zero) make jump if they are equal (these 2 commands have identical opcodes). There is also 'jae' (jump-if-above-or-equal), 'jna' (jump-if-not-above) and etc...
Ha
Right! I should have said something about jumping as well. +1 for pointing it out!
norheim.se