Search code examples
assemblymipsprimes

(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


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 "lw $s2,($s0)" 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


Solution

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


    Your prime function doesn't initialize $s0, and that register is also not a parameter, so something is missing to initialize it.