我正在做一个程序,我可以输入一个数组,然后打印数组的每个元素,然后打印素数。但是我的素数函数有问题,它导致了素数函数中的"lw $s2,($s0)"
行,下面是我的代码:(我希望你能帮我修改我的代码,让它正常运行)
.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
字符串
我需要有人帮助我与我的代码,使它运行的方式应该是正确的
1条答案
按热度按时间up9lanfz1#
这些函数中的一些需要保存和恢复
$ra
-特别是那些(a)使用jal
和(B)返回到其调用者的函数。您的
prime
函数没有初始化$s0
,并且该寄存器也不是参数,因此缺少了初始化它的功能。