Assembly Flags We've gone over the logical hodgepodge, so now we finally get to look at the assembly flags. Remember that these are not TSC flags! They are similar in function, however. All the ASM flags are 1-bit values stored in a special register. As mentioned in a much earlier lesson, that register is called the FLAGS. (wow, what originality!) Each bit of the 16-bit FLAGS register represents a flag, but some of them are unused. Here are 4 important ones: Z flag = Zero flag S flag = Sign flag C flag = Carry flag O flag = Overflow flag So, how are these flags "set"? Well, they are essentially set and reset after almost every mathematical instruction (including logic instructions), as well as CMP and other compare instructions:
In reality, all of the conditional JMP commands are really just "jump if flag is set" or "jump if flag isn't set". CMP A,B = Affects the flags in the same way as SUB A,B, but it doesn't actually perform the subtraction. JE X = Jump to address X if flag Z is set. Same as JZ (jump if zero). JNE X = Jump to address X if flag Z is cleared. Same as JNZ (jump if not zero). JGE X = Jump to X if flag S and flag O are the same value. Same as JNL (jump if not less). JG X = Jump to X if flag S and flag O are the same value, and flag Z is not set. Same as JNLE (jump if not less or equal). JL X = Jump to X if flag S and flag O are different values. Same as JNGE (jump if not greater or equal). JLE X = Jump to X if flag S and flag O are different values, or if flag Z is set. Same as JNG (jump if not greater). JA X = Jump to address X if flag Z and flag C are cleared. Same as JNBE (jump if not below or equal). JB X = Jump to address X if flag C is set. Same as JC (jump carry) or JNAE (jump if not above or equal). JBE X = Jump to address X if flag C or flag Z is set. Same as JNA (jump if not above). JNB X = Jump to address X if flag C is cleared. Same as JNC (jump not carry) or JAE (jump if above or equal). Some Examples Now, I know my explanations for the overflow and carry flags were confusing, but I hope some examples will help. MOV EAX, 244 CMP EAX, 244 JE 490A00 NOPNow, will the above code jump to 490A00 or will it execute the NOP directly after JMP 490A00 ?You should be saying, "Duh. Of course it will jump to 490A00. Since EAX is guaranteed to hold 244, and 244 is equal to 244, the JE instruction will successfully perform a jump." But let's think of this in terms of flags:
MOV EAX, FFFFFFFF MOV ECX, 202 ADD EAX, ECX JC 49F1DCNow, this looks a bit insane. We are trying to use a conditional jump without using CMP! Oh noes! What will happen? But of course, we know what will happen, since we can check how the flags are set. The last math instruction was ADDing 202 to FFFFFFFF and storing the result to EAX. This will definitely set the carry flag: the result is too big to fit into EAX, and so it will just "wrap around" to 201. The JC instruction will successfully jump to 49F1DC because it sees the carry flag is set, so we're done. Previous Lesson: Logic Instructions Next Lesson: Equips and Collisions Table of Contents |