Skip to content

Instantly share code, notes, and snippets.

@luavixen
Created October 31, 2019 03:03
Show Gist options
  • Save luavixen/2d28aaa6e12bb6cc7ab272fb7a667c2a to your computer and use it in GitHub Desktop.
Save luavixen/2d28aaa6e12bb6cc7ab272fb7a667c2a to your computer and use it in GitHub Desktop.
FizzBuzz in x86 NASM, now faster! No `call`s!
bits 32
cpu 386
global _start
%define count 1073741824
section .data
fizzMsg db "Fizz!", 0x0A
fizzLen equ $ - fizzMsg
buzzMsg db "Buzz!", 0x0A
buzzLen equ $ - buzzMsg
fizzBuzzMsg db "FizzBuzz!", 0x0A
fizzBuzzLen equ $ - fizzBuzzMsg
section .text
_start:
nop
main:
inc esi
cmp esi, count
jg exit
test_3:
mov eax, esi
mov ebx, 3
xor edx, edx
div ebx
test edx, edx
jnz test_5
;jmp test_3_and_5
test_3_and_5:
mov eax, esi
mov ebx, 5
xor edx, edx
div ebx
test edx, edx
jz out_fizzbuzz
jmp out_fizz
test_5:
mov eax, esi
mov ebx, 5
xor edx, edx
div ebx
test edx, edx
jz out_buzz
test_fallthrough:
;jmp out_number
out_number:
mov eax, esi
;xor edi, edi
push 0x0A
;inc edi
mov edi, 1
out_number_find:
xor edx, edx
mov ebx, 10
div ebx
add edx, 48
push edx
out_number_find_loop:
inc edi
cmp eax, 0
jz out_number_print
jmp out_number_find
out_number_print:
cmp edi, 0
jz out_number_done
dec edi
mov ecx, esp
mov edx, 1
mov ebx, 1
mov eax, 4
int 0x80
add esp, 4
jmp out_number_print
out_number_done:
jmp main
out_fizz:
mov edx, fizzLen
mov ecx, fizzMsg
mov ebx, 1
mov eax, 4
int 0x80
jmp main
out_buzz:
mov edx, buzzLen
mov ecx, buzzMsg
mov ebx, 1
mov eax, 4
int 0x80
jmp main
out_fizzbuzz:
mov edx, fizzBuzzLen
mov ecx, fizzBuzzMsg
mov ebx, 1
mov eax, 4
int 0x80
jmp main
exit:
mov eax, 1
int 0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment