I'm still a beginner, so please bear with me;) I'm supposed to write a code that generates the Fibonacci series.
Here is the sample program output:
Please enter n (This program generates 0 to Fn) : 5
0 1 1 2 3 5
So, that's what I did..
.data
in_string: .asciiz "Input a positive integer:\n\n"
out_string: .asciiz "The Fibonacci number is:\n\n"
.text
main:
# print out prompt
li $v0, 4 # system call code for printing string = 4
la $a0, in_string # load address of string to be printed into $a0
syscall # call operating system to perform print operation
# read integer into $s0
li $v0, 5 # system call code for read integer = 5
syscall # call operating system
move $s0, $v0 # value read from keyboard returned in register $v0; transfer to $s0
sw $s0,($sp) # push argument for Fib on stack
addi $sp,$sp,-4 # and decrement stack pointer
jal Fib # jump to subroutine
addi $sp,$sp,4 # increment stack pointer
lw $s1,($sp) # and pop result from stack
# print out prompt
li $v0, 4 # system call code for printing string = 4
la $a0, in_string # load address of string to be printed into $a0
syscall # call operating system
# print out result (stored in $s1)
li $v0, 1 # system call code for printing integer = 1
move $a0, $s1 # move integer to be printed into $a0: $a0 = $s1
syscall # call operating system to perform print
# exit program
li $v0, 10 # system call code for exit = 10
syscall # call operating system
However, this is recursive.. I now read that it is supposed to be a non-recursive implementation. I don't know, I thought that's how you do it. How would I do non-recursive.
It's also supposed to be for positive integers only. I can't compile it on my computer right now, ut I think that's what it is ( at least should be) doing.
Any input is appreciated!!