Combination of AND and JGE in assembly

0

I have the following assembly lines which I do not understand exactly:

...
AND EDX, 0x80000003
JGE SHORT prog.00401304
...

Normally I have always seen the JGE instruction after CMP instruction. With a CMP I must look if the first operand is greater or equal than the second operand. But with and AND, I do not know. Can somebody tell me how I must interpret it with and AND instruction? Should I perform the AND operation on EDX with the value 0x80000003? And then? How it can look in a pseudo-C code language?

assembly
x86
asked on Stack Overflow Aug 30, 2014 by user3097712 • edited Aug 30, 2014 by nrz

3 Answers

2

and modifies flags in the following way (See Intel® 64 and IA-32 Architectures Software Developer’s Manual Combined Volumes:1, 2A, 2B, 2C, 3A, 3B and 3C):

Flags Affected
The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to
the result. The state of the AF flag is undefined.

jge means "Jump if greater or equal (SF=OF)", it's synonymous with jnl. See Intel x86 JUMP quick reference.

As OF (overflow flag) is always cleared (set to zero) after and, and jge jumps when (SF=OF), jge after and jumps when SF is set to zero, that is, when the highest bit of the result (here edx is set to zero), which means that the signed result is zero or positive integer (0..2147483647).

answered on Stack Overflow Aug 30, 2014 by nrz
0

You should have consulted the instruction set reference.

JGE operates based on flag bits, namely: Jump if greater or equal (SF=OF). Okay, now you need to figure out the value of those flags. You turn to the page describing the operation of the AND instruction and see: The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to the result. 0x80000003 has the highest bit set, thus after the AND operation SF gets the highest bit of EDX (also known as the sign bit). All in all, the branch is taken if the EDX >= 0, because then SF=OF=0.

answered on Stack Overflow Aug 30, 2014 by Jester
0

NRZ explained that OK. I will add that JGE in that code is equivalent to JNS. A small piece of C-code that produces these assembly instructions is:

test( ) {
    int i;
    i &= 0x80000003;
    if( i < 0 ) i = -i;
}

If you compile it with

cl /c /FAs test.c

the listing (part of it ) is :

; 2    :    int i;
; 3    :    i &= 0x80000003;
mov eax, DWORD PTR _i$[ebp]
and eax, -2147483645            ; 80000003H
mov DWORD PTR _i$[ebp], eax
; 4    :    if( i < 0 ) i = -i;
jge SHORT $LN2@test
mov ecx, DWORD PTR _i$[ebp]
neg ecx
mov DWORD PTR _i$[ebp], ecx

$LN2@test:

Keep in mind that the MOV instruction after AND does not affect flags.

I hope this helps.

answered on Stack Overflow Aug 31, 2014 by Tasos

User contributions licensed under CC BY-SA 3.0