Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. idioms
4 min read

Assembly Idioms & Shortcuts

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.

Zeroing with xor reg, reg

Covered 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 Arithmetic

From 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, 0

Both 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 Quirk

inc 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.

String Ops: rep movsb / stosb

The 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.

Conditional Moves: cmov

A 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
$ 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
shell
$ ./cmov; echo $?12

cmp 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.

Recognizing These in the Wild

You seeIt means
xor reg, regset reg to 0
lea with no pointer in playarithmetic (often ×2/×3/×5/…)
test reg, reg + je/jne"is it zero / NULL?"
rep stosb / rep movsbmemset / memcpy
cmp + cmova branchless if

Writing Small Assembly

When size is the goal — hand-written shellcode, a tiny binary — the same idioms become tools:

  • Prefer short encodings. The eax forms, xor over mov …, 0, and inc/dec all assemble to fewer bytes. Reusing a register you already set beats reloading a constant.
  • Keep it null-free. Shellcode is often injected through string functions that stop at a 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.
  • Shrink the binary. Strip symbols, drop unneeded sections, and collapse segments; a freestanding program can be a few hundred bytes.
See the Minimizing ELF Binaries post for how far this can be pushed.