Chapter 5

Assembly - Basics

Basic instructions, data movement, and arithmetic operations

๐Ÿ“ Assembly Instruction Structure

๐Ÿ“– Definition: Assembly Language

A language that represents machine instructions in a human-readable form. Each instruction is directly translated to machine code that the CPU understands.

Basic structure of an instruction:

Structure:
INSTRUCTION  DEST, SRC
; INSTRUCTION = command name (MOV, ADD, SUB...)
; DEST = destination (where to put result)
; SRC = source (where to get value from)
Examples:
MOV EAX, 5      ; EAX = 5
ADD EBX, ECX   ; EBX = EBX + ECX
๐Ÿ“– Definition: Intel Syntax vs AT&T Syntax

There are 2 writing styles. Intel (common in Windows): DEST, SRC.
AT&T (common in Linux): SRC, DEST with additional symbols.
We'll use Intel Syntax!

๐Ÿ“ฆ Operand Types

๐Ÿ“– Definition: Operand

The parameter of an instruction - can be a register, constant value, or memory address.

Type Example Explanation
Register EAX A CPU register
Immediate 5, 0x10 Constant value
Memory [EAX] Address stored in EAX (like a pointer)
Memory + Offset [EBP-4] Address with offset
๐Ÿ’ก Square Brackets

EAX = the value inside the register
[EAX] = the value at the address stored in EAX (dereference)

๐Ÿ“ค Data Movement Instructions

MOV
MOV dest, src

Copies value from src to dest. The most common instruction!

MOV EAX, 10         ; EAX = 10
MOV EBX, EAX        ; EBX = EAX (both are 10 now)
MOV ECX, [EBP-4]    ; ECX = value from memory at address EBP-4
MOV [EBP-8], EDX    ; Store EDX in memory at address EBP-8
LEA
LEA dest, [address]

Load Effective Address - calculates an address and puts it in dest (not the value at that address!).

MOV vs LEA:
MOV EAX, [EBP-8]    ; EAX = VALUE at address EBP-8
LEA EAX, [EBP-8]    ; EAX = ADDRESS itself (EBP minus 8)
PUSH & POP
PUSH src / POP dest

Put/take a value from the Stack.

PUSH EAX    ; Put EAX on Stack, ESP decreases by 4
POP EBX     ; Take value from Stack to EBX, ESP increases by 4
XCHG
XCHG op1, op2

Swaps two values.

XCHG EAX, EBX   ; Swap values of EAX and EBX

โž• Arithmetic Instructions

ADD
ADD dest, src

dest = dest + src

ADD EAX, 5      ; EAX = EAX + 5
ADD EBX, ECX    ; EBX = EBX + ECX
SUB
SUB dest, src

dest = dest - src

SUB EAX, 3      ; EAX = EAX - 3
INC & DEC
INC op / DEC op

Add/subtract 1.

INC EAX     ; EAX = EAX + 1 (like EAX++ in C)
DEC EBX     ; EBX = EBX - 1 (like EBX-- in C)
MUL & IMUL
MUL src / IMUL src

Multiplication. MUL = unsigned, IMUL = signed.
Result is in EDX:EAX (64 bits).

Example (assume EAX = 5):
MUL EBX             ; EDX:EAX = EAX * EBX (result is 64-bit)
IMUL EAX, EBX, 10   ; EAX = EBX * 10 (convenient 3-operand form)
DIV & IDIV
DIV src / IDIV src

Division. EDX:EAX divided by src.
Quotient โ†’ EAX, Remainder โ†’ EDX.

Example - divide EAX by ECX:
XOR EDX, EDX    ; Zero EDX (REQUIRED before DIV!)
DIV ECX         ; EAX = quotient, EDX = remainder
NEG
NEG op

Negates (flip sign). op = -op

NEG EAX     ; Negate: EAX = -EAX

๐Ÿ”ฃ Logical Instructions (Bitwise)

๐Ÿ“– Definition: Bitwise Operations

Operations that work on each bit separately. AND, OR, XOR, NOT.

Operation 0 op 0 0 op 1 1 op 0 1 op 1
AND 0 0 0 1
OR 0 1 1 1
XOR 0 1 1 0
AND EAX, 0xFF   ; Keep only lower byte (mask)
OR  EBX, 0x80   ; Set bit 7 to 1
XOR ECX, ECX    ; Zero ECX (fast trick!)
NOT EDX         ; Flip all bits (0 to 1, 1 to 0)
๐Ÿ’ก Common Tricks

XOR EAX, EAX - fast way to zero a register
AND EAX, 0 - also zeros, but less common
TEST EAX, EAX - checks if EAX equals 0

โ†”๏ธ Shift Instructions

SHL / SHR
SHL dest, count / SHR dest, count

Shift Left / Shift Right - shifts bits left/right.
SHL ร— 2 multiplies by 2. SHR รท 2 divides by 2.

SHL EAX, 1  ; EAX = EAX * 2  (shift left by 1)
SHL EAX, 3  ; EAX = EAX * 8  (shift left by 3, because 2^3=8)
SHR EBX, 2  ; EBX = EBX / 4  (shift right by 2, because 2^2=4)
SHL (Shift Left) Before: 1 0 1 0 โ†’ After SHL 1: 0 1 0 0 0 enters

๐Ÿ” Example - Reading Code

Let's read a simple Assembly snippet:

Assembly:
; PROLOGUE - create stack frame, allocate 8 bytes
push ebp
mov ebp, esp
sub esp, 8

; LOCAL VARIABLES
mov dword [ebp-4], 5    ; local_var_1 = 5
mov dword [ebp-8], 10   ; local_var_2 = 10

; CALCULATION
mov eax, [ebp-4]        ; EAX = 5
add eax, [ebp-8]        ; EAX = 5 + 10 = 15

; EPILOGUE - cleanup and return
mov esp, ebp
pop ebp
ret                     ; Return value is in EAX (15)

This code is equivalent to C:

C equivalent:
int function() {
    int a = 5;
    int b = 10;
    return a + b;  // returns 15
}

๐Ÿ“‹ Chapter Summary