Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

x86 - double condition checking in assembly

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...