在“_prime”函数中使用“lw”时出现了问题,它没有按照我想的方式运行。

huangapple go评论48阅读模式
英文:

(first time, so sorry if i make some mistakes )something wrong when i use 'lw' in "_prime" func, it didnt run the same way as i think

问题

我在做一个程序,可以输入一个数组,然后打印数组的每个元素,接着打印素数。但是我的素数函数有问题,是由于素数函数中的这行代码 `"lw $s2,($s0)"`引起的。以下是我的代码:(感谢您的阅读,希望您能帮我修复我的代码,使其正常运行)


<details>
<summary>英文:</summary>

im doing a program that i can input an array and then print every element of the array , and print the prime after that. But something is wrong with my prime function, it causes by the line `&quot;lw $s2,($s0)&quot;` in the prime function, here is my code: (ty for reading and i hope that u can help me fix my code and make it run normally)

.data
arr: .space 100
input1: .asciiz "Nhap so phan tu: " #input the number elements of the array
input2: .asciiz "Nhap mang: \n" #input the array
output1: .asciiz "Mang vua nhap: " #print the array
output2: .asciiz "\nCac so nguyen to la: " #print the prime in the array
output3: .asciiz "\nGia tri lon nhat trong mang: " #print the max element in the array
output4: .asciiz "\nGia tri trung binh cua mang la: " #print the average value in the array
output5: .asciiz "a["
output6: .asciiz "]: "
.text
#print input1
li $v0,4
la $a0,input1
syscall
#input n
li $v0,5
syscall
#store n into $s1
move $s1,$v0

#passing parameter into _inputarr
move $a1,$s1
la $a0,arr
jal _inputarr
#store return value into $s0
move $s0,$v0

#passing parameter into _inputarr
move $a1,$s1
move $a0,$s0

jal _outputarr

#passing parameter into _printprime
move $a1,$s1
move $a0,$s0
jal _printprime

j exit

_inputarr:
#initialize stack
addi $sp,$sp,-32

#back up register
sw $s1,($sp)
sw $s0,4($sp)
sw $t0,8($sp)

#func's body
#initialize variable
move $s0,$a0
move $s1,$a1
la $t0,0 #i=0
move $s2,$a0

#print input2
li $v0,4
la $a0,input2
syscall

_inputarr.loop:
beq $t0,$s1,_inputarr.end
#print 'a['
li $v0,4
la $a0,output5
syscall
#print i
li $v0,1
move $a0,$t0
syscall
#print '] '
li $v0,4
la $a0,output6
syscall
#input a[i]
li $v0,5
syscall
sw $v0,($s0)
addi $t0,$t0,1
addi $s0,$s0,4
j _inputarr.loop
_inputarr.end:
move $v0,$s2

#restore register
lw $s1,($sp)
lw $s0,4($sp)
lw $t0,8($sp)
#restore stack
addi $sp,$sp,32
#return 
jr $ra

_outputarr:
#initialize stack
addi $sp,$sp,-32

#back up register
sw $s1,($sp)
sw $s0,4($sp)
sw $t0,8($sp)

#func's body
#initialize variable
li $t0,0 #i=0
move $s0,$a0
move $s1,$a1

#print output1
li $v0,4
la $a0,output1
syscall

_outputarr.loop:
beq $t0,$s1,_outputarr.end
lw $a0,($s0)
li $v0,1
syscall
#print space
li $v0,11
li $a0,' '
syscall
addi $t0,$t0,1
addi $s0,$s0,4
j _outputarr.loop
_outputarr.end:
#restore register
lw $s1,($sp)
lw $s0,4($sp)
lw $t0,8($sp)
#restore stack
addi $sp,$sp,32
#return
jr $ra

_printprime:
#initialize stack
addi $sp,$sp,-32

#back up register
sw $s0,($sp)
sw $s1,4($sp)
sw $t0,8($sp)
sw $t8,12($sp)
sw $a0,16($sp)
sw $t1,20($sp)

#func's body
#initialize variable
move $s0,$a0
move $s1,$a1
li $t0,0 #i=0
#print output2
li $v0,4
la $a0,output2
syscall
_printprime.loop:
beq $t0,$s1,_printprime.end
move $a0,$s0
jal _prime
#store return value
move $t8,$v0

addi $t0,$t0,1
addi $s0,$s0,4
beq $t8,0,_printprime.loop
beq $t8,1,_printprime.print

_printprime.print:
lw $a0,($s0)
li $v0,1
syscall
li $v0,11
la $a0,' '
syscall
j _printprime.loop
_printprime.end:
#restore register
lw $s0,($sp)
lw $s1,4($sp)
lw $t0,8($sp)
lw $t8,12($sp)
lw $a0,16($sp)
addi $sp,$sp,32
#return
jr $ra

_prime:

#initialize stack
addi $sp,$sp,-32
#back up register
sw $s0,0($sp)
sw $s1,4($sp)
sw $t0,8($sp)
sw $t8,12($sp)
sw $s2,16($sp)
sw $t9,20($sp)

#func's body
#initialize variable
li $t8,1
move $a0,$s0
li $t0,2 # i = 2
div $s0,$t0 #divine n by 2 (use $t0 as a dividend because it currently contains the value 2.)
mflo $s1 #$s0 = n/2
addi $s1,$s1,1
lw $s2,($s0)
beq $s2,1,_prime.false # 1 isnt a prime
_prime.loop:
sle $t8,$s1,$t0
beq $t8,1,_prime.true
div $s2,$t0
mfhi $t9
beq $t9,0,_prime.false
addi $t0,$t0,1
j _prime.loop
_prime.false:
li $v0,0
j _prime.end
_prime.true:
li $v0,1
#end of func
_prime.end:
#restore register

lw $s0,0($sp)
lw $s1,4($sp)
lw $t0,8($sp)
lw $t8,12($sp)
lw $s2,16($sp)
lw $t9,20($sp)
#restore stack
addi $sp,$sp,32
#return
jr $ra

exit:
li $v0,10


i need someone help me with my code and make it run right the way it should be

</details>


# 答案1
**得分**: 0

一些这些函数需要保存和恢复`$ra`,特别是那些(a)使用`jal`的和(b)返回给它们的调用者的函数。

---

您的`prime`函数没有初始化`$s0`,而且那个寄存器也不是一个参数,所以缺少一些初始化它的东西。

<details>
<summary>英文:</summary>

Some of these functions need to save &amp; restore `$ra` &amp;#8212; in particular, the ones that (a) use `jal` and (b) return to their caller.

---

Your `prime` function doesn&#39;t initialize `$s0`, and that register is also not a parameter, so something is missing to initialize it.


</details>



huangapple
  • 本文由 发表于 2023年7月17日 23:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定