Switch Functions

Empty Function

We start of with a function that is passed no parameters, doesn't perform anything, and returns nothing.

int switch_case(int x) {
    switch (x) {
        case 1:
            return 1;
            break;
        case 2:
        case 3:
            return 23;
        case 8:
            return 8;
        default:
            return -1;
    }
}

This generates the following assembly:

switch_case:
    push    rbp
    mov    rbp, rsp
    mov    DWORD PTR [rbp-4], edi
    mov    eax, DWORD PTR [rbp-4]
    cmp    eax, 3
    jg    .L3
    cmp    eax, 2
    jge    .L4
    cmp    eax, 1
    je    .L5
    jmp    .L2
.L3:
    cmp    eax, 8
    je    .L6
    jmp    .L2
.L5:
    mov    eax, 1
    jmp    .L7
.L4:
    mov    eax, 23
    jmp    .L7
.L6:
    mov    eax, 8
    jmp    .L7
.L2:
    mov    eax, -1
.L7:
    pop    rbp
    ret

results matching ""

    No results matching ""