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, decThe two workhorses take the same operand forms as mov — destination first, and the result lands there:
1add rax, rbx ; rax = rax + rbx
2sub rax, 10 ; rax = rax - 10
3add rax, [rbx] ; add the value in memory at rbxFor the common case of ±1 there are shorter, single-operand forms:
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.
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 PairMultiply 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:
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:
break _startBreakpoint 1 at 0x401000runBreakpoint 1, 0x0000000000401000 in _start ()break *0x401010Breakpoint 2 at 0x401010continueBreakpoint 2, 0x0000000000401010 in _start ()p/d $rax$1 = 28p/d $rdx$2 = 4break *0x40101dBreakpoint 3 at 0x40101dcontinueBreakpoint 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.
imulClearing 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:
1imul rax, rbx ; rax = rax * rbx
2imul rax, rbx, 10 ; rax = rbx * 10This 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.
Every add, sub, inc, dec, and neg updates the status flags in rflags as a side effect. Four matter:
| Flag | Name | Set when |
|---|---|---|
ZF | zero | the result is exactly zero |
SF | sign | the result's top bit is set (negative, read as signed) |
CF | carry | the result carried or borrowed out of the top bit — unsigned overflow |
OF | overflow | the 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 ArithmeticMemory 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:
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.
lea rax, [rdi*3]?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.