Assembly for Beginners: How Computers Make Decisions (Jumps & Loops)
If you are about to start a reverse engineering course, looking at raw assembly code can feel like reading an alien language. It’s just a massive list of abbreviations and memory addresses.
But here is a secret: Assembly is mostly just a bunch of "If this, then do that" statements.
Before you can reverse-engineer a program, you need to understand how the CPU makes decisions. In this beginner-friendly guide, we’ll break down how Comparisons, Jumps, and Loops work using simple, easy-to-read code templates.
1. The Setup: Comparing Two Things (CMP)
Before the computer can decide what to do, it has to compare two values. We use the CMP (Compare) instruction.
Think of CMP like a referee looking at two numbers on a scoreboard. It looks at Number A and Number B, does the math to see which is bigger or if they are equal, and makes a mental note (by setting CPU flags).
Once the CPU has compared the numbers, the very next line is almost always a Conditional Jump.
2. The Decision: Conditional Jumps
A "Jump" does exactly what it sounds like. It tells the CPU to jump to a different line of code, completely skipping over whatever is in between.
There is an Unconditional Jump called JMP (which just means "GOTO this line, no matter what"). But the most important ones are Conditional Jumps. They only jump if a specific condition is met.
Here are the most common ones you'll see:
JE (Jump if Equal): "Jump to this new line if the two numbers are exactly the same."
JNE (Jump if Not Equal): "Jump if the numbers are different."
Real-World Example: If you type a password into a login screen, the code says CMP user_input, real_password. Then it uses JNE to jump to an "Access Denied" screen if they don't match.
Load the values.
CMP(Compare) them.JMPto a specific label based on the result.
🟢 The Equality Jumps (JE / JNE)
Use these when you want to check if two values are exactly the same.
Jump if Equal (JE)
section .data
value1 db 10
value2 db 10
section .text
mov al, [value1]
mov bl, [value2]
cmp al, bl
je equal_found ; If al == bl, jump to equal_found
not_equal:
; Your code here for the NOT equal case
jmp done
equal_found:
; Your code here for the EQUAL case
done:
; Exit code hereJump if Not Equal (JNE)
cmp al, bl jne not_equal ; If al != bl, jump to not_equal
3. The Tricky Part: Signed vs. Unsigned
Here is the hardest part for beginners: The CPU doesn't know what numbers are. It only sees binary data (1s and 0s).
For example, the binary chunk 11111111 can mean 255 (if we are counting apples) OR it can mean -1 (if we are looking at a bank account balance).
Because the CPU is blind to this, assembly has two different sets of jumps. One set is for Unsigned numbers (only positive), and one is for Signed numbers (positive AND negative). You have to tell the CPU which one to use!
🔵 The "Greater/Less" Jumps (Signed Numbers)
Use these when your variables can be negative.
Jump if Greater (JG)
cmp al, bl jg greater ; Jump if al > bl (Signed)
Jump if Greater or Equal (JGE)
cmp al, bl jge greater_or_equal ; Jump if al >= bl (Signed)
Jump if Less (JL)
cmp al, bl jl less ; Jump if al < bl (Signed)
Jump if Less or Equal (JLE)
cmp al, bl jle less_or_equal ; Jump if al <= bl (Signed)
🟠The "Above/Below" Jumps (Unsigned Numbers)
Use these when your variables can only be positive (like memory addresses or array sizes). We use the words "Above" and "Below" to distinguish them from signed jumps.
Jump if Above (JA)
cmp al, bl ja above ; Jump if al > bl (Unsigned)
Jump if Above or Equal (JAE)
cmp al, bl jae above_or_equal ; Jump if al >= bl (Unsigned)
Jump if Below (JB)
cmp al, bl jb below ; Jump if al < bl (Unsigned)
Jump if Below or Equal (JBE)
cmp al, bl jbe below_or_equal ; Jump if al <= bl (Unsigned)
4. Putting It All Together: A Full Working Example
Let's look at a complete, working program that uses a conditional jump and an external function (printf) to print a message to the screen.
This program compares 42 and 41, and uses JGE (Jump if Greater or Equal) to decide which message to print.
; jumping.asm - Exploring Conditional Jumps extern printf ; Declare the external C function printf section .data number1 dq 42 ; Define variable number1 (42) number2 dq 41 ; Define variable number2 (41) fmt1 db "NUMBER1 >= NUMBER2", 10, 0 ; Message 1 fmt2 db "NUMBER1 < NUMBER2", 10, 0 ; Message 2 section .text global main main: push rbp ; Save the base pointer mov rbp, rsp ; Set up the base pointer mov rax, [number1] ; Load number1 into RAX mov rbx, [number2] ; Load number2 into RBX cmp rax, rbx ; Compare RAX and RBX jge greater ; JUMP if RAX >= RBX to the 'greater' label ; --- This code only runs if RAX < RBX --- mov rdi, fmt2 ; Load the "less than" message mov rax, 0 ; Clear rax (required for printf) call printf ; Print "NUMBER1 < NUMBER2" jmp exit ; Jump to the end so we don't print the next line! greater: ; <--- This is where JGE jumps to ; --- This code only runs if RAX >= RBX --- mov rdi, fmt1 ; Load the "greater/equal" message mov rax, 0 call printf ; Print "NUMBER1 >= NUMBER2" exit: mov rsp, rbp ; Restore the stack pointer pop rbp ; Restore the base pointer ret ; Return from main
What did we just learn?
Notice how the labels (greater: and exit:) act like destinations. If the numbers are 42 and 41, the cmp sets the flags, jge sees that 42 is greater, and jumps directly to the greater: label. It completely skips over the code that prints the < message!