Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Conditions.

READTask 18 of 22 · step 8: Extra: how does the CPU see it?How does the CPU see it?
How does the CPU see it? in practice/03/

Extra: to the processor, if, loops and switch are comparisons (cmp) and jumps (jmp, jle, jge…). Find them in the assembly of practice/03/main.c.

Numbered notes explain the assembly lines.

Details & hint

Extra. Not graded. How does the processor run if, loops and switch?

Open the tool window's Assembly tab (topic 2's extra step explains how to read it). On the left is your practice/03/main.c.

Jumps instead of blocks

The processor has no if and no while. It has two commands:

  • cmp a, b compares two numbers and remembers the result;
  • a jump: "carry on not with the next line but at this label". jmp always jumps, while jl, jle, jg, jge, je, jne only jump when the last comparison was <, ≤, >, ≥, =, ≠ respectively.

Labels such as .L18: are places to jump to.

What to notice (-O0)

  • if (grade < lowest) becomes cmp and jge: a jump over the assignment when the condition is not met. The compiler turns the condition around: it has to jump when lowest = grade; must not run.
  • The for loop: the assignment i = 1, a jump down to the condition, and the condition (cmp and jle back up) sits after the loop body. i++ is the add ..., 1 just before the condition.
  • grade < 5 becomes cmp ..., 4 and jg: for integers, "< 5" is the same as "≤ 4".
  • The switch becomes a jump table: 5 is subtracted from grade, and the number 0–5 picks one of six addresses in the table. One check instead of six comparisons.

Switch to -O1: i, sum, lowest and the rest live in registers, so the loop is shorter and the order of the C lines mixes in the instructions.

practice 03 main.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
#include "input.h"
int main(void) {
int count = read_int("How many grades? ");
while (count < 1) {
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
int lowest = 10;
int highest = 1;
int failed = 0;
for (int i = 1; i <= count; i++) {
printf("%d. ", i);
int grade = read_int("Grade: ");
while (grade < 1 || grade > 10) {
printf("Grade must be between 1 and 10.\n");
grade = read_int("Grade: ");
}
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
printf("Unsatisfactory\n");
break;
}
sum += grade;
if (grade < lowest) {
lowest = grade;
}
if (grade > highest) {
highest = grade;
}
if (grade < 5) {
failed++;
}
}
double average = (double)sum / count;
printf("Average: %.2f\n", average);
printf("Lowest: %d, highest: %d\n", lowest, highest);
printf("Failed: %d\n", failed);
return 0;
}
opened for this task

Extra · x86-64 · Intel syntax · Windows (MinGW GCC)

-O0 -O1

C source main.c

#include <stdio.h>
#include "input.h"
int main(void) {
int count = read_int("How many grades? ");
while (count < 1) {
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
int lowest = 10;
int highest = 1;
int failed = 0;
for (int i = 1; i <= count; i++) {
printf("%d. ", i);
int grade = read_int("Grade: ");
while (grade < 1 || grade > 10) {
printf("Grade must be between 1 and 10.\n");
grade = read_int("Grade: ");
}
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
printf("Unsatisfactory\n");
break;
}
sum += grade;
if (grade < lowest) {
lowest = grade;
}
if (grade > highest) {
highest = grade;
}
if (grade < 5) {
failed++;
}
}
double average = (double)sum / count;
printf("Average: %.2f\n", average);
printf("Lowest: %d, highest: %d\n", lowest, highest);
printf("Failed: %d\n", failed);
return 0;
}

x86-64 assembly -O0

.LC0:
.ascii "How many grades? \0"
.LC1:
.ascii "At least one grade is needed.\12\0"
.LC2:
.ascii "%d. \0"
.LC3:
.ascii "Grade: \0"
.LC4:
.ascii "Grade must be between 1 and 10.\12\0"
.LC5:
.ascii "Excellent\12\0"
.LC6:
.ascii "Very good\12\0"
.LC7:
.ascii "Good\12\0"
.LC8:
.ascii "Fair\12\0"
.LC9:
.ascii "Satisfactory\12\0"
.LC10:
.ascii "Weak\12\0"
.LC11:
.ascii "Unsatisfactory\12\0"
.LC12:
.ascii "Average: %.2f\12\0"
.LC13:
.ascii "Lowest: %d, highest: %d\12\0"
.LC14:
.ascii "Failed: %d\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 80
call __main
lea rcx, .LC0[rip]
call read_int
mov DWORD PTR -4[rbp], eax
jmp .L4
.L5:
lea rcx, .LC1[rip]
call printf
lea rcx, .LC0[rip]
call read_int
mov DWORD PTR -4[rbp], eax
.L4:
cmp DWORD PTR -4[rbp], 0
jle .L5
mov DWORD PTR -8[rbp], 0
mov DWORD PTR -12[rbp], 10
mov DWORD PTR -16[rbp], 1
mov DWORD PTR -20[rbp], 0
mov DWORD PTR -24[rbp], 1
jmp .L6
.L21:
mov eax, DWORD PTR -24[rbp]
mov edx, eax
lea rcx, .LC2[rip]
call printf
lea rcx, .LC3[rip]
call read_int
mov DWORD PTR -28[rbp], eax
jmp .L7
.L8:
lea rcx, .LC4[rip]
call printf
lea rcx, .LC3[rip]
call read_int
mov DWORD PTR -28[rbp], eax
.L7:
cmp DWORD PTR -28[rbp], 0
jle .L8
cmp DWORD PTR -28[rbp], 10
jg .L8
mov eax, DWORD PTR -28[rbp]
sub eax, 5
Note 1: switch: 5–10 become 0–5, a row of the jump table.
cmp eax, 5
ja .L9
mov eax, eax
lea rdx, 0[0+rax*4]
lea rax, .L11[rip]
mov eax, DWORD PTR [rdx+rax]
cdqe
lea rdx, .L11[rip]
add rax, rdx
jmp rax
.L11:
.long .L16-.L11
.long .L15-.L11
.long .L14-.L11
.long .L13-.L11
.long .L12-.L11
.long .L10-.L11
.L10:
lea rcx, .LC5[rip]
call printf
jmp .L17
.L12:
lea rcx, .LC6[rip]
call printf
jmp .L17
.L13:
lea rcx, .LC7[rip]
call printf
jmp .L17
.L14:
lea rcx, .LC8[rip]
call printf
jmp .L17
.L15:
lea rcx, .LC9[rip]
call printf
jmp .L17
.L16:
lea rcx, .LC10[rip]
call printf
jmp .L17
.L9:
lea rcx, .LC11[rip]
call printf
nop
.L17:
mov eax, DWORD PTR -28[rbp]
add DWORD PTR -8[rbp], eax
mov eax, DWORD PTR -28[rbp]
cmp eax, DWORD PTR -12[rbp]
jge .L18
Note 2: if (grade < lowest): a jump over the assignment when it is false.
mov eax, DWORD PTR -28[rbp]
mov DWORD PTR -12[rbp], eax
.L18:
mov eax, DWORD PTR -28[rbp]
cmp eax, DWORD PTR -16[rbp]
jle .L19
mov eax, DWORD PTR -28[rbp]
mov DWORD PTR -16[rbp], eax
.L19:
cmp DWORD PTR -28[rbp], 4
Note 3: grade < 5 becomes cmp 4 and jg: "< 5" is the same as "<= 4".
jg .L20
add DWORD PTR -20[rbp], 1
.L20:
add DWORD PTR -24[rbp], 1
.L6:
mov eax, DWORD PTR -24[rbp]
cmp eax, DWORD PTR -4[rbp]
Note 4: The for condition sits after the loop body; jle jumps back up.
jle .L21
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR -8[rbp]
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR -4[rbp]
divsd xmm0, xmm1
movsd QWORD PTR -40[rbp], xmm0
movsd xmm0, QWORD PTR -40[rbp]
mov rax, QWORD PTR -40[rbp]
movupd xmm1, xmm0
mov rdx, rax
lea rcx, .LC12[rip]
call printf
mov edx, DWORD PTR -16[rbp]
mov eax, DWORD PTR -12[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC13[rip]
call printf
mov eax, DWORD PTR -20[rbp]
mov edx, eax
lea rcx, .LC14[rip]
call printf
mov eax, 0
leave
ret