我正在尝试将整数存储到$s0
中,这是在循环标签中。
但问题是我将打印一个整数并将其保存在$s0
.
我做了以下
sw $t7,0($s0)
addi $s0,$s0,4
我认为下面将允许我将每个整数存储在一个插槽中$s0
,但如果我lw
,我只会得到打印的最后一个整数
下面的代码:粗体我尝试存储一个单词,但就像你说的那样它会覆盖这是一个家庭作业问题,所以我很想学习,请提供一些指导,下次我会知道
.data
n1: .asciiz "\n" #ASCII for a new line
.align 2
name: .asciiz "Fibonacci\n\n\n" #Lab Title and Student Name
.align 2
msg1: .asciiz "Fibonacci result " #note the space
.align 2
msg2: .asciiz " is " #note the space
.align 2
**array: .space 20 #define the array**
.text #
.globl main #declare main to be global
main:
la $a0,name #load the ram address of "name" into $a0
li $v0,4 #system call, type 4, print a string, $a0
syscall #call the "OS" to perform operation
li $t2,0 # $t2 :=0; initial value of F(n-2)
li $t1,1 # $t1 :=1; initial value of F(n-1)
li $t4,0 #index, n.
#loop is a local Variable
li $t5,11 #loading immediate preset value for $t5
**la $t7,array #load address (array) into $t7**
loop: addu $t0,$t1,$t2 # F(n) = F(n-1) + F(n-2)
**#Only print the last 5 numbers**
sub $t3,$t5,$t4
li $t6,5
bgt $t3,$t6,Printl
la $a0,msg1 # $a0 := address of message 1
li $v0,4 #system call, type 4, print a string
syscall #call the "OS"
move $a0,$t4 # $t4 contains the current value of n
li $v0,1 #system call, type 1, print an integer $a0
syscall #call the "OS"
la $a0,msg2 # $a0:=address of "msg2"
li $v0,4 #system call, type 4, print a string
syscall #call the "OS"
move $a0,$t0 # $a0 :=$t0, which is F(n)
li $v0,1 #system call, type 1, print an integer $a0
syscall #call the "OS"
**sw $s0, 4($t7)**
la $a0,n1 # $a0 := address of "n1"
li $v0,4 #system call, type 4, print a string
syscall #call the "OS"
addi $t4,1 # $t4 := $t4 + 1; increment n
move $t2,$t1 # $t2 := $t1; old F(n-2) because old F(n-1)
move $t1,$t0 # $t1 := $t0; old F(n-2) because old F(n)
beq $t5,$t4,Exit #Conditional Check | exit
j loop # branch unconditionally to loop ($t3 != 0)
Exit:
la $a0,n1 # $a0 := address of message 1
li $v0,4 #system call, type 4, print a string
syscall #call the "OS"
#la $t7,array
#li $v0,1
#sw $s0, 4($t7)
#syscall
li $v0,10 #system call, type 10, standard exit
syscall #call the "OS"
Printl:
addi $t4,1
move $t2,$t1
move$t1,$t0
j loop