0Pricing
Assembly Language & x86 Low-Level Systems Programming · 강의

조건부 점프와 반복문

비교 명령어(CMP, TEST)와 조건부 점프 명령어(JMP, JE, JNE, JL, JG 등)를 사용해 의사 결정과 반복 작업을 구현합니다.

조건부 점프와 반복문은(는) CoddyKit의 무료 Assembly Language & x86 Low-Level Systems Programming 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Assembly Language & x86 Low-Level Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Guiding Program Flow

Programs rarely run straight through. We need to make decisions and repeat actions. This is called control flow.

Assembly language provides special instructions to change the order in which instructions are executed. This allows your programs to be dynamic and respond to different conditions.

Think of it like a choose-your-own-adventure story: you decide which path to take next!

Always Taking the Jump

The simplest way to change program flow is with an unconditional jump using the JMP instruction. It always transfers control to a specified label.

This is like a "goto" statement in higher-level languages. The program will immediately execute instructions starting from the target label.

section .data
    msg1 db "Hello!", 0xA
    len1 equ $ - msg1
    msg2 db "Skipped!", 0xA
    len2 equ $ - msg2
    msg3 db "End.", 0xA
    len3 equ $ - msg3

section .text
    global _start

_start:
    mov rax, 1           ; syscall write
    mov rdi, 1           ; stdout
    mov rsi, msg1
    mov rdx, len1
    syscall              ; Print "Hello!"

    jmp skip_message     ; Unconditional jump

    mov rax, 1
    mov rdi, 1
    mov rsi, msg2
    mov rdx, len2
    syscall              ; This part is skipped!

skip_message:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg3
    mov rdx, len3
    syscall              ; Print "End."

    mov rax, 60          ; syscall exit
    mov rdi, 0
    syscall

Making Comparisons (CMP)

Before we can make decisions, we need to compare values. The CMP instruction does this for us.

CMP destination, source works like SUB destination, source, but it discards the result. Instead, it only updates the CPU flags based on the comparison.

This is crucial for conditional jumps!

; Compare AX with BX
cmp ax, bx

; Compare value in memory with 10
cmp [my_var], 10

; Compare AL with 0
cmp al, 0

The CPU Flags Register

After a CMP instruction (or most arithmetic/logic ops), special bits in the Flags Register are updated. These bits tell us about the result of the operation.

  • ZF (Zero Flag): Set if the result was zero (or operands were equal).
  • SF (Sign Flag): Set if the result was negative.
  • CF (Carry Flag): Set if an unsigned overflow occurred.
  • OF (Overflow Flag): Set if a signed overflow occurred.

Conditional jumps check these flags!

Jumping on Equality (JE, JNE)

Once CMP sets the flags, we can use conditional jump instructions. Two common ones are JE (Jump if Equal) and JNE (Jump if Not Equal).

  • JE label: Jumps if the Zero Flag (ZF) is set (meaning operands were equal).
  • JNE label: Jumps if the Zero Flag (ZF) is clear (meaning operands were not equal).

Let's see an example.

section .data
    eq_msg db "Numbers are equal!", 0xA
    eq_len equ $ - eq_msg
    ne_msg db "Numbers are not equal!", 0xA
    ne_len equ $ - ne_msg

section .text
    global _start

_start:
    mov rax, 10
    mov rbx, 10          ; Try changing this to 5

    cmp rax, rbx         ; Compare RAX and RBX

    je  are_equal        ; If equal, jump to are_equal

    ; Else (not equal)
    mov rax, 1
    mov rdi, 1
    mov rsi, ne_msg
    mov rdx, ne_len
    syscall
    jmp end_program

are_equal:
    mov rax, 1
    mov rdi, 1
    mov rsi, eq_msg
    mov rdx, eq_len
    syscall

end_program:
    mov rax, 60
    mov rdi, 0
    syscall

Greater or Lesser (Signed)

For signed numbers, we use different conditional jumps to check for greater than or less than relationships.

  • JG label: Jump if Greater (ZF=0 and SF=OF)
  • JGE label: Jump if Greater or Equal (SF=OF)
  • JL label: Jump if Less (SF!=OF)
  • JLE label: Jump if Less or Equal (ZF=1 or SF!=OF)

These rely on combinations of the Sign Flag (SF), Overflow Flag (OF), and Zero Flag (ZF).

section .data
    gt_msg db "RAX is greater!", 0xA
    gt_len equ $ - gt_msg
    lt_msg db "RAX is less!", 0xA
    lt_len equ $ - lt_msg
    eq_msg db "RAX is equal!", 0xA
    eq_len equ $ - eq_msg

section .text
    global _start

_start:
    mov rax, 5
    mov rbx, 10          ; Compare 5 with 10

    cmp rax, rbx

    jg  is_greater       ; If RAX > RBX
    jl  is_less          ; If RAX < RBX

    ; Else, they must be equal
    mov rsi, eq_msg
    mov rdx, eq_len
    jmp print_msg

is_greater:
    mov rsi, gt_msg
    mov rdx, gt_len
    jmp print_msg

is_less:
    mov rsi, lt_msg
    mov rdx, lt_len
    jmp print_msg

print_msg:
    mov rax, 1
    mov rdi, 1
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

Unsigned Comparisons (JA, JB)

When dealing with unsigned numbers, the flags are interpreted differently. The Carry Flag (CF) is key here.

  • JA label: Jump if Above (unsigned greater than) (CF=0 and ZF=0)
  • JAE label: Jump if Above or Equal (unsigned greater than or equal) (CF=0)
  • JB label: Jump if Below (unsigned less than) (CF=1)
  • JBE label: Jump if Below or Equal (unsigned less than or equal) (CF=1 or ZF=1)

Always be mindful if you're comparing signed or unsigned values!

Building an IF-ELSE Block

We can combine CMP and conditional jumps to create if-else logic, just like in high-level languages.

The general pattern is: compare, jump if condition is FALSE to the "else" block, execute "if" block, then jump past "else" block.

section .data
    if_msg db "Condition is TRUE!", 0xA
    if_len equ $ - if_msg
    else_msg db "Condition is FALSE!", 0xA
    else_len equ $ - else_msg

section .text
    global _start

_start:
    mov rax, 20
    mov rbx, 10

    cmp rax, rbx         ; Is RAX > RBX?
    jle else_block       ; If not (less or equal), jump to else

    ; IF block (RAX > RBX)
    mov rsi, if_msg
    mov rdx, if_len
    jmp print_and_exit

else_block:
    ; ELSE block (RAX <= RBX)
    mov rsi, else_msg
    mov rdx, else_len

print_and_exit:
    mov rax, 1
    mov rdi, 1
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

Creating Loops with Jumps

Loops are fundamental for repeating code. In assembly, we combine a label, a comparison, and a conditional jump to create them.

A common pattern involves: initializing a counter, defining a loop label, performing operations, decrementing/incrementing the counter, comparing, and jumping back to the label if the condition is met.

section .data
    msg db "Looping... ", 0xA
    len equ $ - msg

section .text
    global _start

_start:
    mov rcx, 3           ; Initialize loop counter

loop_start:
    cmp rcx, 0           ; Check if counter is 0
    je  loop_end         ; If it is, exit loop

    ; Print message
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, len
    syscall

    dec rcx              ; Decrement counter
    jmp loop_start       ; Jump back to start of loop

loop_end:
    mov rax, 60
    mov rdi, 0
    syscall

Jump Condition Check

Consider the following assembly snippet. What will be printed if RAX holds the value 5 and RBX holds 10?

section .data
    msg_a db "Result A", 0xA
    len_a equ $ - msg_a
    msg_b db "Result B", 0xA
    len_b equ $ - msg_b

section .text
    global _start

_start:
    ; Assume RAX = 5, RBX = 10
    cmp rax, rbx
    jg  print_a
    jmp print_b

print_a:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg_a
    mov rdx, len_a
    syscall
    jmp end_program

print_b:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg_b
    mov rdx, len_b
    syscall

end_program:
    mov rax, 60
    mov rdi, 0
    syscall

Recap: Jumps and Loops

Great job! You've learned how to control your program's flow in assembly!

  • JMP is for unconditional jumps.
  • CMP compares values and updates the CPU's flags register.
  • Conditional jumps (like JE, JNE, JG, JL, JA, JB) check these flags to decide whether to jump.
  • By combining these, you can create powerful if-else statements and loops.

Next, we'll dive into procedures and stack management to organize your code even further!

자주 묻는 질문

“조건부 점프와 반복문” 강의는 무료인가요?

네 — “조건부 점프와 반복문” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Assembly Language & x86 Low-Level Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“조건부 점프와 반복문”에서 뭘 배우나요?

비교 명령어(CMP, TEST)와 조건부 점프 명령어(JMP, JE, JNE, JL, JG 등)를 사용해 의사 결정과 반복 작업을 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Assembly Language & x86 Low-Level Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Assembly Language & x86 Low-Level Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Assembly Language & x86 Low-Level Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“조건부 점프와 반복문” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Assembly Language & x86 Low-Level Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Assembly Language & x86 Low-Level Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 데이터 이동 명령어 (MOV, PUSH, POP)
  2. 산술 및 논리 연산
  3. 조건부 점프와 반복문
  4. 비트 연산 및 시프트 명령어
← Assembly Language & x86 Low-Level Systems Programming(으)로 돌아가기