Saltar al contenido

Lección 20 · Assembly and Reverse Engineering Basics

x86 Registers and Instructions for Analysts

Learn the x86 registers, flags and the instructions you meet most in disassembly, with a runnable toy emulator that steps through a short, harmless routine.

Static and dynamic analysis tell you a lot, but sooner or later a question comes up that only the code can answer: how does this routine decide what to do, how is this string decrypted, what does this function return? Answering it means reading assembly, the human-readable form of the machine instructions a disassembler prints. You do not need to write assembly. You need to read it fluently enough to follow a short function and to spot the handful of patterns that matter.

This chapter uses x86 and x86-64 because most Windows malware is built for them. No real disassembler or debugger can run on this site, so listings are read-only examples with made-up code. The runnable parts are small stand-ins in Python: a toy CPU that executes a few instructions on harmless numbers, so you can watch registers and flags change. They are teaching models, not real emulators.

What you read in a disassembler

Registers Tiny fast variables inside the CPU.
Instructions One small operation each.
Flags Results of the last compare or maths.

Everything else is built from these three.

1 / 2

Registers

A register is a small storage slot inside the CPU. On 32-bit x86 the general-purpose ones are eax, ebx, ecx, edx, esi, edi, ebp and esp. On 64-bit x86-64 they are widened to rax, rbx and so on (64 bits), and eight more are added: r8 to r15. The smaller names still exist: eax is the low 32 bits of rax, ax the low 16, and al the low 8.

Some registers have habitual roles, and recognising them speeds up reading:

  • eax/rax usually holds a return value, and is the accumulator for arithmetic.
  • ecx/rcx is often a counter for loops and string operations.
  • esp/rsp is the stack pointer and ebp/rbp often the frame pointer; see the stack and calling conventions.
  • esi and edi are source and destination in memory copies.
  • eip/rip is the instruction pointer: the address of the next instruction. You never write to it directly; jumps and calls change it.

Flags

The CPU keeps a register of single-bit flags describing the result of the last arithmetic or compare. The ones you need first are:

Flag Name Set when
ZF Zero the result was zero (so a compare found equality)
SF Sign the result was negative
CF Carry an unsigned operation overflowed or borrowed
OF Overflow a signed operation overflowed

Conditional jumps read these flags. That is how if and while exist at the machine level: an instruction sets flags, and a jump looks at them. More in control flow in disassembly.

The instructions you meet constantly

Assembly in Intel syntax reads operation destination, source. The following small set covers a large share of what you will see:

Instruction Meaning
mov a, b copy b into a
lea a, [expr] compute the address expr and store it in a (no memory read)
add, sub arithmetic; update flags
xor a, b bitwise exclusive-or; xor eax, eax is the idiom for setting a register to 0
and, or, shl, shr bit masks and shifts
cmp a, b compute a - b, keep only the flags
test a, b compute a AND b, keep only the flags
push, pop put a value on / take a value off the stack
call, ret call a function / return from it
jmp, jz, jnz, jl... unconditional and conditional jumps

Square brackets mean memory: mov eax, [ebx+8] reads the 32-bit value at address ebx+8; mov eax, ebx just copies a register. xor is worth remembering for another reason: encryption and string-decoding routines in malware lean on it, as the string obfuscation lesson shows.

A short read-only listing, with the meaning of each line in a comment (made-up code):

TEXT
mov   ecx, 5          ; counter = 5
xor   eax, eax        ; total = 0
loop_top:
add   eax, ecx        ; total += counter
sub   ecx, 1          ; counter -= 1
jnz   loop_top        ; repeat while counter != 0
ret                   ; return total in eax

Read it as the pseudo-code total = 0; for counter in 5..1: total += counter; return total. The function returns 15. This is the skill you are building: turn lines back into intent.

A toy CPU you can step through

The stand-in below implements a few instructions over a dictionary of registers and runs the listing above. It prints the registers after every step so you can see the loop. It is a model of the ideas, not an x86 emulator: no memory, no real encoding, no real flags beyond zero.

Python Step a tiny routine and watch the registers

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that jnz does not look at ecx directly: it looks at the zero flag left behind by the sub before it. That separation, an operation that sets flags followed by a jump that reads them, appears in every loop and condition you will read.

Reading little-endian data

x86 stores multi-byte numbers little-endian: the least significant byte comes first in memory. The 32-bit value 0x00401000 is stored as the bytes 00 10 40 00. When you read a hex dump of a file or of memory, integers look reversed, and so do addresses. The next block uses Python's struct module to show it on made-up bytes.

Python Little-endian numbers in a hex dump

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fourth chunk, 4d 5a 90 00, begins with the bytes for the letters MZ, the signature of a Windows executable, which you met in PE file structure.

A listing means nothing without the calling context: who calls this function and with what arguments. Always check the callers before you decide what a routine is for.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does the instruction xor eax, eax do?

    XOR of a value with itself is always zero, so it is the shortest way to clear a register.
  2. 2. How is the 32-bit value 0x00401000 stored in memory on x86?

    x86 is little-endian, so the least significant byte comes first.
  3. 3. Which instruction only sets flags, without storing the subtraction result?

    cmp computes a - b only to update the flags; test does the same with AND.
  4. 4. What do the square brackets in mov eax, [ebx+8] mean?

    Brackets denote a memory access; without them the operand is the register or value itself.

Summary

  • Registers are the CPU's fast variables; eax often carries return values and ecx counters.
  • Flags record the result of the last operation, and conditional jumps read them.
  • A small set of instructions (mov, lea, add, sub, xor, cmp, test, push, pop, call, ret, jumps) covers most listings.
  • Brackets mean memory access; integers in dumps are little-endian.
  • Translate lines back into intent, and check callers for context.
  • The emulator here is a labelled teaching model, not a real x86 emulator.

Next: The Stack and Calling Conventions

Sigue tu progreso

Crea una cuenta gratis para marcar lecciones y retomar donde lo dejaste en cualquier dispositivo.