Created
December 5, 2019 14:14
-
-
Save ioquatix/428e257fda3326a741cd262f25593079 to your computer and use it in GitHub Desktop.
Fizz Buzz X86-64 (amd64) assembly (works on Linux)
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
.text | |
.global main | |
main: | |
# Save the non-volatile registers: | |
pushq %r15 | |
pushq %r14 | |
pushq %r13 | |
pushq %r12 | |
pushq %rbx | |
# Initialize the loop counter: | |
movl $1, %ebx | |
# Load the string literal addresses: | |
leaq FizzBuzz(%rip), %r14 | |
leaq Fizz(%rip), %r15 | |
leaq Buzz(%rip), %r12 | |
leaq Format(%rip), %r13 | |
Top: | |
# Compute i % 3 and i % 5: | |
movzbl %bl, %eax | |
imull $205, %eax, %ecx | |
shrl $10, %ecx | |
leal (%rcx,%rcx,4), %edx | |
imull $171, %eax, %eax | |
shrl $9, %eax | |
leal (%rax,%rax,2), %eax | |
negl %eax | |
movzbl %al, %ecx | |
addl %ebx, %ecx | |
movl %ebx, %eax | |
subl %edx, %eax | |
movl %ecx, %edx | |
# Decide what to print: | |
orb %al, %dl | |
je PutsFizzBuzz | |
testb %cl, %cl | |
je PutsFizz | |
testb %al, %al | |
je PutsBuzz | |
movq %r13, %rdi | |
movl %ebx, %esi | |
xorl %eax, %eax | |
callq printf | |
jmp Next | |
PutsFizzBuzz: | |
movq %r14, %rdi | |
jmp CallPuts | |
PutsFizz: | |
movq %r15, %rdi | |
jmp CallPuts | |
PutsBuzz: | |
movq %r12, %rdi | |
CallPuts: | |
callq puts | |
Next: | |
incl %ebx | |
cmpl $101, %ebx | |
jne Top | |
# Set the return valueto 0: | |
xorl %eax, %eax | |
# Restore the non-volatile registers: | |
popq %rbx | |
popq %r12 | |
popq %r13 | |
popq %r14 | |
popq %r15 | |
# Return: | |
retq | |
Format: | |
.asciz "%d\n" | |
Buzz: | |
.asciz "Buzz" | |
Fizz: | |
.asciz "Fizz" | |
FizzBuzz: | |
.asciz "FizzBuzz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was modified from compiling
To produce the initial assembly:
To compile the above assembly (in a file called
fizzbuzz.s
):