Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cmp

326 views
Skip to first unread message

yqyq22

unread,
Jul 6, 2011, 7:34:25 AM7/6/11
to
Hi all,
I would like to understand what means:
cmp eax, 0FFFFFFFFh

thanks in advance
luc

Frank Kotler

unread,
Jul 6, 2011, 7:42:10 AM7/6/11
to

Essentially, subtracts 0FFFFFFFFh from eax and sets flags, but doesn't
store the result in eax as "sub" would.

Best,
Frank

yqyq22

unread,
Jul 6, 2011, 8:05:37 AM7/6/11
to
On Jul 6, 1:42 pm, Frank Kotler <fbkot...@nospicedham.myfairpoint.net>
wrote:

Ok thanks.. a lot the last question is to understand this instruction
mov eax, fd[eax*4]
thanks a lot for your support
luc

Lasse Reichstein Nielsen

unread,
Jul 6, 2011, 12:38:58 PM7/6/11
to
yqyq22 <yqy...@nospicedham.hotmail.com> writes:

> On Jul 6, 1:42 pm, Frank Kotler <fbkot...@nospicedham.myfairpoint.net>
> wrote:
>> yqyq22 wrote:
>> > Hi all,
>> > I would like to understand what means:
>> > cmp eax, 0FFFFFFFFh

...

> Ok thanks.. a lot the last question is to understand this instruction
> mov eax, fd[eax*4]

If "fd" refers to the memory location of an array of integers (or
pointers, or some other 32-bit values), then
fd[eax*4]
uses the value of eax as an index into that array and
mov eax, fx[eax*4]
loads the value at that index into eax (overwriting the index).

In that context, the
cmp eax, 0FFFFFFFFh
effectively compares the value in eax to -1, probably to avoid
reading a value before the start of the array.


/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Terje Mathisen

unread,
Jul 6, 2011, 12:58:26 PM7/6/11
to

As you say, CMP is the same as SUB, but only saving the flags, not
writing the result back to the register.

CMP/SUB EAX,0ffffffffh

is in fact the same as

CMP/SUB EAX,-1

which would give the same (register) result as

ADD EAX,1

but with different flags:

CMP EAX,-1

will (of course) set the zero flag only if EAX was -1, but the carry
flag will be set for all inputs except -1.

Doing ADD EAX,1 instead will set carry for the opposite condition, i.e.
clear for all values except -1.

It is most often seen in "trick" code that needs to propagate the carry
flag, or if you want an incrementing counter that saturates at a full
count, i.e. doesn't wrap around from 0xfff... to 0:

sub eax,-1 ;; Increment the counter
sbb eax,0 ;; Decrement if the previous operation wrapped around.

Terje
--
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

hopcode

unread,
Jul 6, 2011, 5:06:47 PM7/6/11
to
Il 06.07.2011 18:58, Terje Mathisen ha scritto:
> It is most often seen in "trick" code that needs to propagate the carry
> flag, or if you want an incrementing counter that saturates at a full
> count, i.e. doesn't wrap around from 0xfff... to 0:
>
> sub eax,-1 ;; Increment the counter
> sbb eax,0 ;; Decrement if the previous operation wrapped around.

did i read it in 32/64bit code !?
anyway, i use some similiar method for utf8 validation
to know how many units follow the prefix in the codepoint

shr al,4 ;--- in al first unit of the cpt (when >= C2h)
sub al,0Dh
adc al,1 ;--- now num required units

because C2->DF share 1 only following unit
while E0->EF 2
and F0->F4 3

and this is one of the many interesting
utf-8 "properties"... that helps avoiding
branching on "cmp".

Cheers,
--

.:hopcode[marc:rainer:kranz]:.
x64 Assembly Lab
http://sites.google.com/site/x64lab

yqyq22

unread,
Jul 7, 2011, 2:51:18 AM7/7/11
to
On Jul 6, 11:06 pm, hopcode <hopc...@nospicedham.nullnichtsnada.com>
wrote:

thanks to all for the useful explanaton.
Luc

0 new messages