\" operator on 32-bit integers. Logically, I mean. Without any knowledge of Assembly." />
开发者

How is the ">" operator implemented (on 32-bit integers)?

Let's say that the environment is x86.

How do compilers compile the ">" operator on 32-bit integers. Logically, I mean. Without any knowledge of Assembly.

Let's say that the high level language code is:

int32 x, y;
x = 123;
y = 456;
bool z;
z = x > y;

What does the compiler do for evaluating the expression x > y?

Does it perform something like (assuming that x and y are positive integers):

w = sign_of(x - y);
if (w == 0)
   // expression is 'false'
else if (w == 1)
   // expression is 'true'
else
   // expression is 'false'

Is there any referen开发者_运维技巧ce for such information?


Well, logically it's something like that. The thing is that "compare two integers" is generally a primitive operation on almost any machine, so from the standpoint of the machine if just does a compare. The exact details differ from architecture to architecture, but in general you'd have something like a CMP instruction that leaves a condition code or other such control signal for "less than" "equal" and "greater than". So the generated code would be something like

         LD A, X    ; value of x into register A
         LD B, y    ;
         CMP        ; compare reg A with reg B

at which point the result code has the answer. If you want to store the result, then you use the sort of conditional you have --

         JMPGE @FALSE ; goto label FALSE if >=
         STO   Z, 0x1 ; put a hex 1 into location Z
         JMP   @END   ; jump to the end
FALSE:   STO   Z, 0x0
END:


I'm fairly sure integer comparisons are implemented at the CPU level. At that level, you are usually looking at something like the the following logic:

x > y if y - x < 0

This looks like a recursive definition, but isn't since the second half can be determined by looking at the sign bit of the result.

To expand on this slightly, from a description of the CMP instruction in x86 which is what the compiler would compile a > operator to, but maps more directly to the <=> operator available in some languages.

Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.

The reference on SUB.

After the CMP instruction, the compiler then needs to do one or more conditional instructions, but this is how the actual comparison works.


Typically (y - x) & INT32_SIGN_BIT for microcode.

64-bit comparisons on 32-bit architectures tend to be more interesting as the 64-bit sign bit may not be recognized as a true value and you have to force the check, i.e. ((y - x) & INT64_SIGN_BIT) != 0.


I think I found a good reference:

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-2.html#HEADING2-244


All comparisons in x86 (and any architectures that use flags) are implemented by doing a temporary subtraction and then set some flags depending on the result. The CPU can then use those flags to decide whether to jump on any comparisons such as >, >=, =, <=, <

How is the ">" operator implemented (on 32-bit integers)?

For example to check if two values equal, it'll check if their difference is zero (ZF = 1). To check if a < b (unsigned) then it'll check if a - b needs a borrow (i.e. CF = 1). Below are some comparing conditions on x86:

JE      Jump if equal                           ZF = 1
JZ      Jump if zero

JNE     Jump if not equal                       ZF = 0
JNZ     Jump if not zero

JB      Jump if below               unsigned    CF = 1 
JNAE    Jump if not above or equal
JC      Jump if carry

JNB     Jump if not below           unsigned    CF = 0 
JAE     Jump if above or equal
JNC     Jump if not carry

JBE     Jump if below or equal      unsigned    CF = 1 or ZF = 1
JNA     Jump if not above

JA      Jump if above               unsigned    CF = 0 and ZF = 0
JNBE    Jump if not below or equal

You can all of see them here

Some architectures (such as MIPS) don't use flags but rather set a register to some specific value based on the comparison result from a comparer. In that case the comparer may just do a simple lexicographic comparison on the bits without any subtraction. That means they'll iterate from the most significant bit and check if any pair of bits differ, if yes then the value that has a one bit will be larger

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜