Skip to content

Instantly share code, notes, and snippets.

@0x1F602
Created April 7, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x1F602/d3b6762c638b4a6784edd81a80d95aa7 to your computer and use it in GitHub Desktop.
Save 0x1F602/d3b6762c638b4a6784edd81a80d95aa7 to your computer and use it in GitHub Desktop.
; to compile and run:
; nasm -f elf64 -l fizzbuzz.lst fizzbuzz.asm
; gcc -o fizzbuzz fizzbuzz.o
; ./fizzbuzz
global main
extern printf
section .text
main:
mov ecx, 0
xor rax, rax
print:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; print the number
push rax
push rcx
mov rdi, num_format
mov rsi, rcx
xor rax, rax
call printf
pop rcx
pop rax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; end print the number
cmp rcx, 0 ; We don't print fizzbuzz for zero, special case
je handle_newline
push rax
push rcx
push rdx
xor rdx, rdx
mov rax, rcx
mov rcx, 3
idiv rcx
cmp rdx, 0 ; should we display fizz?
jne dont_handle_fizz
mov rcx, rdx
mov rdi, fizz_format
xor rax, rax
call printf ; print fizz
dont_handle_fizz:
pop rdx
pop rcx
pop rax
push rax
push rcx
push rdx
xor rdx, rdx
mov rax, rcx
mov rcx, 5
idiv rcx
cmp rdx, 0 ; should we display buzz?
jne dont_handle_buzz
mov rcx, rdx
mov rdi, buzz_format
xor rax, rax
call printf ; print buzz
dont_handle_buzz:
pop rdx
pop rcx
pop rax
handle_newline:
push rax
push rcx
mov rdi, nl_format
xor rax, rax
call printf ; print newline either way
pop rcx
pop rax
inc ecx
cmp ecx, 101
jne print
ret
num_format:
db "%20d", 9, 0 ; 9 = tab
fizz_format:
db "Fizz", 0
buzz_format:
db "Buzz", 0
nl_format:
db 10, 0 ; 10 = \n newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment