Skip to the task
Layout
Back to classic view

You are looking at lesson 3. Your place is still lesson 8, task 1: Memory.

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

Why jge if the condition is <?

if (grade < lowest) becomes a cmp and a jump. Which jump, and where does it lead?

Stuck?

jge: a jump over lowest = grade; when grade >= lowest, that is when the condition is not met. The compiler turns the condition around: it jumps when the block must not run.

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