Assembly programs control flow with conditional jumps based on CPU flags. Today you master arithmetic instructions, the FLAGS register, loops, and function calls using the call/ret convention.
ADD, SUB, MUL, DIV, INC, DEC, NEG, AND, OR, XOR, NOT, SHL, SHR are the core arithmetic instructions. MUL (unsigned) and IMUL (signed) multiply RAX by the operand; the 128-bit result goes into RDX:RAX. DIV divides RDX:RAX by the operand; quotient in RAX, remainder in RDX. Shifts (SHL/SHR) are fast multiply/divide by powers of 2.
Arithmetic operations set flags in RFLAGS: ZF (zero), SF (sign), CF (carry), OF (overflow), PF (parity). CMP a, b subtracts b from a and sets flags without storing the result. JE/JZ (jump if equal/zero), JNE (not equal), JL/JG (less/greater, signed), JB/JA (below/above, unsigned). These combine to implement if/else, while loops, and for loops.
CALL pushes RIP+1 onto the stack and jumps to the target. RET pops the saved return address into RIP. The System V AMD64 ABI (Linux calling convention): first 6 args in RDI, RSI, RDX, RCX, R8, R9; return value in RAX; callee must preserve RBX, RBP, R12-R15. Build a stack frame with 'push rbp / mov rbp, rsp' at the start and 'pop rbp / ret' at the end.
; sum.asm: sum integers 1..N using a loop
section .text
global _start
; int sum(int n) - compute 1+2+...+n
sum:
push rbp
mov rbp, rsp
xor rax, rax ; accumulator = 0
xor rcx, rcx ; counter = 0
.loop:
inc rcx ; counter++
add rax, rcx ; acc += counter
cmp rcx, rdi ; counter == n?
jne .loop
pop rbp
ret ; return rax
_start:
mov rdi, 100 ; n = 100
call sum ; result in rax
; rax now holds 5050
mov rax, 60 ; exit
xor rdi, rdi
syscall
Write a bubble sort in assembly that sorts an array of 8 quadwords (64-bit integers) defined in .data. Print the before and after arrays to stdout.