#  FIBONNACCI!  A simple Assembly Program using SPIM
#  The data segment is where variables are usually stored

	.data    
X:	.word 0
Y:	.word 1

# Loop increment variable
loop:	.word 0

#  User prompt, an ASCII string terminated with the null character
prompt:	.asciiz "Enter a number: "
	.align 4

#  A variable to hold a newline character for simplecarriage returns
newline:
	.asciiz "\n"
	.align 4

#  The text segment where programs are usually stored.  Note that "main" is 
#  a required label which links to other code needed for program execution.

	.text              
main:	
	la $a0, prompt		# Load address of prompt in $a0
	li $v0, 4		# Put number for output (4) in $v0
	syscall			# Make system call using $v0 and $a0

	li $v0, 5		# Load number for integer input
	syscall			# Make system call
	sw $v0, loop		# Move input value to variable "loop"

cont:	lw $t3, loop		# Load loop variable into a temp register
	beqz $t3, fini 		# Branch if zero to "fini" label
	lw $t0, X		# Load temp0 with X
	lw $t1, Y 		# Load temp1 with Y
	add $a0, $t0, $t1 	# Store sum of X and Y to $a0
	sw $t1, X		# Store $t1 (Y) to X
	sw $a0, Y		# Store $a0 (sum) to Y

#  Put a 1 in Register $v0 and make system call to print an integer
	li $v0, 1		# Change syscall to integer print
	syscall

	la $a0, newline		# Give address of '\n' character
	li $v0, 4		# Get ready for simple ASCII output
	syscall			# Begin printing

#  Put a 10 in Register $v0 and make system call to exit the program
	addi, $t3, -1		# Decrement counter
	sw $t3, loop		# Save counter
	b cont			# Branch back to "cont" label

#  End routine 
fini:	li $v0, 10		# Load value used for "exit"
	syscall			# Quit program
	

