Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c - why logical"NOT" the condition in if statement in assembly?

I'm learning assembly and a textbook shows an example of condition control:

enter image description here

I don't understand why x>=y is used in line 3, why not just follow the logic and use x<y (same as original C code)? is any particular reason to !(not) the condition in if statement?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

An if() statement means "skip this if the condition is not true".

If the condition is true, you want execution to fall-through into the if body, and if it's false you want execution to jump over the if body.

Thus, the obvious / literal way to compile an if is with a jcc on the inverse condition, like jnl. (Optimized code could certainly do much better for those highly-related if/else blocks, like subtract and conditional negate.)

If you wanted to use jl, you have to put the if-body out-of-line, maybe after the ret at the end of the function, and then jump back from it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...