Created
May 6, 2020 07:37
-
-
Save kariya-mitsuru/d252f5820ee8bcb500e9ac31f6cc44c3 to your computer and use it in GitHub Desktop.
x64 Assembly FizzBuzz(286bytes version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: ts=8 sw=8 | |
# build and execute | |
# $ gcc -mx32 -nostdlib -static -s -Wl,--build-id=none,-Ttext,0x400094 fizzbuzz.s | |
# $ dd if=a.out of=fizzbuzz count=286 bs=1 | |
# $ chmod +x fizzbuzz | |
# $ ./fizzbuzz | |
.bss | |
.ds 15 | |
.Snumber: | |
.ds 1 | |
.text | |
.Sfizzbuzz: | |
.ascii "Fizz" | |
.Sbuzz: | |
.ascii "Buzz\n" | |
.SbuzzLen = . - .Sbuzz | |
.SfizzbuzzLen = . - .Sfizzbuzz | |
.Sfizz: | |
.ascii "Fizz\n" | |
.SfizzLen = . - .Sfizz | |
.global _start | |
_start: | |
lea .Snumber(%rip), %r8 | |
movb $'\n', (%r8) | |
# loop index | |
xor %ebp, %ebp | |
# divisor | |
xor %ebx, %ebx | |
.Lloop: | |
# increment loop index | |
inc %ebp | |
# check multiple of 15 | |
mov $15, %bl # mov $15, %ebx | |
xor %edx, %edx | |
mov %ebp, %eax | |
div %ebx | |
test %edx, %edx | |
lea .Sfizzbuzz(%rip), %rsi | |
mov $.SfizzbuzzLen, %dl # mov $.SfizzbuzzLen, %edx | |
je .Loutput | |
# check multiple of 5 | |
mov $5, %bl # mov $5, %ebx | |
xor %edx, %edx | |
mov %ebp, %eax | |
div %ebx | |
test %edx, %edx | |
lea .Sbuzz-.Sfizzbuzz(%rsi), %rsi # lea .Sbuzz(%rip), %rsi | |
mov $.SbuzzLen, %dl # mov $.SbuzzLen, %edx | |
je .Loutput | |
# check multiple of 3 | |
mov $3, %bl # mov $3, %ebx | |
xor %edx, %edx | |
mov %ebp, %eax | |
div %ebx | |
test %edx, %edx | |
lea .Sfizz-.Sbuzz(%rsi), %rsi # lea .Sfizz(%rip), %rsi | |
mov $.SfizzLen, %dl # mov $.SfizzLen, %edx | |
je .Loutput | |
# convert number to string | |
mov %r8, %rsi # lea .Snumber(%rip), %rsi | |
mov %ebp, %eax | |
mov $10, %bl # mov $10, %ebx | |
.Ltostrloop: | |
xor %edx, %edx | |
div %ebx | |
add $'0', %dl | |
dec %rsi | |
mov %dl, (%rsi) | |
test %eax, %eax | |
jne .Ltostrloop | |
# calculate # of bytes for output | |
lea 1(%r8), %rdx # lea .Snumber+1(%rip), %rdx | |
sub %esi, %edx | |
.Loutput: | |
# write(1, %rsi, %rdx) | |
xor %eax, %eax | |
inc %eax # mov $1, %eax | |
mov %eax, %edi # mov $1, %edi | |
syscall | |
# check loop condition | |
cmp $40, %ebp | |
jl .Lloop | |
# exit(0) | |
xor %edi, %edi | |
xor %eax, %eax | |
mov $60, %al # mov $60, %eax | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment