.title "fib.ic" # global declarations .global _ic_main .global _fib # data section .data .align 4 .int 24 strNPC: .string "Null pointer violation.\n" .align 4 .int 24 strABC: .string "Array bounds violation.\n" .align 4 # text (code) section .text #---------------------------------------------------- .align 4 _ic_main: push %ebp # prologue mov %esp,%ebp # prologue push %ebx # prologue mov 8(%ebp), %eax # s = args # null pointer checking cmp $0, %eax je labelNPC # array bounds checking mov -4(%eax), %ebx # ebx = length mov $0, %ecx # ecx = index cmp %ecx, %ebx jle labelABC # ebx <= ecx ? cmp $0, %ecx jl labelABC # ecx < 0 ? mov (%eax,%ecx,4), %edx # s = args[0] # stoi function call push $0 push %edx call _stoi add $8, %esp # fib function call push %eax # pre-call call _fib add $4, %esp # post-call # print result push %eax # pre-call call _printi add $4, %esp # post-call # print new line push $10 # pre-call call _printc add $4, %esp # post-call xor %eax, %eax # return 0 epilogue_ic_main: pop %ebx # epilogue mov %ebp,%esp # epilogue pop %ebp # epilogue ret # epilogue #---------------------------------------------------- .align 4 _fib: push %ebp # prologue mov %esp, %ebp # prologue push %ebx # prologue cmp $2, 8(%ebp) # if (n < 2) jge L1 mov 8(%ebp), %eax # return n jmp epilogue_fib L1: mov 8(%ebp), %eax # t1 = fib(n-1) dec %eax push %eax call _fib add $4, %esp mov %eax, %ebx # ebx = t1 mov 8(%ebp), %eax # t2 = fib(n-2) sub $2, %eax push %eax call _fib add $4, %esp add %ebx, %eax # return t1+t2 epilogue_fib: pop %ebx # epilogue mov %ebp, %esp # epilogue pop %ebp # epilogue ret # epilogue #---------------------------------------------------- .align 4 labelNPC: push $strNPC call __icabort .align 4 labelABC: push $strABC call __icabort