0Pricing
Reverse Engineering & Binary Analysis Basics · บทเรียน

การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน

เรียนรู้เกี่ยวกับการกระโดดตามเงื่อนไข ลูป และกลไกการเรียกฟังก์ชัน รวมถึงการใช้สแตก

การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน เป็นบทเรียน Reverse Engineering & Binary Analysis Basics ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Reverse Engineering & Binary Analysis Basics และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Reverse Engineering & Binary Analysis Basics มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Control Flow: Making Programs Smart

In assembly, instructions usually run one after another. This is called sequential execution. But real programs need to make decisions, repeat actions, and call functions.

This is where control flow comes in! It's how a program changes its execution path based on conditions, creating loops and calling subroutines.

Conditional Jumps: If-Statements

Conditional jumps are like if statements in high-level languages. They let your program execute different code blocks based on a condition.

The CMP instruction compares two values and sets CPU flags. Then, jump instructions like JE (Jump if Equal) or JNE (Jump if Not Equal) check these flags to decide whether to jump.

; Example: if (EAX == EBX) goto equal_label;
MOV EAX, 10
MOV EBX, 10
CMP EAX, EBX    ; Compare EAX and EBX
JE  equal_label ; Jump if Equal
; Code here runs if EAX != EBX
JMP end_label

equal_label:
; Code here runs if EAX == EBX

end_label:
; Program continues

More Jump Conditions

Beyond JE and JNE, many other conditional jump instructions exist to handle different comparisons:

  • JG: Jump if Greater
  • JL: Jump if Less
  • JGE: Jump if Greater or Equal
  • JLE: Jump if Less or Equal
  • JZ: Jump if Zero (often used after CMP or arithmetic)
  • JNZ: Jump if Not Zero

These instructions interpret the CPU's flags register, which stores the results of previous operations.

Building Loops in Assembly

You can create loops using conditional jumps. A loop typically involves:

  1. An initialization (e.g., setting a counter).
  2. A condition check (using CMP and a conditional jump).
  3. The loop body (instructions to repeat).
  4. An update (e.g., incrementing the counter).
  5. An unconditional jump back to the condition check.
; Example: int i = 0; while (i < 3) { i++; }
MOV ECX, 0      ; Initialize counter i = 0

loop_start:
CMP ECX, 3      ; Compare i with 3
JGE loop_end    ; Jump if i >= 3 (exit loop)

INC ECX         ; Increment i
JMP loop_start  ; Jump back to loop_start

loop_end:
; Loop has finished, continue here

The Stack: LIFO Storage

The stack is a crucial memory region used for temporary storage, especially during function calls. It operates on a LIFO (Last-In, First-Out) principle.

Think of it like a stack of plates: you can only add a new plate to the top (PUSH) or remove the top plate (POP).

  • PUSH: Decreases the stack pointer (ESP/RSP) and places data on the stack.
  • POP: Retrieves data from the top of the stack and increases the stack pointer.

Function Calls: CALL and RET

When a program needs to execute a separate block of code (a function or subroutine), it uses the CALL instruction. This is fundamental for modular programming.

  • The CALL instruction first pushes the return address (the address of the instruction immediately following CALL) onto the stack.
  • Then, it jumps unconditionally to the target function's entry point.
  • The RET instruction, typically found at the end of a function, pops the return address from the stack and jumps back to that address, resuming execution in the caller.
; Caller code:
; ... instructions
CALL my_function ; Calls the function
; ... execution continues here after my_function returns

; my_function definition:
my_function:
; ... function's body instructions
RET              ; Returns to the caller

Passing Arguments to Functions

How do functions receive input? In x86/x64 assembly, arguments are often passed via the stack or registers.

When using the stack, arguments are pushed onto the stack by the caller before the CALL instruction. The called function then accesses these arguments relative to the stack pointer (ESP/RSP) or base pointer (EBP/RBP).

The calling convention dictates the order and method of argument passing.

Local Variables and the Stack Frame

Functions also need space for their own local variables. This space is allocated on the stack within what's called a stack frame.

A stack frame is typically established by:

  1. Saving the old base pointer (PUSH EBP/RBP).
  2. Setting the new base pointer to the current stack pointer (MOV EBP, ESP/RBP, RSP).
  3. Allocating space for local variables (SUB ESP, size).

The base pointer (EBP/RBP) provides a stable reference point to access arguments and local variables within the current function.

Function Call Walkthrough

Let's see a simple C function and understand how its call, arguments, and local variables relate to assembly and the stack.

When main calls calculate_sum, the arguments 15 and 25 are pushed onto the stack. Inside calculate_sum, space for local_var is allocated on the stack. The function's return value is typically placed in a register like EAX.

#include <stdio.h>

int calculate_sum(int a, int b) {
  int local_var = a + b; // Local variable on stack
  return local_var;
}

int main() {
  int result = calculate_sum(15, 25);
  printf("Result: %d\n", result);
  return 0;
}

Quick Check: Function Calls

Understanding the stack's role in function calls is crucial for reverse engineering.

Which of the following actions occur when an x86/x64 CALL instruction is executed?

Recap: Control Flow & Functions

Great job! You've learned how programs make decisions and execute functions in assembly.

  • Conditional Jumps (JE, JNE, JG, etc.) combined with CMP enable if statements and loops.
  • The Stack is a LIFO structure critical for temporary data, managed by PUSH and POP.
  • Function Calls use CALL to push the return address and jump, and RET to pop it and return.
  • Arguments and local variables are often handled via the stack within a function's stack frame.

Understanding these concepts is vital for tracing program execution and analyzing binaries!

คำถามที่พบบ่อย

บทเรียน “การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Reverse Engineering & Binary Analysis Basics ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Reverse Engineering & Binary Analysis Basics มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน”

เรียนรู้เกี่ยวกับการกระโดดตามเงื่อนไข ลูป และกลไกการเรียกฟังก์ชัน รวมถึงการใช้สแตก คุณปฏิบัติ Reverse Engineering & Binary Analysis Basics ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Reverse Engineering & Binary Analysis Basics หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Reverse Engineering & Binary Analysis Basics บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Reverse Engineering & Binary Analysis Basics นี้ได้ไหม

ได้ บทเรียน Reverse Engineering & Binary Analysis Basics ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานภาษาแอสเซมบลี x86/x64
  2. รีจิสเตอร์และการดำเนินการกับหน่วยความจำ
  3. การควบคุมลำดับการทำงานและการเรียกฟังก์ชัน
  4. สแตกและรูปแบบการเรียกใช้
← กลับไปที่ Reverse Engineering & Binary Analysis Basics