Skip to content

Instantly share code, notes, and snippets.

@craigjb
Created May 4, 2012 21:01
Show Gist options
  • Save craigjb/2597706 to your computer and use it in GitHub Desktop.
Save craigjb/2597706 to your computer and use it in GitHub Desktop.
x64 System V Assembly FizzBuzz
/*
FizzBuzz written in x64 Assembly
Uses System V ABI (so Mac, Linux, Unix)
To Compile:
gcc -o fizzbuzz fizzbuzz.s
Written by Craig Bishop
5/4/2012
*/
.data
_num:
.asciz "%i\n"
_fizz:
.asciz "Fizz\n"
_buzz:
.asciz "Buzz\n"
_fizzbuzz:
.asciz "FizzBuzz\n"
.text
.globl _main
_main:
sub $8, %rsp
mov $1, %r8
_loop:
mov $0, %r10
mov $0, %rdx
mov %r8, %rax
mov $3, %rsi
div %esi
cmp $0, %rdx
jne _skip1
add $1, %r10
_skip1:
mov $0, %rdx
mov %r8, %rax
mov $5, %rsi
div %esi
cmp $0, %rdx
jne _skip2
add $2, %r10
_skip2:
cmp $0, %r10
je _printNum
cmp $2, %r10
jb _printFizz
je _printBuzz
ja _printFizzBuzz
_printNum:
lea _num(%rip), %rdi
jmp _loope
_printFizz:
lea _fizz(%rip), %rdi
jmp _loope
_printBuzz:
lea _buzz(%rip), %rdi
jmp _loope
_printFizzBuzz:
lea _fizzbuzz(%rip), %rdi
_loope:
mov $1, %rax
mov %r8, %rsi
push %r8
push %r10
call _printf
pop %r10
pop %r8
add $1, %r8
cmp $101, %r8
jne _loop
_end:
add $8, %rsp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment