Home > other >  Overflow flag set to 1 after ROL operation even though it should be undefined
Overflow flag set to 1 after ROL operation even though it should be undefined

Time:09-07

I'm learning 8086 assembly using emu8086, I was playing around with some rotate instructions, and as for my question I know that for more than 1 bit shifts/rotations, the behaviour of the overflow flag OF is undefined.

But when I tried the following code

MOV BH,72H

MOV CL,4

ROL BH,CL

The result is 27H in the BH register, and as for the flags the carry and overflow flag get set to 1, even though the OF shouldnt get set since it's undefined.

Can someone explain how and why the OF got set here please

CodePudding user response:

A flag being undefined means that it is not specified whether it'll be cleared or set. Your emulator decides to set the flag. Another x86 CPU might clear the flag. The behaviour might even vary depending on the operands used or be different each time the instruction is executed. Do not depend on the value of the overflow flag after a rol instruction.

CodePudding user response:

There is no value for "undefined", so as a single bit, it is going to be a 0 or 1.

It could be that it is unchanged from before this instruction is executed, or it could be that it is set depending on the value being rotated, or it could be that it is completely random.

Further, by definition, we cannot rely on any of these being the case, so a program that inspects the value after such operation would be considered to have a logic error (similar to uninitialized variable).

So, even if one has tested every possible input to rol and come to a conclusion about how it works today on that specific processor or simulator, a program should still heed the documentation that it is undefined rather than expecting this once observed behavior.

  • Related