Skip to content

Instantly share code, notes, and snippets.

@kariya-mitsuru
Last active May 6, 2020 06:17
Show Gist options
  • Save kariya-mitsuru/2d213668b58b38e05faa00d3503961f4 to your computer and use it in GitHub Desktop.
Save kariya-mitsuru/2d213668b58b38e05faa00d3503961f4 to your computer and use it in GitHub Desktop.
x64 Assembly FizzBuzz(nostdlib version)
// vim: ts=8 sw=8
// build and execute
// $ gcc -nostdlib -static -s fizzbuzz.s -o fizzbuzz
// $ ./fizzbuzz
.section .rodata
.Sfizz:
.asciz "Fizz\n"
.Sbuzz:
.asciz "Buzz\n"
.Sfizzbuzz:
.asciz "FizzBuzz\n"
.data
.ds 14
.Snumber:
.asciz "\n"
.text
.global _start
_start:
// loop index
xor %ebp, %ebp
.Lloop:
// increment loop index
inc %ebp
// check multiple of 15
xor %edx, %edx
mov %ebp, %eax
mov $15, %ecx
div %ecx
lea .Sfizzbuzz(%rip), %rsi
test %edx, %edx
je .Loutput
// check multiple of 3
xor %edx, %edx
mov %ebp, %eax
mov $3, %ecx
div %ecx
lea .Sfizz(%rip), %rsi
test %edx, %edx
je .Loutput
// check multiple of 5
xor %edx, %edx
mov %ebp, %eax
mov $5, %ecx
div %ecx
lea .Sbuzz(%rip), %rsi
test %edx, %edx
je .Loutput
// convert number to string
lea .Snumber(%rip), %rsi
mov %ebp, %eax
mov $10, %ecx
.Ltostrloop:
xor %edx, %edx
div %ecx
add $'0', %edx
dec %rsi
mov %dl, (%rsi)
test %eax, %eax
jne .Ltostrloop
.Loutput:
// count output bytes
xor %edx, %edx
.Lcountloop:
mov (%rsi,%rdx), %al
inc %edx
test %al, %al
jne .Lcountloop
dec %edx
// write(1, %rsi, %rdx)
mov $1, %edi
mov $1, %eax
syscall
// check loop condition
cmp $40, %ebp
jl .Lloop
// exit(0)
xor %edi, %edi
mov $60, %eax
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment