2

If I have the following table:

Case 1:    x: 42           y: -15           (y-x) = -57 
Case 2:    x: -17          y: -17           (y-x) = 0
Case 3:    x: 0x7ffffffd   y: -67           (y-x) = 2147483584
Case 4:    x: 67           y: -0x7fffffffd  (y-x) = 2147483584

What would the condition code flags set (zero or one, per flag) for ZF SF OF and CF

when considering the instruction: cmp1 %eax %ecx if %eax contains x and %ecx contains y?

I understand that cmp1 ...,... is executed by: cmp1 SRC2,SRC1

which means: "sets condition codes of SRC1SRC2"

I understand that the flags represent:

OF = overflow (?)
ZF = zero flag i.e. zero...
CF = carry out from msb
SF - sign flag i.e. negative

For my four cases in the table, I believe the flags would be:

1) ZF = 0 SF = 1 CF = 0 OF = ?
2) ZF = 1 SF = 0 CF = 0 OF = ?
3) ZF = 0 SF = 0 CF = 1 OF = ?
4) ZF = 0 SF = 0 CF = 1 OF = ?

Am I correct? Please explain what CF and OF are and how to determine if either will be set TRUE, and correct any of my flawed understanding. Thank you.

  • Usage of `CF and `OF` is determined by whether you're interpreting your values as signed or unsigned. See also - http://stackoverflow.com/questions/19340167/x86-assembly-language-test-operation-and-its-effect-on-flags/19340287#19340287 – Leeor Nov 04 '13 at 22:34

1 Answers1

1

Carry overflow occurs when an arithmetic operation generates a carry that cannot fit into the register. So if you had 8-bit registers, and wanted to add 10000000 and 10000000 (unsigned):

 10000000
 10000000
 --------
100000000

This 1 is the carry from most significant bit, and thus sets CF = 1.

You might also want to check this other answer.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Oh man, thanks. That makes sense. Are %eax and %ecx 8-bit registers? –  Nov 04 '13 at 22:12
  • No, those are 32 bits. I just posted 8 bits so it's easier to read. – m0skit0 Nov 04 '13 at 22:14
  • Is a 32-bit register a DWORD (double-word)? A word is 16-bits, correct? Will you explain when the CF would be set? –  Nov 04 '13 at 22:19
  • DWORD is usually 32-bits in Intel context, yes. I already explained when the CF would be set in the answer :) – m0skit0 Nov 04 '13 at 22:28
  • I'm sorry, I meant to ask when the `OF` flag would be set! Thanks. –  Nov 04 '13 at 22:29
  • It's explained in detail in the linked answer. Quoting it: *"The OF (overflow flag) tells whether a carry flipped the sign of the most significant bit in the result so that it is different from the most significant bits of the arguments"*. So in the 8-bit example I set OF = 1 as well, because most significant bit of the result (0) differs from most significant bit of the arguments (1). OF only has a meaning when numbers are signed. – m0skit0 Nov 04 '13 at 22:34
  • So in my table, would cases 3 and 4 result of CF = 1 because the MSB is being pushed into the sign-bit, and OF = 1, since the sign of the answer changed from what the sign should have been if there weren't limitations on the min/max int of -2^31 through 2^31-1? –  Nov 04 '13 at 22:58