On this page

The result in a register is only half the answer. The other half is four status bits that tell the next instruction where to go.

Addition looks harmless as long as operands stay far from type boundaries. At the boundary, two distinct events appear: an unsigned carry out of the most significant bit, and a signed overflow. In this lesson, I run add, sub, cmp, test, and bit shifts through GDB, inspect RFLAGS, and see which bits each operation leaves behind.

Mental Model: Result, Flags, and Shifts

What it looks like in C99 What happens in hardware
a + b only changes a value The ALU writes the result and simultaneously updates ZF, SF, CF, and OF; the next instruction can branch on them
x << 4 is “hex magic” or appending a zero shl multiplies by 24=162^4 = 16: every left shift doubles the number
inc updates flags just like add inc and dec update ZF, SF, and OF, while leaving CF untouched
cmp and test store their result They compute a difference or bitwise AND, yet only write the flags

RFLAGS: Four Bits After the Result

RFLAGS doesn’t store the result itself—only its properties. The lower 16 bits are exposed as FLAGS and the lower 32 as EFLAGS, though the bits we care about are identical.

 upper 8 bits: 15 14 13 12 11 10  9  8
                0  0  0  0 OF DF IF  0
 lower 8 bits:  7  6  5  4  3  2  1  0
               SF ZF  0 AF  0 PF  1 CF

Four bits handle arithmetic:

  • ZF — the result is zero;
  • SF — copy of the result’s most significant bit, i.e., the sign bit for signed integers;
  • CF — carry out of the most significant bit on addition, or borrow on subtraction, for unsigned integers;
  • OF — indicates signed-range overflow.

In GDB output, housekeeping bits DF, PF, AF, and IF sit alongside these four; they aren’t relevant to the arithmetic in this lesson.

Under the hood: the remaining bits in eflags output
  • DF — direction flag for string operations: 0 walks from low to high addresses, 1 walks backward;
  • PF — parity of the result’s lowest byte;
  • AF — auxiliary carry out of the low nibble, used for binary-coded decimal (BCD) arithmetic;
  • IF — interrupt enable flag; managed by the kernel, not arithmetic instructions;
  • bit 1 is hardwired to 1 by the architecture, which is why 0x257 and other eflags values always include 0x200 (IF) and bit 1 (0x02).

For 0x7FFFFFFF + 1 in 32 bits, 0xa96 breaks down into those same bit weights:

0xa96=0x800 (OF)+0x200 (IF)+0x80 (SF)+0x10 (AF)+0x4 (PF)+0x2 (bit 1). \begin{aligned} \mathtt{0xa96} ={}& \mathtt{0x800}\ (\text{OF}) + \mathtt{0x200}\ (\text{IF}) + \mathtt{0x80}\ (\text{SF}) \\ &+ \mathtt{0x10}\ (\text{AF}) + \mathtt{0x4}\ (\text{PF}) + \mathtt{0x2}\ (\text{bit 1}). \end{aligned}

Here OF, IF, SF, AF, PF, and the hardwired 1 in bit 1 are set; ZF, CF, and DF are zero. That’s why 0x80000000 is treated as negative even though no carry occurred out of the top bit.

Every number in the eflags column is the sum of the weights of its set bits. For 0x257:

0x200+0x40+0x10+0x04+0x02+0x01=0x257 \mathtt{0x200} + \mathtt{0x40} + \mathtt{0x10} + \mathtt{0x04} + \mathtt{0x02} + \mathtt{0x01} = \mathtt{0x257}

That’s IF, ZF, AF, PF, the hardwired 1 in bit 1, and CF. 0xa96 and the other values read the exact same way: the table in the next section isn’t a list of arbitrary codes, just the sum of specific bits.

Mechanics: Two ALU Contracts

The arithmetic logic unit has two independent contracts, and this lesson rests on both. First, status flags: the result goes into a register while status bits update alongside it, guiding the next instruction’s decision. Second, width and masking: for bitwise operations, the behavior depends not just on the operands, but also on register width.

First I’ll walk through arithmetic and its exception (inc), then shifts, and finally how to inspect all of these bits in GDB.

Arithmetic: add, sub, cmp, test

The first contract is status flags. Our testbed runs eight scenarios back to back and leaves the flags after each one:

flags_lab.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; flags_lab.asm — ZF, SF, CF, and OF at 8/32/64-bit operand boundaries.

default rel

%define SYS_EXIT 60

section .text
    global _start

_start:
    mov eax, 0xFFFFFFFF
    add eax, 1

after_u32_wrap:
    mov eax, 0x7FFFFFFF
    add eax, 1

after_s32_overflow:
    mov rax, 0xFFFFFFFFFFFFFFFF
    add rax, 1

after_u64_wrap:
    mov al, 0x80
    add al, 0x80

after_u8_wrap:
    mov ebx, 5
    sub ebx, 5

after_sub_zero:
    mov eax, 100
    sub eax, 200

after_sub_borrow:
    mov eax, 0x2A
    cmp eax, 0x2A

after_cmp_equal:
    mov ebx, 0xF0
    test ebx, 0x0F

after_test_zero:
    ; syscall: exit(0)
    mov eax, SYS_EXIT
    xor edi, edi
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits

GDB at each label gives this picture:

Expression Result eflags Set flags
0xFFFFFFFF + 1 (32-bit) 0x0 0x257 CF, ZF
0x7FFFFFFF + 1 (32-bit) 0x80000000 0xa96 SF, OF
0xFFFFFFFFFFFFFFFF + 1 (64-bit) 0x0 0x257 CF, ZF
0x80 + 0x80 (8-bit) 0x00 0xa47 CF, ZF, OF
5 - 5 0x0 0x246 ZF
100 - 200 0xFFFFFF9C 0x297 CF, SF
cmp 0x2A, 0x2A not written 0x246 ZF
test 0xF0, 0x0F not written 0x246 ZF

In practice, you read flag state from GDB’s text decoding rather than from the raw hex number: in eflags 0xa96 [ PF AF SF IF OF ], the square brackets name the set bits directly, so you never have to sum weights by hand. That’s how you should read the info registers eflags output below.

The “Set flags” column lists only the bits relevant to this lesson’s arithmetic; housekeeping bits AF, PF, IF, and the hardwired bit 1—which also appear in eflags—are omitted from the table. Why 0x7FFFFFFF + 1 produces 0xa96 in hex is shown in the collapsible note above: for reading a program, [ SF OF ] is all you need, and summing bit weights is only for checking the raw integer.

  • Unsigned carry sets CF, not OF: 0xFFFFFFFF + 1 wraps to 0 with a carry.
  • Signed overflow sets OF, not CF: 0x7FFFFFFF + 1 turned negative.
  • The 8-bit sum 0x80 + 0x80 overflows a byte under both rules simultaneously.
  • Subtracting 100 - 200 requires a borrow: CF and SF are set, and the result 0xFFFFFF9C is -100 in 32-bit two’s complement.

The two overflow boundaries in the 32-bit ring sit at opposite points:

Unsigned boundary (CF=1, ZF=1):           Signed boundary (OF=1, SF=1):
  0xFFFFFFFF  ──(+1)──►  0x00000000         0x7FFFFFFF  ──(+1)──►  0x80000000
  (4294967295)           (carry)            (INT32_MAX)            (INT32_MIN)

For a negative value, its unsigned representation wraps around the 32-bit ring:

u=232+x,x<0 u = 2^{32} + x,\quad x < 0
where
uu
unsigned 32-bit value stored in the register.
xx
negative value seen by signed arithmetic.
2322^{32}
offset added by wrapping past zero in the 32-bit ring.

For x=−100x = -100, substituting into (3) yields:

232−100=4 294 967 196=0xFFFFFF9C 2^{32} - 100 = 4\,294\,967\,196 = \mathtt{0xFFFFFF9C}

That matches what GDB prints.

cmp a, b computes a - b and discards the result: only the flags change. test a, b computes a & b and likewise updates only the flags, always clearing CF and OF. In both cases, the operands stay intact: eax remains 0x2A after cmp, and ebx remains 0xF0 after test.

That wraps up the first contract for add, sub, cmp, and test. However, inc handles flags differently—let’s look at that exception next.

inc: Same Result, Different Flag Contract

Arithmetic has one symmetry-breaking exception: inc adds one and updates ZF, SF, and OF, yet leaves CF untouched. Whenever you need an unsigned carry, inc cannot replace add, even though the register result is identical.

Looking ahead: capturing RFLAGS whole
The pushfq instruction temporarily copies the entire 64-bit flags register onto the stack, and pop pulls it into a register. We’ll cover the stack and rsp in full in lesson 09; here, this pair acts as a quick silicon snapshot: pop r8 followed by and r8d, 1 isolates just the CF bit in a register.

Before the test, clc (clear carry flag) clears CF to zero without touching any other bits of RFLAGS. Without it, a stale CF from an earlier operation could taint the snapshot.

inc_cf.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; inc_cf.asm — inc leaves CF untouched, add updates it; both bits go into the exit code.
; pushfq/pop temporarily save RFLAGS on the stack and pop a snapshot into a register
; to read the CF bit; the stack is covered in lesson 09.

default rel

%define SYS_EXIT 60

section .text
    global _start

_start:
    ; clc clears CF to 0 before the test: otherwise a leftover flag
    ; from a prior operation could skew the snapshot.
    clc
    mov eax, 0xFFFFFFFF
    inc eax

after_inc:
    pushfq
    pop r8
    and r8d, 1

    clc
    mov ecx, 0xFFFFFFFF
    add ecx, 1

after_add:
    pushfq
    pop r9
    and r9d, 1

    shl r8d, 1
    or r8d, r9d

    ; syscall: exit(cf_after_inc * 2 + cf_after_add)
    mov eax, SYS_EXIT
    mov edi, r8d
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits

The exit code is 1, or 01 in binary:

$ ./build/inc_cf; echo $?
1
  • bit 1 of the mask is 0: CF stayed clear after inc;
  • bit 0 is 1: CF was set after add.

GDB confirms this by inspecting RFLAGS at both labels:

$ gdb -q --nx -batch build/inc_cf \
    -ex 'set debuginfod enabled off' \
    -ex 'break after_inc' -ex run -ex 'info registers eflags'
Breakpoint 1 at 0x401008: file inc_cf.asm, line 22.
Breakpoint 1, after_inc () at inc_cf.asm:22
22	    pushfq
eflags         0x256               [ PF AF ZF IF ]
$ gdb -q --nx -batch build/inc_cf \
    -ex 'set debuginfod enabled off' \
    -ex 'break after_add' -ex run -ex 'info registers eflags'
Breakpoint 1 at 0x401018: file inc_cf.asm, line 31.
Breakpoint 1, after_add () at inc_cf.asm:31
31	    pushfq
eflags         0x257               [ CF PF AF ZF IF ]

inc set ZF because the result became zero, yet left CF untouched. add across the same wrap-around set CF as well. That closes the first contract. The second contract (width and masking) shows up when shifting operands or packing bits into a byte.

Shifts, Immediates, and Masking

The second contract is width and masking. The shift count is given by an immediate, though not just any count works as written: the hardware masks the shift count to the operand’s width.

imm_trap.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; imm_trap.asm — shl by 4 multiplies by 16, and the shift count is masked by width.

default rel

%define SYS_EXIT 60

section .text
    global _start

_start:
    mov eax, 1
    shl eax, 4

after_shl4:
    mov ecx, 1
    shl ecx, 32

after_shl32:
    mov edx, 1
    shl rdx, 32

after_shl64:
    mov edi, eax

    ; syscall: exit(result_of_shl4)
    mov eax, SYS_EXIT
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits

Running it returns 16 (using the same teaching trick of returning a number via exit status as in lesson 04), because 1 << 4 multiplies by 16. Shifting left by nn multiplies by 2n2^n: each bit moves up to the next power’s weight.

  • 1 << 4 equals 1⋅24=161 \cdot 2^4 = 16;
  • 0x10 is simply compact hexadecimal notation for 16, not a separate “magic” rule;
  • in bitpack, 0xA << 4 | 0x7 gives 10⋅16+7=16710 \cdot 16 + 7 = 167, or 0xA7.
$ cd lessons/07-arithmetic-flags
$ make build
$ ./build/imm_trap; echo $?
16

objdump shows the shift-count byte, and GDB shows what happened at each step:

Instruction Bytes Value after Why
shl eax, 4 c1 e0 04 eax = 0x10 shift by 4
shl ecx, 32 c1 e1 20 ecx = 0x1 32 & 31 = 0, no shift
shl rdx, 32 48 c1 e2 20 rdx = 0x100000000 32 & 63 = 32, shifted

The instruction bytes have three parts:

  • c1 — opcode for a shift by an immediate;
  • e0, e1, e2 — byte encoding the register and operation;
  • the final byte — the shift count itself.

For a 32-bit operand, the CPU uses the lower 5 bits of the shift count; for a 64-bit operand, the lower 6 bits. That’s why shl ecx, 32 doesn’t zero ecx—it turns into a shift by zero. In mov edx, 1, writing to the 32-bit window clears the upper 32 bits of rdx (the zero-extension rule from lesson 03), so the subsequent shl rdx, 32 produces 0x100000000 rather than shifting garbage in the upper half.

Count 32 = 0x20 = 0b0010_0000:
  shl ecx, 32  (5 bits) : [0_0000] = 0   ──► no shift (ecx = 1)
  shl rdx, 32  (6 bits) : [10_0000] = 32 ──► shift by 32 (rdx = 0x100000000)

Packing in bitpack.asm:
  0x0A << 4 = 0xA0  ──(or 0x07)──►  [1010 | 0111] = 0xA7 (167)

Packing two fields into one byte demonstrates the same principle without magic:

bitpack.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; bitpack.asm — two 4-bit fields in one byte: packing and unpacking.

default rel

%define SYS_EXIT 60

section .text
    global _start

_start:
    mov eax, 0x0A
    mov ecx, 0x07
    shl eax, 4
    or eax, ecx

after_pack:
    mov r8d, eax
    mov edx, eax
    and edx, 0x0F
    shr eax, 4

after_unpack:
    ; syscall: exit(packed_byte)
    mov eax, SYS_EXIT
    mov edi, r8d
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits

./build/bitpack; echo $? prints 167, which is 0xA7. In GDB before unpacking, eax = 0xA7; afterward, eax = 0xA and edx = 0x7. The upper four bits moved up via a left shift by 4, while the lower four stayed behind under the 0x0F mask.

How to Inspect Flags in GDB

With both contracts covered, all that’s left is reading them in the debugger. Simply pause at a label and inspect the register:

$ gdb -q --nx -batch build/flags_lab \
    -ex 'set debuginfod enabled off' \
    -ex 'break after_u32_wrap' -ex run -ex 'info registers eflags'
Breakpoint 1 at 0x401008: file flags_lab.asm, line 17.
Breakpoint 1, after_u32_wrap () at flags_lab.asm:17
17	    mov eax, 0x7FFFFFFF
eflags         0x257               [ CF PF AF ZF IF ]

Inside the square brackets, GDB decodes the set bits, while p/x $eflags prints the raw hex value without decoding. Housekeeping bits PF, AF, and IF inside the brackets were already covered above.

You can capture the same snapshot without stopping at a label: pushfq and pop copy RFLAGS into a register, just like in inc_cf.asm. In GDB, though, info registers eflags is far more convenient: it shows both the hex value and the names of set bits.

Common Pitfalls

  1. "inc updates all flags just like add."
    Fact: after inc, eflags is 0x256 with no CF in the decoded list; after add, it’s 0x257 with CF.
    Cause: inc and dec update ZF, SF, and OF, while leaving CF unchanged.
    Fix: use add/sub when you need unsigned carry detection, or capture CF separately before and after.

  2. "shl ecx, 32 zeros a 32-bit register."
    Fact: objdump shows count 20, and GDB shows ecx = 0x1 after the instruction.
    Cause: for a 32-bit operand, the hardware takes only the lowest 5 bits of the count, so 32 & 31 = 0.
    Fix: use a 64-bit operand (shl rdx, 32) for shifts of 32 or more, or split the shift into two steps.

  3. "CF and OF are the same thing."
    Fact: 0x7FFFFFFF + 1 leaves eflags at 0xa96 without CF but with OF; 0xFFFFFFFF + 1 leaves 0x257 with CF but without OF.
    Cause: CF signals unsigned carry, while OF signals signed-range overflow.
    Fix: pick the right flag for your comparison: ja/jb check CF, while jg/jl check SF and OF (conditional jumps are covered in detail in lesson 08).

  4. "cmp and test write the result back to the operand."
    Fact: after cmp eax, 0x2A, eax stays 0x2A; after test ebx, 0x0F, ebx stays 0xF0; only eflags changes.
    Cause: both instructions compute a result without storing it: cmp subtracts, while test performs bitwise AND.
    Fix: read the outcome from the flags rather than the register; use sub or and if you need the actual result.

Lab

Goal. Build flags_mask.asm to run three specified scenarios and exit with a bitmask built from the flags of the final scenario.

Contract and requirements.

  • scenario A: 0xFFFFFFFF + 1 in a 32-bit register;
  • scenario B: 100 - 200 in a 32-bit register;
  • scenario C: 0x40000000 + 0x40000000 in a 32-bit register;
  • pause at a dedicated label after each scenario;
  • exit status is a mask of the final scenario: bit 0 = CF, bit 1 = ZF, bit 2 = SF, bit 3 = OF;
  • nothing is printed to standard output.

Verification criteria.

  1. ./flags_mask; echo $? prints 12.
  2. GDB at the label after scenario A shows eflags with CF and ZF.
  3. At the label after scenario B, CF and SF are set.
  4. At the label after scenario C, SF and OF are set, while CF is clear.
  5. Modifying one operand changes the mask in the exact bits described by that scenario.
Expected flags, mask, and walkthrough

RFLAGS state at the three labels:

Scenario eflags Set flags Mask contribution
A: 0xFFFFFFFF + 1 0x257 CF, ZF 1 + 2 = 3
B: 100 - 200 0x297 CF, SF 1 + 4 = 5
C: 0x40000000 + 0x40000000 0xa86 SF, OF 4 + 8 = 12

After scenario C, adding two positive numbers produced a negative result. The high bit of 0x80000000 set SF, overflowing int32_t set OF, and there was no carry out of the top bit, so CF is zero.

A snapshot of RFLAGS is captured with pushfq, and the target bits are isolated with masks:

  • CF — bit 0;
  • ZF — bit 6;
  • SF — bit 7;
  • OF — bit 11.

To pack these flags into bits 0..3 of the exit status (capped at 8 bits), shift their bit positions right: CF stays at bit 0, ZF shifts right by 5 (into bit 1), SF by 5 (into bit 2), and OF by 8 (into bit 3).

An exit code of 12 confirms the scenario is wired up right; 3 would mean scenario A ran last.

Lab source code, Makefile, and automated tests for this lesson: lessons/07-arithmetic-flags.

Summary

  1. RFLAGS stores properties of a result, not the result itself.
  2. ZF means zero, SF means sign, CF means unsigned carry or borrow, and OF means signed overflow.
  3. cmp and test modify only flags: the former subtracts, the latter runs bitwise AND.
  4. inc and dec leave CF untouched, so detecting carry requires add and sub.
  5. Shift counts are masked by operand width: 5 bits for 32-bit operands and 6 bits for 64-bit operands.
  6. shl by 4 multiplies by 16—it isn’t a special property of hexadecimal notation.
Next Up
We can read the flags now, though no instruction branches on them yet. In lesson 08, I unpack conditional jumps: how jcc picks a target address using ZF, SF, CF, and OF, why jg and ja give different answers on the exact same bits, and what that looks like in objdump.