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?

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.

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.

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