" operator implemented (for 32-bit integers)? Let's say the x86 environment. How compilers compile the ">" operator on 32-bit i...">

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

Let's say the x86 environment.

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

Assume a high-level language code:

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

What does the compiler do to evaluate the expression x > y?

This does something like (assuming 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 a link to such information?

+3
source share
5 answers

, . - :

x > y if y - x < 0

, , .

, CMP x86, > to, <=>, .

EFLAGS . , SUB.

SUB.

CMP , , .

+1

, , - . , " " , , . , - CMP, "", "" "". , -

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

. , , -

         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:
+4

(y - x) & INT32_SIGN_BIT .

64- 32- , , , 64- , , .. ((y - x) & INT64_SIGN_BIT) != 0.

0

x86 ( , ) , . , , - , >,> =, =, <=, <

x86 flag regsiter

, , , , (ZF = 1). , a <b ( ), , a - b ( CF = 1). 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

( MIPS) , - , . . , , - ; , , ,

0

Source: https://habr.com/ru/post/1781784/


All Articles