Please comment every line of code
I need to write a program using assembly language. I will be using mars 4.5. The assignment for this program is to print out the first 20 fibonacci numbers and the running sum of the numbers as they are displayed.
For example, the output of the program should have first 5 numbers in the series.
Fibonacci series |
Number | total |
1 | 1 |
1 | 2 |
2 | 4 |
3 | 7 |
5 | 12 |
… | … |
… | … |
… | … |
6765 | ???? |
Hint! Here is the skeleton of the code you will need to do this project.
.data
fibs: .word 0 : 20
size: .word 20
prompt: .asciiz “How many fibonacci numbers to generate? (2<= x <= 20)
.text
la $s0, fibs
la $s5, size
lw $s5, 0($s5)
li $s2, 1
sw $s2, 0($s0)
sw $s2, 4($s0)
addi $s1, $s5, -2
loop: lw $s3, 0($s0)
lw $s4, 4($s0)
add $s2, $s3, $s4
sw $s2, 8($s0)
addi $s0, $s0, 4
addi $s1, $s1, -1
bgtz $s1, loop
la $a0, fibs
add $a1, $zero, $s5
li $v0, 10
syscall
Expert Answer
.data
fibs: .word 0 : 20
size: .word 20
prompt: .asciiz “How many fibonacci numbers to generate? (2<= x <= 20)
.text
la $s0, fibs
la $s5, size
lw $s5, 0($s5)
li $s2, 1
sw $s2, 0($s0)
sw $s2, 4($s0)
addi $s1, $s5, -2
loop: lw $s3, 0($s0)
lw $s4, 4($s0)
add $s2, $s3, $s4
sw $s2, 8($s0)
addi $s0, $s0, 4
addi $s1, $s1, -1
bgtz $s1, loop
la $a0, fibs
add $a1, $zero, $s5
li $v0, 10
syscall