i'm beginning assembly, i'm using nasm for assembling the code, i'm trying to process a string residing in memory and change it, i want to check if a byte is in a certain range (ascii) so i can decide what 开发者_开发问答to do with it, i can't seem to figure how to check if a value is in a certain range, i know all about the different kind of jump flags but how can i combine 2 cmp statements ?
my question is : how do i produce something similiar to this in assembly ?
if (x>=20 && x<=100)
do something
thanks alot !
There is a way to express a range check like this using only a single conditional jump:
sub eax, 20
cmp eax, 80
ja END
// do something
END: ret
This is a very common optimization trick when working with integer ranges. The initial subtract maps the range [20,100] to [0,80]; membership in that range is then be checked with a single unsigned comparison.
Note also that the same thing can be done in C:
unsigned int upperBound = 100;
unsigned int lowerBound = 20;
if (yourValue - lowerBound <= upperBound - lowerBound) {
// do something
}
Depending on what syntax you're using, and assuming x
is in the eax
register, something like this:
cmp eax, 20
jl ELSE
cmp eax, 100
jg ELSE
#do something
jmp END
ELSE:
#do else
END:
You can try compiling it from a higher level language (C/C++/ ...) with optimalisations at high (-O3 for gcc), and have a look at what the compiler generates (objdump). It should generate very efficient code.
Like that?
x<20
if false jump to ELSE
x>100
if false jump to ELSE
do something
jump to ENDIF
:ELSE
do something else
:ENDIF
Or do you mean using only one assembly instruction?
精彩评论