Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 ASM: useless conditional jump?

I am looking at the following piece of x86 assembly code (Intel syntax):

movzx   eax, al
and     eax, 3
cmp     eax, 3
ja      loc_6BE9A0

In my understanding, this should equal something like this in C:

eax &= 0xFF;
eax &= 3;
if (eax > 3)
   loc_6BE9A0();

This does not seem to make much sense since this condition will never be true (because eax will never be greater than 3 if it got and-ed with 3 before). Am I missing something here or is this really just an unnecessary condition?

And also: the movzx eax, al should not be necessary either if it gets and-ed with 3 right after that, is it?

I am asking this because I am not so familiar with assembly language and so I am not entirely sure if I am missing something here.

like image 808
Kebberling Avatar asked Aug 01 '26 07:08

Kebberling


1 Answers

You're correct: the movzx is redundant given the following and. It might have been produced by a non-optimizing compiler.

And yes, if this code executes straight through then the ja jump will never be taken. However, the cmp/ja may not be completely useless if there is code somewhere else that jumps straight to the cmp (or even to the ja).

like image 175
Nate Eldredge Avatar answered Aug 02 '26 21:08

Nate Eldredge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!