# ----------------------------------------------------------------------------- # A 64-bit Linux application that writes the first N Fibonacci numbers. # # Assemble and Link: # gcc fib.s # ----------------------------------------------------------------------------- .global main .text main: push %rbx # we have to save this since we use it #Get how many to print from user mov $0, %rax # clear AL (zero FP args in XMM registers) leaq prompt(%rip), %rdi # load format string to proper register call printf # print it # Read int leaq fmt_in(%rip), %rdi # load format string leaq nums(%rip), %rsi # set storage to address of x call scanf # Load value from memory movq nums(%rip), %rsi movq %rsi, %rcx xor %rax, %rax # rax will hold the current number xor %rbx, %rbx # rbx will hold the next number inc %rbx # rbx is originally 1 print: # We need to call printf, but we are using eax, ebx, and ecx. printf # may destroy eax and ecx so we will save these before the call and # restore them afterwards. push %rax # caller-save register push %rcx # caller-save register mov $format, %rdi # set 1st parameter (format) mov %rax, %rsi # set 2nd parameter (current_number) xor %rax, %rax # because printf is varargs # Stack is already aligned because we pushed three 8 byte registers call printf # printf(format, current_number) pop %rcx # restore caller-save register pop %rax # restore caller-save register mov %rax, %rdx # save the current number mov %rbx, %rax # next number is now current add %rdx, %rbx # get the new next number dec %ecx # count down jnz print # if not done counting, do some more pop %rbx # restore rbx before returning ret .data format: .asciz "%20ld\n" fmt_in: .string "%d" prompt: .asciz "Number of Fibonaccis to Print? >" nums: .long 0