← Retour à la page de la matière

🧩 Exercices Corrigés — Langage Assembleur (x86-64)

Sommaire


🧮 Exercice 1 — if simple

🎯 Objectif

Traduire un if C vers code aplati puis en assembleur.

🔹 Énoncé (C)


if (x > y)
    x = x - y;

💡 Correction

🧱 Flattened C


t1 = (x > y);
if (t1 == 0) goto END;
x = x - y;
END:

⚙️ Assembleur (NASM)


cmp eax, ebx
jle END
sub eax, ebx
END:

Explication :


🔁 Exercice 2 — if ... else

🎯 Objectif

Traduire un if ... else en code aplati et en assembleur.

🔹 Énoncé (C)


if (x > y)
    z = x;
else
    z = y;

💡 Correction

🧱 Flattened C


t1 = (x > y);
if (t1 == 0) goto ELSE;
z = x;
goto END;
ELSE:
z = y;
END:

⚙️ Assembleur (NASM)


cmp eax, ebx
jle ELSE
mov ecx, eax
jmp END
ELSE:
mov ecx, ebx
END:

🔂 Exercice 3 — Boucle while

🎯 Objectif

Traduire un while en version aplatie et assembleur.

🔹 Énoncé (C)


while (x < y)
    x++;

💡 Correction

🧱 Flattened C


LOOP:
t1 = (x < y);
if (t1 == 0) goto END;
x++;
goto LOOP;
END:

⚙️ Assembleur (NASM)


LOOP:
cmp eax, ebx
jge END
inc eax
jmp LOOP
END:

🔁 Exercice 4 — Boucle for

🎯 Objectif

Traduire une boucle for avec calculs et condition.

🔹 Énoncé (C)


sum = 0;
for (i = 0; i < n; i++)
    sum += i;

💡 Correction

🧱 Flattened C


sum = 0;
i = 0;
LOOP:
t1 = (i < n);
if (t1 == 0) goto END;
sum = sum + i;
i++;
goto LOOP;
END:

⚙️ Assembleur (NASM)


mov eax, 0      ; sum
mov ecx, 0      ; i
LOOP:
cmp ecx, ebx
jge END
add eax, ecx
inc ecx
jmp LOOP
END:

🔸 Exercice 5 — Entiers non signés

🎯 Objectif

Traduire un test entre entiers non signés.

🔹 Énoncé (C)


unsigned int a = 10, b = 20;
if (a < b)
    a = b;

💡 Correction

🧱 Flattened C


t1 = (a < b);  // comparaison non signée
if (t1 == 0) goto END;
a = b;
END:

⚙️ Assembleur (NASM)


cmp eax, ebx
jae END
mov eax, ebx
END:

🧩 Exercice 6 — Tableau : accès direct

🎯 Objectif

Lire les éléments d’un tableau et les additionner.

🔹 Énoncé (C)


int a[3] = {2, 4, 6};
sum = a[0] + a[1] + a[2];

💡 Correction

🧱 Flattened C


sum = a[0];
sum = sum + a[1];
sum = sum + a[2];

⚙️ Assembleur (NASM)


mov eax, [a]        ; a[0]
add eax, [a + 4]    ; a[1]
add eax, [a + 8]    ; a[2]

🧩 Exercice 7 — Tableau : index variable

🎯 Objectif

Utiliser un registre pour accéder à a[i].

🔹 Énoncé (C)


x = a[i];

💡 Correction

🧱 Flattened C


t1 = i * 4;      // chaque int = 4 octets
x = *(a + t1);

⚙️ Assembleur (NASM)


mov eax, [rdi + rsi*4]

🧱 Exercice 8 — Structures et alignement

🎯 Objectif

Accéder aux champs d’une structure en mémoire.

🔹 Énoncé (C)


struct Point {
    char a;
    int b;
};

p.a = 1;
p.b = 5;

💡 Correction

🧱 Flattened C


p.a = 1;
p.b = 5;    // mais décalé de +4 octets à cause du padding

⚙️ Assembleur (NASM)


mov byte [p], 1        ; champ a
mov dword [p + 4], 5   ; champ b

🧩 Exercice 9 — Little Endian

🎯 Objectif

Observer comment une valeur est stockée en mémoire en Little Endian.

🔹 Énoncé


x = 0x12345678;

💡 Correction

🧱 Flattened C


// mémoire : x = 78 56 34 12

⚙️ Assembleur (NASM)


mov eax, 0x12345678
mov [val], eax
AdresseOctet (hex)
val78
val+156
val+234
val+312

🧠 En Little Endian, l’octet de poids faible est stocké en premier.


🧩 Exercice 10 — Big Endian

🎯 Objectif

Représenter la même valeur dans un ordre Big Endian.

🧱 Flattened C


// mémoire (Big Endian) : 12 34 56 78
AdresseOctet (hex)
val12
val+134
val+256
val+378

Remarque :


✅ Bilan des notions couvertes

SujetExercices
if, else, while, for1 → 4
Entiers signés / non signés5
Tableaux & adressage mémoire6 → 7
Structures & padding8
Little / Big Endian9 → 10