[2015-07-15] Challenge #223 [Intermediate] Eel of Fortune

Fixed version using a lookup table, same time complexity.

;; int problem(const char *word, const char *offensive);
problem:
        push bp
        push bx
        push si
        push di
        mov bp, sp
        sub sp, 26              ; char table[26]
        mov di, sp              ; di == table
        mov cx, 13
        xor ax, ax
        rep stosw               ; char table[26] = {0}
        mov si, [bp+12]         ; si == offensive
.table: lodsb                   ; assign table entries for offensive word
        cmp al, 0
        je .scan
        mov di, ax
        mov [bp-26+di-'a'], byte 1
        jmp .table
.scan:  mov si, [bp+10]         ; si = word
        mov bx, [bp+12]         ; bx = offensive
.loop:  mov al, [bx]            ; al == *offensive
        cmp al, 0               ; offensive == 0
        je .done                ; ran out of offensive letters
        mov cl, [si]            ; cl == *word
        cmp al, cl              ; *offensive == *word
        je .both
        cmp cl, 0               ; *word != 0
        je .done                ; ran out of word letters
        movzx di, cl
        cmp [bp-26+di-'a'], byte 0
        jne .done               ; table[*word-'a'] != 0
        jmp .skip
.both:  inc bx                  ; offensive++;
.skip:  inc si                  ; word++;
        jmp .loop
.done:  cmp al, 0
        jne .false
        mov al, 1
        jmp .ret
.false: mov al, 0
.ret:   mov sp, bp
        pop di
        pop si
        pop bx
        pop bp
        ret
/r/dailyprogrammer Thread Parent