NASM Programming Concepts - Summary Notes
Assembler & Linker
Assembler converts .asm to .o files. Linker converts .o files to executables (.exe or [Link]).
Steps to Execute a NASM Program
1. Write code: [Link]
2. Assemble: nasm -f elf64 [Link] -o program.o
3. Link: ld program.o -o program
4. Run: ./program
Assembler Directives
.data, .bss, .text, global, extern, section - used to manage memory and program structure.
dd, dw, db, dq, dt, resb, resw
db: byte (1 byte)
dw: word (2 bytes)
dd: doubleword (4 bytes)
dq: quadword (8 bytes)
dt: ten bytes
resb: reserve bytes
resw: reserve words
.data, .bss, .text Sections
.data: initialized data
.bss: uninitialized data
.text: program instructions
System Calls (64-bit)
Exit: rax=60, rdi=status
NASM Programming Concepts - Summary Notes
Read: rax=0, rdi=fd, rsi=buf, rdx=len
Write: rax=1, rdi=fd, rsi=buf, rdx=len
Accessing Array Elements
mov rax, [array + rbx] ; rbx = index in bytes
Use of Counter
Used in loops to count iterations (e.g., ecx/rcx).
Macro
Text substitution at compile time.
Example:
%macro display 1
mov rsi, %1
call print_string
%endmacro
Procedure
A function called with 'call' and exited with 'ret'. Used for code reuse.
Macro vs Procedure
Macro: Compile time, faster, no call overhead.
Procedure: Runtime, reusable, modular.
Conditional vs Unconditional Jump
Conditional: jz, jnz, js, jne etc.
Unconditional: jmp
NASM Programming Concepts - Summary Notes
String Length Register
Usually RAX after syscall read.
Call and Return Instruction
call: jumps to label and stores return address
ret: returns control
ROL and CMP Instructions
ROL BX,1: Rotate BX left by 1
CMP AX,BX: Compare AX and BX
Find Largest Number in Array
Loop through array and compare elements, store max.
Swap Instruction
xchg AX, BX
Program Logic
Sequence of operations to perform a task.
BT Instruction
Bit Test - checks a bit and updates CF.
ASCII for Newline
0x0A
Positive/Negative Check
NASM Programming Concepts - Summary Notes
Use js (jump if sign), jns (jump if not sign).
DIV Instruction
div operand: divides AX/RAX by operand, result in AL/AX/RAX, remainder in AH/DX/RDX
PUSH Instruction
push rax: pushes rax onto the stack.
Hex vs BCD
Hex: Base-16 (0-9, A-F)
BCD: Decimal digits stored in 4 bits each
HEX to BCD & vice versa
HEX to BCD: divide by 10 repeatedly
BCD to HEX: combine digits as binary
JNZ, JZ, JMP, JBE
JNZ: Jump if not zero
JZ: Jump if zero
JMP: Unconditional jump
JBE: Jump if below/equal
Accept & Display Number Procedure
Accept: read syscall, convert ASCII to int
Display: convert int to ASCII, write syscall
Operating Modes
NASM Programming Concepts - Summary Notes
Real Mode (16-bit), Protected Mode (32-bit), Long Mode (64-bit)
Descriptor & Tables
Descriptor: Defines segment info
Tables: GDT, LDT, IDT
CR0 Register
Control Register enabling protection modes.
Overlapped vs Non-Overlapped Transfer
Overlapped: Shared source and destination
Non-overlapped: Separate memory
String Instructions & Direction Flag
movsb, stosb, lodsb
STD: set flag (reverse)
CLD: clear flag (forward)
Multiplication Methods
Successive Add: Repeated addition
Add & Shift: Binary multiplication
Stack Size
Depends on OS, usually large, managed via rsp.
Data Types
Byte, Word, Doubleword, Quadword, Float
NASM Programming Concepts - Summary Notes
Coprocessor
x87 FPU or SSE/AVX SIMD
Arithmetic Instructions
add, sub, mul, div, inc, dec, neg, adc, sbb