# fib.ic # global declarations .globl __ic_main # data section .data .align 8 _Fibonacci_VT: .quad _Fibonacci_main .quad _Fibonacci_fib .quad 0 str1Chars: .ascii "" str1: .quad str1Chars .quad 23 strNullPtrErrorChars: .ascii "Null pointer violation." strNullPtrError: .quad strNullPtrErrorChars .quad 23 strArrayBoundsErrorChars: .ascii "Array bounds violation." strArrayBoundsError: .quad strArrayBoundsErrorChars .quad 21 strArraySizeErrorChars: .ascii "Array size violation." strArraySizeError: .quad strArraySizeErrorChars # text (code) section .text #---------------------------------------------------- .align 8 _Fibonacci_main: pushq %rbp # prologue movq %rsp,%rbp pushq %rbx movq 24(%rbp), %rax # s = args cmpq $0, %rax # null pointer check je labelNullPtrError # array bounds check movq -8(%rax), %rbx # ebx = length movq $0, %rcx # ecx = index cmpq %rcx, %rbx jle labelArrayBoundsError # ebx <= ecx ? cmpq $0, %rcx jl labelArrayBoundsError # ecx < 0 ? mov (%rax,%rcx,8), %rdx # s = args[0] movq $-1, %rsi # n = stoi(s,-1) movq %rdx, %rdi call __LIB_stoi pushq %rax # r = fib(n) movq 16(%rbp), %rax pushq %rax movq (%rax), %rax call *8(%rax) addq $8, %rsp movq %rax, %rdi # printi(r) call __LIB_printi movq str1(%rip), %rdi # println("") call __LIB_println _epilogue_Fibonacci_main: popq %rbx # epilogue movq %rbp,%rsp popq %rbp ret #---------------------------------------------------- .align 8 _Fibonacci_fib: pushq %rbp # prologue movq %rsp, %rbp pushq %rbx cmp $2, 24(%rbp) # if (n < 2) jge L1 mov 24(%rbp), %rax # return n jmp _epilogue_Fibonacci_fib L1: mov 24(%rbp), %rax # t1 = fib(n-1) dec %rax pushq %rax movq 16(%rbp), %rax pushq %rax movq (%rax), %rax call *8(%rax) add $16, %rsp movq %rax, %rbx # ebx = t1 movq 24(%rbp), %rax # t2 = fib(n-2) subq $2, %rax pushq %rax movq 16(%rbp), %rax pushq %rax movq (%rax), %rax call *8(%rax) addq $16, %rsp addq %rbx, %rax # return t1+t2 _epilogue_Fibonacci_fib: popq %rbx movq %rbp, %rsp popq %rbp ret #---------------------------------------------------- .align 8 __ic_main: pushq %rbp # prologue movq %rsp,%rbp pushq %rdi # o.main(args) -> push args movq $8, %rdi # o = new Fibonacci call __LIB_allocateObject leaq _Fibonacci_VT(%rip), %rdi movq %rdi, (%rax) pushq %rax # o.main(args) -> push o movq (%rax), %rax call *(%rax) addq $16, %rsp mov $0, %rax # return 0 _epilogue_ic_main: movq %rbp,%rsp # epilogue popq %rbp ret #---------------------------------------------------- .align 8 labelNullPtrError: movq strNullPtrError(%rip), %rdi call __LIB_println movq $1, %rdi call __LIB_exit .align 8 labelArrayBoundsError: movq strArrayBoundsError(%rip), %rdi call __LIB_println movq $1, %rdi call __LIB_exit .align 8 labelArraySizeError: movq strArraySizeError(%rip), %rdi call __LIB_println movq $1, %rdi call __LIB_exit