Перейти к основному содержимому
Assembly Basics: x86/x64 & ARM Control Flow
Урок 2 из 11

Assembly Basics: x86/x64 & ARM Control Flow

Read core x86-64 and ARM64 data, arithmetic, and branching instructions, and reconstruct conditionals and XOR loops inside objdump and Ghidra.

Средний 30 мин Реверс-инжиниринг
Этого урока пока нет на выбранном языке — показана версия на другом языке. Перевод готовится.
Только в разрешённой среде — Описанные здесь приёмы применяются только в учебных лабораториях и в системах, которые принадлежат вам или на которые есть письменное разрешение.

Цели обучения

  • Distinguish Intel and AT&T operand ordering when reading x86-64 disassembly
  • Interpret flag-setting comparisons and signed versus unsigned conditional jumps
  • Recognise basic ARM64 load-store and conditional branch instructions
  • Reconstruct and invert a compiled XOR loop from assembly instructions

Рекомендуется заранее: CPU Registers, Memory Addressing & Calling Conventions

#Why this matters

Understanding registers and calling conventions lets you spot function inputs, yet most security logic lives inside branches and loops. Password checkers, protocol parsers, and obfuscation routines all reduce to comparisons, flags, and jumps.

When you open a binary in objdump or Ghidra, recognising these control-flow idioms turns hundreds of assembly lines into readable if statements and for loops across both x86-64 and ARM64 targets.

#Moving, calculating, and syntax conventions

Before tracing branches, check which syntax your tool displays. Ghidra and objdump -M intel use Intel syntax (mnemonic destination, source), whereas objdump -d without -M intel defaults to AT&T syntax (mnemonic source, destination with % before registers and $ before constants):

  • Intel: mov eax, 0x10 copies 0x10 into eax.
  • AT&T: mov $0x10, %eax does the exact same thing.

Core arithmetic and bitwise operations (add, sub, imul, and, or, xor, shl, shr) appear constantly. Two idioms deserve special attention:

  • xor eax, eax zeroes eax (and upper bits of rax) using fewer instruction bytes than mov eax, 0.
  • xor al, key encodes or decodes a byte reversibly, because (b ^ key) ^ key == b.

ARM64 (AArch64) uses a load-store architecture. Instructions such as add, sub, and eor (exclusive OR) only touch registers; reading or writing memory requires ldr (load register) and str (store register), for example ldr w1, [x0, #4] to read 32 bits from x0 + 4 and str w1, [x0, #4] to write w1 to x0 + 4.

#Flags and conditional branching

Processors decide branches in two steps: an instruction updates status flags (RFLAGS on x86-64, NZCV on ARM64), and a branch instruction reads those flags.

On x86-64, cmp a, b computes a - b without storing the result, while test a, b computes a & b without storing it. Compilers routinely emit test rax, rax followed by jz (jump if zero) to check for NULL pointers or zero return values.

Condition x86-64 jump ARM64 branch Meaning
Equal / Zero je / jz b.eq ZF == 1 (a == b)
Not equal jne / jnz b.ne ZF == 0 (a != b)
Signed greater jg b.gt Signed a > b
Signed less jl b.lt Signed a < b
Unsigned above ja b.hi Unsigned a > b
Unsigned below jb b.lo Unsigned a < b

ARM64 also provides cbz x0, label (compare and branch on zero) and cbnz x0, label, combining a zero check and branch into one instruction.

#Recognising loops and XOR decoders

Compilers often transform a for loop into a bottom-tested structure: check once before entering (or jump straight to the condition at the bottom), run the body, increment the counter, and jump backward if more iterations remain.

  1. Initmov ecx, 0 (counter = 0)
  2. Bodymovzx eax, byte [rdi+rcx]; xor al, 0x55
  3. Checkcmp al, byte [rsi+rcx]; jne fail
  4. Nextinc rcx; cmp rcx, 5; jb Body

When you see a backward jump (jb, jl, jne, or ARM64 b.lt) targeting an earlier address in the same function, you are looking at a loop. In Ghidra's Function Graph view, backward edges appear clearly as arrows looping upward around a block.

Inspecting an XOR verification loop in Intel syntax
$ objdump -d -M intel ./xor_check | sed -n '/<verify_key>:/,/<main>:/p'
0000000000001160 <verify_key>:
    1160: 31 c9                 xor    ecx,ecx
    1162: 0f b6 04 0f           movzx  eax,BYTE PTR [rdi+rcx*1]
    1166: 34 55                 xor    al,0x55
    1168: 3a 04 0e              cmp    al,BYTE PTR [rsi+rcx*1]
    116b: 75 0d                 jne    117a <verify_key+0x1a>
    116d: 48 ff c1              inc    rcx
    1170: 48 83 f9 05           cmp    rcx,0x5
    1174: 75 ec                 jne    1162 <verify_key+0x2>
    1176: b8 01 00 00 00        mov    eax,0x1
    117b: c3                    ret
    117a: 31 c0                 xor    eax,eax
    117c: c3                    ret

Here rdi holds the user buffer, rsi holds the 5-byte expected array, ecx starts at 0, every input byte is XORed with 0x55, and any mismatch exits immediately via jne 117a returning 0.

#Common mistakes

  • Misreading AT&T operand order. In default objdump -d, sub $0x20, %rsp subtracts 0x20 from rsp, not the reverse. Pass -M intel when you prefer Intel order.
  • Confusing signed and unsigned jumps. jl/jg interpret values as signed two's complement (0xffffffff is -1, which is < 100), whereas jb/ja treat them as unsigned (4294967295 > 100).
  • Assuming test writes its result. Both cmp and test only update CPU flags; neither modifies its operands.
  • Reversing ARM64 str direction. str x1, [x0] stores x1 into memory at x0, unlike ldr x1, [x0] which loads from memory into x1.

#Summary

Control flow in compiled binaries relies on instructions that set flags (cmp, test, subs) paired with conditional branches (je, jne, jg, jb, b.eq, cbz). Loops stand out through counter updates and backward jumps to earlier addresses, and byte-level XOR loops can be reversed directly by applying the same XOR key to the target bytes. Checking operand syntax and signedness prevents common misreadings when analysing unfamiliar x86-64 or ARM64 functions.

Проверка знаний

Войдите, чтобы проходить тест, завершать уроки и сохранять прогресс. Войти в систему

Практика: проверьте свои знания