Challenge 9

Index | Up


Table of Contents

1. Original assembly

Original challenge link: Click me.

Even the author says that this challenge is quite easy, so I won't make any assembly notes for this one.

This challenge only included the used labels, so I only had to make some minor changes to fit the nasm syntax.

.LC0:
    .string "error!"
func:
    sub   rsp, 8
    movzx eax, BYTE [rdi]
    cmp   al, 89
    je    .L3
    jle   .L21
    cmp   al, 110
    je    .L6
    cmp   al, 121
    jne   .L2
.L3:
    mov   eax, 1
    add   rsp, 8
    ret
.L21:
    cmp   al, 78
    je    .L6
.L2:
    mov   edi, .LC0
    call  puts
    xor   edi, edi
    call  exit
.L6:
    xor   eax, eax
    add   rsp, 8
    ret

2. C translation

Literal translation preserving the branching.

int func(char* str) {
    if (*str == 'Y') {
        /* .L3 */
        return 1;
    } else if (*str < 'Y') {
        /* .L21 */
        if (*str == 'N') {
            /* .L6 */
            return 0;
        }
    } else if (*str == 'n') {
        /* .L6 */
        return 0;
    } else if (*str != 'y') {
        /* .L2 */
        puts("error!");
        exit(0);
    } else {
        /* .L3 */
        return 1;
    }
}

Simplified into this.

bool func(char* str) {
    if (*str == 'Y' || *str == 'y')
        return true;

    if (*str == 'N' || *str == 'n')
        return false;

    puts("error!");
    exit(0);
}