You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: Each register has smaller variants (e.g., AX=lower 16 bits of EAX, AL=lower 8 bits)
Data Movement
; Basic movesmoveax,42 ; Load immediate value 42 into EAXmovebx,eax ; Copy EAX value to EBXmoveax,[ebx] ; Load value from memory address in EBXmov[address],ecx ; Store ECX value at memory address; Size-specific movesmoval,0xFF ; Move byte (8-bit) to ALmovax,0x1234 ; Move word (16-bit) to AXmoveax,0x12345678 ; Move dword (32-bit) to EAX; Zero/sign extensionmovzxeax, byte [mem] ; Zero-extend byte to 32-bitmovsxeax, word [mem] ; Sign-extend word to 32-bit; Address calculationleaedi,[eax+ecx*4] ; Load effective address (eax + ecx*4)
; Logical operationsandeax,0xFF ; Keep only lowest 8 bits (mask)oreax,0x80000000 ; Set highest bitxoreax,ebx ; Bitwise XORnoteax ; Bitwise NOT (flip all bits)xoreax,eax ; Fast way to zero EAX; Bit shiftsshleax,2 ; Shift left 2 bits (multiply by 4)shreax,2 ; Shift right 2 bits (unsigned divide by 4)sareax,2 ; Arithmetic shift right (signed divide by 4); Bit rotationroleax,8 ; Rotate left 8 bitsroreax,8 ; Rotate right 8 bits; Bit testingtesteax,0x01 ; Test if bit 0 is set (sets ZF)bteax,5 ; Test bit 5 (sets CF)bswapeax ; Reverse byte order (endian swap)
Flags Register
Common flags affected by operations:
ZF (Zero Flag): Set if result is zero
SF (Sign Flag): Set if result is negative (MSB = 1)
CF (Carry Flag): Set if arithmetic overflow/underflow occurs
OF (Overflow Flag): Set if signed overflow occurs
PF (Parity Flag): Set if result has even number of 1 bits
; Comparison (sets flags without storing result)cmpeax,ebx ; Compare EAX with EBX (sets ZF, SF, CF, OF)testeax,eax ; Test if EAX is zero (sets ZF, SF); Flag manipulationstc ; Set carry flag (CF = 1)clc ; Clear carry flag (CF = 0)cmc ; Complement carry flag
Control Flow
Unconditional Jumps
jmp label ; Jump to labeljmpeax ; Jump to address in EAX
Conditional Jumps (use after cmp or test)
; Equalityje/jz label ; Jump if equal/zero (ZF = 1)jne/jnz label ; Jump if not equal/not zero (ZF = 0); Signed comparisonsjg/jnle label ; Jump if greater (signed)jge/jnl label ; Jump if greater or equal (signed)jl/jnge label ; Jump if less (signed)jle/jng label ; Jump if less or equal (signed); Unsigned comparisonsja/jnbe label ; Jump if above (unsigned)jae/jnb label ; Jump if above or equal (unsigned)jb/jnae label ; Jump if below (unsigned)jbe/jna label ; Jump if below or equal (unsigned); Flag-based jumpsjs label ; Jump if negative (SF = 1)jns label ; Jump if not negative (SF = 0)jc label ; Jump if carry (CF = 1)jnc label ; Jump if no carry (CF = 0)jo label ; Jump if overflow (OF = 1)jno label ; Jump if no overflow (OF = 0)
Loops
loop label ; Decrement ECX, jump if ECX ≠ 0loope label ; Decrement ECX, jump if ECX ≠ 0 AND ZF = 1loopne label ; Decrement ECX, jump if ECX ≠ 0 AND ZF = 0; Example loopmovecx,10 ; Set loop countermy_loop: ; ... loop body ...loop my_loop ; Repeat 10 times
Memory Addressing Modes
; Direct addressingmoveax,[0x401000] ; Load from absolute address; Register indirectmoveax,[ebx] ; Load from address in EBX; Register + displacementmoveax,[ebx+8] ; Load from EBX + 8 bytes; Scaled indexingmoveax,[ebx+ecx*4] ; Load from EBX + (ECX * 4)moveax,[ebx+ecx*4+8]; Load from EBX + (ECX * 4) + 8; Array access examplemovesi,0 ; Index = 0moveax,[array+esi*4]; Load array[index] (4 bytes per element)
Stack Operations
The stack grows downward (toward lower addresses).
; Basic operationspusheax ; ESP -= 4, then [ESP] = EAXpopebx ; EBX = [ESP], then ESP += 4; Multiple registerspusha/pushad ; Push all general-purpose registerspopa/popad ; Pop all general-purpose registerspushf/pushfd ; Push flags registerpopf/popfd ; Pop flags register; Stack frame managementpushebp ; Save old base pointermovebp,esp ; Set new base pointersubesp,16 ; Allocate 16 bytes for local variables; ... function body ...movesp,ebp ; Restore stack pointerpopebp ; Restore old base pointer; Or use: leave ; Equivalent to above two instructions
Function Calls (cdecl convention)
; Calling a functionpush20 ; Push second argumentpush10 ; Push first argumentcall my_function ; Call function (pushes return address)addesp,8 ; Clean up stack (2 args × 4 bytes); Return value is in EAX; Function definitionmy_function:pushebp ; Save caller's base pointermovebp,esp ; Set up new base pointersubesp,8 ; Allocate local variables ; Access argumentsmoveax,[ebp+8] ; First argumentmovebx,[ebp+12] ; Second argument ; Function body here ; Return value goes in EAXmovesp,ebp ; Clean up local variablespopebp ; Restore caller's base pointerret ; Return to caller
Assembly Structure & Directives
section .data ; Initialized data segment msg db "Hello, World!",0x0A,0 ; String with newline and null terminator number dd 42 ; 32-bit integer array dd 1,2,3,4,5 ; Array of integerssection .bss ; Uninitialized data segment buffer resb 256 ; Reserve 256 bytes counter resd 1 ; Reserve 1 dword (4 bytes)section .text ; Code segmentglobal_start ; Entry point for linker_start: ; Your code here ; Exit program (Linux)moveax,1 ; sys_exitmovebx,0 ; exit statusint0x80 ; system call