Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. arithmetic
6 min read

Arithmetic

The CPU's arithmetic instructions do what you expect — add, subtract, multiply, divide — but two things make them worth a careful look. They work in place, writing the result back over the destination operand, and they quietly set the flags as a side effect, which is how the next instruction learns whether the result was zero, negative, or overflowed. Multiply and divide also break the usual two-operand mold in a way that trips up everyone the first time.

add, sub, inc, dec

The two workhorses take the same operand forms as mov — destination first, and the result lands there:

nasm
1add rax, rbx        ; rax = rax + rbx
2sub rax, 10         ; rax = rax - 10
3add rax, [rbx]      ; add the value in memory at rbx

For the common case of ±1 there are shorter, single-operand forms:

nasm
1inc rax             ; rax = rax + 1
2dec rcx             ; rcx = rcx - 1
3neg rax             ; rax = -rax  (two's complement)

inc and dec are a byte shorter than the equivalent add/sub and read better in a loop, with one subtlety: they leave the carry flag untouched where add/sub would update it. That rarely matters, but it is why some compilers still prefer add rax, 1.

Signed and Unsigned Are the Same Instruction

There is no separate "signed add." Because of two's complement (from Binary & Hex), adding and subtracting produce identical bits whether you read the operands as signed or unsigned — add on 0xffffffffffffffff means either -1 + x or (2⁶⁴ − 1) + x, and the resulting bit pattern is the same either way. What differs is which flag tells you it overflowed, covered below. Multiply and divide are the exception: there the signed and unsigned versions really are different instructions.

mul and div: the Implicit rdx:rax Pair

Multiply and divide are lopsided. A 64-bit multiply can produce a 128-bit result, and a 64-bit divide needs a 128-bit dividend — too big for one register — so these instructions treat rdx:rax as a single 128-bit pair, rdx holding the high half and rax the low half. The one explicit operand is the other number:

nasm
1mul rbx             ; rdx:rax = rax * rbx                      (unsigned)
2div rbx             ; rax = rdx:rax / rbx , rdx = remainder    (unsigned)

imul / idiv are the signed counterparts. This hidden use of rdx is the classic trap: before a div you must set rdx yourself, or the CPU divides a garbage 128-bit number — a wrong answer, or a crash (#DE) when the quotient does not fit. For an unsigned divide, zero it with xor rdx, rdx; for a signed divide, sign-extend rax into rdx with cqo. Watch a division and a multiply run:

gdb
gef➤ break _startBreakpoint 1 at 0x401000gef➤ runBreakpoint 1, 0x0000000000401000 in _start ()gef➤ break *0x401010Breakpoint 2 at 0x401010gef➤ continueBreakpoint 2, 0x0000000000401010 in _start ()gef➤ p/d $rax$1 = 28gef➤ p/d $rdx$2 = 4gef➤ break *0x40101dBreakpoint 3 at 0x40101dgef➤ continueBreakpoint 3, 0x000000000040101d in _start ()

The division put the quotient 28 in rax and the remainder 4 in rdx — one instruction hands you both halves of 200 / 7. The multiply left 42 in rax and 0 in rdx, the product being small enough to fit the low half.

The Modern Two-Operand imul

Clearing rdx and losing whatever it held is a nuisance, so for ordinary multiplication — where you do not need the full 128-bit result — imul also has two- and three-operand forms that behave like every other instruction and leave rdx alone:

nasm
1imul rax, rbx       ; rax = rax * rbx
2imul rax, rbx, 10   ; rax = rbx * 10

This is what compilers emit for a plain a * b in C. Reach for one-operand mul/div only when you actually need the wide product or the remainder.

Flags Set by Arithmetic

Every add, sub, inc, dec, and neg updates the status flags in rflags as a side effect. Four matter:

FlagNameSet when
ZFzerothe result is exactly zero
SFsignthe result's top bit is set (negative, read as signed)
CFcarrythe result carried or borrowed out of the top bit — unsigned overflow
OFoverflowthe result left the signed range — signed overflow

The split between CF and OF is the payoff of the "same instruction" point above: a single add sets both, and your code reads whichever one matches how you are treating the value. In a byte, 255 + 1 sets CF (unsigned wrapped past 255) while 127 + 1 sets OF (signed wrapped past +127). These flags are exactly what conditional jumps test — the whole mechanism is Control Flow.

lea for Arithmetic

Memory introduced lea as "compute the address, don't dereference it." Because the address it computes can be any base + index*scale + displacement, lea doubles as a fast arithmetic instruction that never reads memory — and, crucially, never disturbs the flags:

nasm
1lea rax, [rdi + rdi*2]      ; rax = rdi * 3
2lea rax, [rdi + rsi*4 + 

Two things make this a compiler favorite. First, because lea leaves the flags alone, you can slip it into the middle of a flag-sensitive sequence without clobbering a comparison you are about to act on. Second, it multiplies by a small constant for free. The scale (2, 4, or 8) gives you rdi*2, rdi*4, or rdi*8, and adding the base register back in — rdi + rdi*2, as above — reaches the in-between factors: rdi + rdi*2 is ×3, rdi + rdi*4 is ×5, rdi + rdi*8 is ×9. Between those two tricks, a single lea can multiply by 2, 3, 4, 5, 8, or 9 without ever issuing a mul. The limits: the scale must be 1, 2, 4, or 8, and the destination must be a register.

Why can't I just write lea rax, [rdi*3]?
Because the scale lives in a 2-bit field of the instruction (the scale bits of the x86 SIB byte), and 2 bits can name only four values — 1, 2, 4, and 8, the powers of two. There is no bit pattern for a scale of 3, so the assembler rejects it. That is exactly why ×3 is built as rdi + rdi*2: one rdi from the base register plus two more from a legal scale of 2. The same move covers ×5 (rdi + rdi*4) and ×9 (rdi + rdi*8); a factor that can't be reached this way — ×7, for instance — needs a real imul or more than one instruction. The full layout of that byte is in Instruction Encoding.
gef➤
p/d $rax
$3 = 42
gef➤ p/d $rdx
$4 = 0
8
]
; rax = rdi + rsi*4 + 8, in one instruction