Some instruction patterns appear so often that a fluent reader stops seeing the literal operation and reads the intent instead. This lesson collects the ones you will meet constantly — several you have already used — so that xor eax, eax reads as "zero" at a glance and a cmov does not stop you cold.
xor reg, regCovered in Bitwise Ops: xor eax, eax sets a register to zero in two bytes, where mov eax, 0 takes five and carries literal zero bytes in its encoding. Compilers emit it universally, so read xor reg, reg as "set to zero," not as exclusive-or.
lea as ArithmeticFrom Arithmetic: lea computes base + index*scale + displacement without touching memory or the flags, so compilers lean on it for address math and for multiplying by small constants. lea eax, [rdi + rdi*2] is ×3; seeing lea where no pointer is in sight almost always means "arithmetic."
test reg, reg vs cmp reg, 0Both check whether a register is zero, but test rax, rax needs no immediate operand, so it is shorter than cmp rax, 0 and sets ZF identically. It is the idiomatic "is this zero or NULL?" test, and you will see it before nearly every je/jne that checks a pointer or a count.
inc / dec and Their Flag Quirkinc and dec are the compact way to adjust by one, but — as Arithmetic noted — they deliberately leave the carry flag untouched where add/sub update it. In a multi-word add that threads carries from one limb to the next, that difference matters; everywhere else inc/dec are simply the shorter choice.
rep movsb / stosbThe rep prefix repeats a string instruction rcx times, advancing its pointers automatically. stosb stores al to [rdi]; movsb copies [rsi] to [rdi]. So rep stosb is a memset (fill rcx bytes with al) and rep movsb is a memcpy (copy rcx bytes). Compilers inline these for small fixed-size fills and copies, so recognize the pair as bulk memory operations.
cmovA cmovCC moves its source into its destination only if the flags satisfy the condition — a branch with no jump. It is how compilers compile a small if or a min/max without a conditional jump, avoiding the cost of a mispredicted branch. Here is a branchless max(7, 12):
objdump -d -M intel cmov0000000000401000 <_start>: 401000: b8 07 00 00 00 mov eax,0x7 401005: bb 0c 00 00 00 mov ebx,0xc 40100a: 48 39 d8 cmp rax,rbx 40100d: 48 0f 4c c3 cmovl rax,rbx 401011: 48 89 c7 mov rdi,rax 401014: b8 3c 00 00 00 mov eax,0x3c 401019: 0f 05 syscall./cmov; echo $?12cmp rax, rbx sets the flags, and cmovl rax, rbx overwrites rax with rbx only because rax < rbx was true — so rax ends up holding the larger value, 12, with no branch anywhere. When you see a cmp followed by a cmov instead of a jump, you are looking at a branchless conditional.
| You see | It means |
|---|---|
xor reg, reg | set reg to 0 |
lea with no pointer in play | arithmetic (often ×2/×3/×5/…) |
test reg, reg + je/jne | "is it zero / NULL?" |
rep stosb / rep movsb | memset / memcpy |
cmp + cmov | a branchless if |
When size is the goal — hand-written shellcode, a tiny binary — the same idioms become tools:
eax forms, xor over mov …, 0, and inc/dec all assemble to fewer bytes. Reusing a register you already set beats reloading a constant.0x00 byte, so the machine code must contain no zero bytes. mov eax, 0 encodes literal zeros (b8 00 00 00 00) and is disqualified — xor eax, eax (31 c0) does the same job with none. Much of shellcode craft is choosing instructions whose encodings avoid zeros.