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

التنبؤ بالتفرعات والتنفيذ التخميني

تعرّف إلى كيفية تنبؤ وحدات المعالجة المركزية الحديثة بالتفرعات وتنفيذها تخمينيًا لإخفاء زمن الانتظار، وكيف تكلف التنبؤات الخاطئة دورات، وكيف أدت الآثار الجانبية إلى هجمات من فئة Spectre.

الدرس 4 من 413 خطوة

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

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

The Pipeline Problem

Modern CPUs are deeply pipelined, fetching and decoding many instructions ahead. But a conditional branch is a fork: the CPU does not yet know which path to fetch. Stalling would waste the whole pipeline.

Branch Prediction

To avoid stalls the CPU predicts which way a branch will go and keeps fetching. If correct, no time is lost. If wrong, the pipeline is flushed — a costly misprediction penalty of 15-20+ cycles.

How Predictors Learn

The Branch Target Buffer and history tables record past outcomes. A simple 2-bit saturating counter remembers whether a branch was recently taken, predicting that loops keep looping.

Speculative Execution

Beyond predicting, the CPU speculatively executes the predicted path before the condition resolves. If the guess holds, results are committed; if not, they are discarded as if they never ran — architecturally.

Writing Predictable Branches

You help the predictor by making branches consistent. A branch that is almost always taken predicts well; a random branch defeats prediction. Sorting data before a conditional loop can dramatically speed it up.

for (int i = 0; i < n; i++)
    if (data[i] >= 128)   // predictable only if data is sorted
        sum += data[i];

A Runnable Benchmark

This C program shows the dramatic effect of sorted vs unsorted data on a branch-heavy loop. Run it and compare timings.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int n = 32768;
    int *d = malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) d[i] = rand() % 256;
    long sum = 0;
    for (int r = 0; r < 1000; r++)
        for (int i = 0; i < n; i++)
            if (d[i] >= 128) sum += d[i];
    printf("sum=%ld\n", sum);
    free(d);
    return 0;
}

Branchless Programming

You can sometimes eliminate a branch entirely with arithmetic or conditional-move instructions (cmov), so the CPU never needs to predict.

cmp eax, 128
cmovge ebx, ecx   ; conditionally move, no branch to mispredict

Likely/Unlikely Hints

Compilers expose hints like __builtin_expect (the source of likely()/unlikely() macros) so hot paths fall through and cold paths jump away, improving instruction-cache layout.

if (__builtin_expect(error, 0)) {
    handle_error();   // marked cold/unlikely
}

The Security Side Effect

Speculative results are discarded architecturally — but they leave traces in the cache. Speculatively loaded data warms cache lines, and that timing difference can be measured. This is the basis of side-channel leaks.

Spectre in a Nutshell

Spectre tricks the predictor into speculatively reading memory it should not, then leaks the value through a cache timing side channel. The reads never commit, so they bypass normal bounds checks during the speculation window.

Mitigations

Defenses include serializing instructions (lfence) to stop speculation past a bounds check, retpolines for indirect branches, and microcode updates. They trade some performance for safety.

cmp index, limit
jae out_of_range
lfence            ; block speculation past the check

Quick Check

Test your understanding of speculation.

Recap

You learned how CPUs hide branch latency:

  • Branch prediction guesses the path; mispredicts cost a pipeline flush
  • Speculative execution runs the predicted path early
  • Predictable branches, cmov, and likely/unlikely hints boost speed
  • Speculation leaves cache side effects exploited by Spectre; lfence and retpolines mitigate it
البدء مجانًا

تعلم Assembly مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

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

هل درس «التنبؤ بالتفرعات والتنفيذ التخميني» مجاني؟

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

ماذا ستتعلم في «التنبؤ بالتفرعات والتنفيذ التخميني»؟

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

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

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

كم من الوقت يستغرق درس «التنبؤ بالتفرعات والتنفيذ التخميني»؟

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

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

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

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

  1. اتساق ذاكرة التخزين المؤقت والأداء
  2. التحسين اليدوي للأقسام الحرجة
  3. تجاوزات المخزن المؤقت وShellcode
  4. التنبؤ بالتفرعات والتنفيذ التخميني
← العودة إلى Assembly Language & x86 Low-Level Systems Programming