Skip to content

Instantly share code, notes, and snippets.

@skejeton
Last active December 25, 2022 09:03
Show Gist options
  • Save skejeton/bd4793cf2edf2ec9d449a780934160f2 to your computer and use it in GitHub Desktop.
Save skejeton/bd4793cf2edf2ec9d449a780934160f2 to your computer and use it in GitHub Desktop.
fizzbuzz in 126 bytes mf
org 0x7C00
bits 16
xor bl, bl
mov di, t_numbuf+6
fizzbuzz:
inc bl
mov si, t_numbuf+7
.again:
dec si
mov al, [si]
inc al
cmp al, '9'+1
mov [si], al
jne .skip
mov byte [si], '0'
jmp .again
.skip:
cmp si, di
cmovle di, si
xor si, si
xor cl, cl
.fizz:
mov cl, 3
call r
.buzz:
mov cl, 5
call r
.num:
test si, si
jne .ok
mov si, di
call prints
.ok:
mov si, t_end
call prints
; delay
mov cl, 0x01
mov ah, 0x86
int 15h
; check if 1 million
cmp byte [t_numbuf], '1'
jl fizzbuzz
finish:
cli
hlt
r:
xor ax, ax
mov al, bl
div cl
test ah, ah
jne p_end
mov si, t_fizz
xor cl, 11b ; tricky way to get offset, (cl = 3 :: add si, 0) (cl = 5, :: add si, 6)
add si, cx
prints:
mov ah, 0x0e
lodsb
.loop:
int 0x10
lodsb
test al, al
jne .loop
p_end:
ret
t_fizz: db "Fizz", 0, 0 ; this extra byte is required for the bitwise trick to work in "r"
t_buzz: db "Buzz", 0
t_end: db 13, 10, 0
t_numbuf: db "0000000", 0
; Without this below (Required for BIOS to boot) results in 126 bytes
times 510-($-$$) db 0
dw 0xAA55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment