cs24-20fa Conditionals

Introduction to Computing Systems (Fall 2020)

hello

Jumps

jump.c

void f(int x) {
    if (x == 2) {
        puts("it's two");
    }
    else {
        puts("not two");
    }
}

jump.s

f:
    cmp $2, %edi
    jne else
    mov $.str1, %edi
    xor %eax, %eax
    jmp puts
else:
    mov $.str2, %edi
    xor %eax, %eax
    jmp puts


set

set.c

int g(int x) {
    if (x == 2) {
        return 1;
    }
    else {
        return 0;
    }
}

set.s

g:
    xor  %eax, %eax
    cmp  $2, %edi
    sete %al
    retq


test

test.c

int h(int x) {
    if (x == 0) {
        return 1;
    }
    return 0;
}

test.s

h:
    xor  %eax, %eax
    test %edi, %edi
    sete %al
    retq


Conditional Moves

conditional-move.c

int i(int x) {
    if (x == 2) {
        return -2;
    }
    else {
        return x;
    }
}

conditional-move.s

i:
    cmp    $2, %edi
    mov    $-2, %eax
    cmovne %edi, %eax
    retq


Submit QuickChecks For Credit