Note that there are some explanatory texts on larger screens.

plurals
  1. POx86 Assembly: INC and DEC instruction and overflow flag
    text
    copied!<p>In x86 assembly, the overflow flag is set when an <code>add</code> or <code>sub</code> operation on a signed integer overflows, and the carry flag is set when an operation on an unsigned integer overflows.</p> <p>However, when it comes to the <code>inc</code> and <code>dec</code> instructions, the situation seems to be somewhat different. According to this <a href="http://www.c-jump.com/CIS77/ASM/Flags/lecture.html" rel="noreferrer">website</a>, the <code>inc</code> instruction does not affect the carry flag at all.</p> <p>But I can't find any information about how <code>inc</code> and <code>dec</code> affect the overflow flag, if at all.</p> <p>Do <code>inc</code> or <code>dec</code> set the overflow flag when an integer overflow occurs? And is this behavior the same for both signed and unsigned integers?</p> <p>============================= <strong>EDIT</strong> =============================</p> <p>Okay, so essentially the consensus here is that INC and DEC should behave the same as ADD and SUB, in terms of setting flags, with the exception of the carry flag. This is also what it says in the Intel manual.</p> <p>The problem is I can't actually reproduce this behavior in practice, when it comes to unsigned integers.</p> <p>Consider the following assembly code (using GCC inline assembly to make it easier to print out results.)</p> <pre><code>int8_t ovf = 0; __asm__ ( "movb $-128, %%bh;" "decb %%bh;" "seto %b0;" : "=g"(ovf) : : "%bh" ); printf("Overflow flag: %d\n", ovf); </code></pre> <p>Here we decrement a signed 8-bit value of -128. Since -128 is the smallest possible value, an overflow is inevitable. As expected, this prints out: <code>Overflow flag: 1</code></p> <p>But when we do the same with an <em>unsigned</em> value, the behavior isn't as I expect:</p> <pre><code>int8_t ovf = 0; __asm__ ( "movb $255, %%bh;" "incb %%bh;" "seto %b0;" : "=g"(ovf) : : "%bh" ); printf("Overflow flag: %d\n", ovf); </code></pre> <p>Here I increment an unsigned 8-bit value of 255. Since 255 is the largest possible value, an overflow is inevitable. However, this prints out: <code>Overflow flag: 0</code>. </p> <p>Huh? Why didn't it set the overflow flag in this case?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload