cs24-20fa Loops

Introduction to Computing Systems (Fall 2020)

hello

For Loop Elaboration

for.c

int f() {
    for (int i = 0; i < 100; i++) {
        x += i;
    }
    return x;
}

while.c

int f() {
    int x = 0;
    int i = 0;
    while (i < 100) {
        x += i;
        i++;
    }
    return x;
}


While Loop Translation

while.c

int f() {
    int x = 0;
    int i = 0;
    while (i < 100) {
        x += i;
        i++;
    }
    return x;
}

while.s

f:
    mov $0, %edx
    mov $0, %eax
    jmp test
body:
    add %edx, %eax
    add $1, %edx
test:
    cmp $99, %edx
    jle body
    rep ret


Submit QuickChecks For Credit