0Pricing
Assembly Language & x86 Low-Level Systems Programming · درس

استدعاء لغة التجميع من C

تعلّم دمج إجراءات التجميع في مشاريع C/C++، وتمرير المعاملات واستقبال القيم المرجعة باستخدام اصطلاحات الاستدعاء

استدعاء لغة التجميع من C درس مجاني في Assembly Language & x86 Low-Level Systems Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Assembly Language & x86 Low-Level Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why C Needs Assembly

Why would you mix C/C++ and Assembly? It might seem old-fashioned, but there are powerful reasons!

  • Performance: For critical code sections, hand-optimized assembly can be faster than compiler-generated code.
  • Hardware Access: Interact directly with hardware features not exposed by C/C++.
  • Specific Instructions: Use special CPU instructions (e.g., for cryptography or multimedia) directly.
  • Legacy Code: Integrate with existing assembly routines or operating system components.

It allows you to get the "best of both worlds" – C's high-level structure with assembly's low-level power.

The Calling Contract

When C calls an assembly function, they need to agree on a "contract" for how things work. This contract is called a calling convention.

It defines:

  • How arguments are passed (registers or stack).
  • Who cleans up the stack after the call.
  • Which registers the called function must preserve.

For modern 64-bit Linux/macOS systems, the System V AMD64 ABI is standard. We'll focus on this.

Assembly's Public Face

To make an assembly function callable from C, you need to declare it as global. This tells the linker that the function can be accessed from other files.

The basic structure looks like this:

  • Use the global directive.
  • Define your function with a label (often prefixed with an underscore, like _my_function, though not strictly required by System V ABI, it's a common practice).
  • Include your assembly instructions.
  • End with a ret instruction to return control to the caller.
section .text
global _my_function
_my_function:
    ; Your assembly code here
    ret

C's View of Assembly

From the C side, an assembly function is just like any other function defined elsewhere. You declare it using the extern keyword.

The function prototype in C must exactly match what the assembly function expects in terms of arguments and return type.

For example, if your assembly function takes two long integers and returns a long, your C prototype would be: extern long my_assembly_func(long a, long b);

extern long my_assembly_func(long a, long b);

Sending Data to Assembly

Under the System V AMD64 ABI, the first six integer or pointer arguments are passed via specific registers:

  • 1st argument: RDI
  • 2nd argument: RSI
  • 3rd argument: RDX
  • 4th argument: RCX
  • 5th argument: R8
  • 6th argument: R9

Any additional arguments are pushed onto the stack from right to left.

Getting Results Back

When your assembly function finishes, it places its return value into a specific register so C can retrieve it.

  • For integer or pointer return values, use the RAX register.
  • For floating-point values, use the XMM0 register.

After placing the result, the assembly function simply executes ret to jump back to the C code that called it.

Add Numbers: C Calls Assembly (ASM)

Let's create an assembly function to add two numbers. This file would typically be named my_add.asm.

Pay attention to how arguments are received in RDI and RSI, and the result is placed in RAX.

; my_add.asm
section .text
global add_two_numbers

add_two_numbers:
    ; RDI holds the first argument (a)
    ; RSI holds the second argument (b)
    mov rax, rdi    ; Move 'a' into RAX
    add rax, rsi    ; Add 'b' to RAX (RAX = a + b)
    ret             ; Return to caller (RAX holds the result)

Add Numbers: C Calls Assembly (C)

Now, let's write the C code that uses our assembly function. This file would typically be named main.c. Remember to declare it with extern.

To compile and link:

nasm -f elf64 my_add.asm -o my_add.o

gcc main.c my_add.o -o my_program

./my_program

// main.c
#include <stdio.h>

// Declare the assembly function
extern long add_two_numbers(long a, long b);

int main() {
    long num1 = 10;
    long num2 = 25;
    long sum;

    // Call the assembly function
    sum = add_two_numbers(num1, num2);

    printf("The sum of %ld and %ld is %ld\n", num1, num2, sum);

    return 0;
}

Protecting Registers

When your assembly function runs, it might use various registers. Some registers are caller-saved (caller must save them if it needs them later), and others are callee-saved (the assembly function must save and restore them if it modifies them).

Common callee-saved registers (x64): RBX, RBP, RSP, R12, R13, R14, R15.

If your assembly function modifies a callee-saved register, you must PUSH its value at the start and POP it back before returning.

_my_function_with_preservation:
    push rbx      ; Save RBX
    ; ... use RBX ...
    pop rbx       ; Restore RBX
    ret

Quick Check

Based on the System V AMD64 ABI, which register would typically hold the third integer argument passed to an assembly function from C?

Recap: Bridging C & Assembly

Great job! You've learned the fundamentals of calling assembly code from C.

  • We understood why you'd link C and assembly.
  • We explored calling conventions, focusing on the System V AMD64 ABI.
  • You saw how to declare functions on both the assembly and C sides.
  • You learned how arguments are passed (registers) and return values are received (RAX).
  • We touched upon register preservation rules to avoid unexpected side effects.

This skill is vital for low-level optimization and system interaction!

الأسئلة الشائعة

هل درس «استدعاء لغة التجميع من C» مجاني؟

نعم — نص درس «استدعاء لغة التجميع من C» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Assembly Language & x86 Low-Level Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.

ماذا ستتعلم في «استدعاء لغة التجميع من C»؟

تعلّم دمج إجراءات التجميع في مشاريع C/C++، وتمرير المعاملات واستقبال القيم المرجعة باستخدام اصطلاحات الاستدعاء تتمرن على Assembly Language & x86 Low-Level Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Assembly Language & x86 Low-Level Systems Programming؟

لا تُشترط خبرة سابقة. Assembly Language & x86 Low-Level Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «استدعاء لغة التجميع من C»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Assembly Language & x86 Low-Level Systems Programming هذا؟

نعم. كل درس في Assembly Language & x86 Low-Level Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استدعاء لغة التجميع من C
  2. استدعاء C من لغة التجميع
  3. تقنيات البرمجة متعددة اللغات
  4. اتفاقيات الاستدعاء: cdecl وstdcall وSystem V
← العودة إلى Assembly Language & x86 Low-Level Systems Programming