Skip to the task
Layout
Back to classic view

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

CHECKTask 20 of 22 · step 8: Extra: how does the CPU see it?

Why does the switch subtract 5?

Before the jump table there are sub eax, 5 and cmp eax, 5. What do they do?

Stuck?

The cases 5–10 become the numbers 0–5, the row of the table. Everything above 5 (and grades below 5, which become "very large" unsigned numbers after the subtraction) jumps to default. # ── topic 5 (lesson 42, step 9): practice/05/recursion.c ────────────

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 -O1

.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 r15
push r14
push r13
push r12
push rbp
push rdi
push rsi
push rbx
sub rsp, 56
call __main
lea rcx, .LC0[rip]
call read_int
mov ebp, eax
test eax, eax
jg .L4
lea rsi, .LC1[rip]
lea rbx, .LC0[rip]
.L5:
mov rcx, rsi
call printf
mov rcx, rbx
call read_int
mov ebp, eax
test eax, eax
jle .L5
.L4:
mov r12d, 1
mov DWORD PTR 44[rsp], 0
mov r14d, 1
mov r13d, 10
mov r15d, 0
lea rdi, .LC4[rip]
jmp .L18
.L9:
lea rcx, .LC5[rip]
call printf
.L16:
add r15d, ebx
cmp r13d, ebx
cmovg r13d, ebx
cmp r14d, ebx
cmovl r14d, ebx
jmp .L17
.L11:
lea rcx, .LC6[rip]
call printf
jmp .L16
.L12:
lea rcx, .LC7[rip]
call printf
jmp .L16
.L13:
lea rcx, .LC8[rip]
call printf
jmp .L16
.L14:
lea rcx, .LC9[rip]
call printf
jmp .L16
.L15:
lea rcx, .LC10[rip]
call printf
jmp .L16
.L8:
lea rcx, .LC11[rip]
call printf
add r15d, ebx
cmp r13d, ebx
cmovg r13d, ebx
cmp r14d, ebx
cmovl r14d, ebx
cmp ebx, 4
jg .L17
add DWORD PTR 44[rsp], 1
.L17:
add r12d, 1
cmp ebp, r12d
jl .L25
.L18:
mov edx, r12d
lea rcx, .LC2[rip]
call printf
lea rcx, .LC3[rip]
call read_int
mov ebx, eax
lea eax, -1[rax]
cmp eax, 9
jbe .L6
lea rsi, .LC3[rip]
.L7:
mov rcx, rdi
call printf
mov rcx, rsi
call read_int
mov ebx, eax
lea eax, -1[rax]
cmp eax, 9
ja .L7
.L6:
lea eax, -5[rbx]
cmp eax, 5
ja .L8
mov eax, eax
lea rdx, .L10[rip]
movsx rax, DWORD PTR [rdx+rax*4]
add rax, rdx
jmp rax
.L10:
.long .L15-.L10
.long .L14-.L10
.long .L13-.L10
.long .L12-.L10
.long .L11-.L10
.long .L9-.L10
.L25:
pxor xmm1, xmm1
cvtsi2sd xmm1, r15d
pxor xmm0, xmm0
cvtsi2sd xmm0, ebp
divsd xmm1, xmm0
movq rdx, xmm1
lea rcx, .LC12[rip]
call printf
mov r8d, r14d
mov edx, r13d
lea rcx, .LC13[rip]
call printf
mov edx, DWORD PTR 44[rsp]
lea rcx, .LC14[rip]
call printf
mov eax, 0
add rsp, 56
pop rbx
pop rsi
pop rdi
pop rbp
pop r12
pop r13
pop r14
pop r15
ret