1

I saw the following assembly code in a compiled program:

orl %eax, %eax

Does it do anything? As far as I can tell, it has no effect. Any bits that were 0 are turned into 0 | 0 = 0, and any bits that were 1 are turned into 1 | 1 = 1.

Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60
  • 3
    It might change the condition flags. Of course, if that were the intent, there's `test %eax, %eax` for that which would be more idiomatic. – Daniel Schepler Apr 18 '17 at 22:58
  • Reading the documentation, I couldn't find out in what ways the condition flags are set by `orl`, apart from the vague "The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to the result. The state of the AF flag is undefined." – Andres Riofrio Apr 18 '17 at 23:01
  • Oh, I see. The flags have defined behavior. Like ZF is only 1 if the result of an operation is all zeros. – Andres Riofrio Apr 18 '17 at 23:02
  • Yes, what Daniel said: you use it to set flags. See the code that immediately follows it. However, I can't think of any advantage that this would have over `test %eax, %eax`. In the best case, it would be equivalent. In many cases, I can see it being less optimal. Was this code generated by a compiler, or was it something someone hand-wrote in assembly? Where did the code come from? Perhaps that would give us some idea as to what compiler might have been used, or even the date (which would suggest which micro-architecture was being targeted). – Cody Gray - on strike Apr 19 '17 at 11:58
  • As I mentioned, this comes from a compiled program. Indeed, the next operation is `je`, so this explanation seems correct. – Andres Riofrio Apr 19 '17 at 18:23

0 Answers0