##Stratis Frangos ## Fib.asm - generates the Fibonacci series ## ## Program which generates the Fibonacci series F(n) = F(n-1) + F(n-2), ## F(n) = 0 if n=0 ## F(n) =1 if n=1 ## It generates it element by element, and stores the elements in memory. ## ## Register Legend ## ## $s0 - points to the begininning of the array ## $t0 - points to the current element of the fibonacci sequence ## $t1 - contains the count of elements ## $t2 - contains the f(n-1) ## $t3 - contains the f(n-2) ## $t4 - contains the SUM ## #------------------------------------------------------------------------------ # Text Section (Program) #------------------------------------------------------------------------------ .text #tells assember program code starts here .globl main #defines label for execution start main: #execution starts here la $s0, arrayF #$s0 will to elements la $t0, arrayF #$t0 contains a the current element of arrayF li $t1, 0 Fib: slti $s1,$t1, 2 bne $s1,$zero, True sub $t7, $t0,4 sub $t8, $t0,8 add $t9, $t7, $t8 bltz $t9,overflow # If $s1 <= 0, this checks if an overflow occured. sw $t9, ($t0) # If no overflow occured, save the sum into last node in arrayF addi $t0, $t0, 4 # Incriment array node addi $t1,$t1,1 # Increment the counter (i=i+1) b Fib # Go back to the begining of fib function True: sw $t1, ($t0) # save the counter into last node in arrayF (n<2) addi $t0, $t0, 4 # Incriment by 1 array node addi $t1,$t1,1 # Increment the counter (i=i+1) j Fib overflow: la $a0, func_overflowed # li $v0, 4 # Print "Function overflowed at:" syscall # move $a0, $t1 # li $v0, 1 # Prints the value at which overflow occured. syscall # exit: li $v0, 10 syscall #Exit the program. #---------------------------------------------------------------------------------- # Data Segment #---------------------------------------------------------------------------------- .data func_overflowed: .asciiz "Overflow occurred when adding element " arrayF: .word 0, 1