Reputation: 185
I have to create a mips code which it has to tell me if a random number (given by the user) is in the array (1) or its not (0). I have two problems, the first one is that the random number that I´m trying to ask to the user (li $v0, 5 -- syscall -- move $t0, $v0) its not working, in fact it does nothing when I assemble it. The second problem is weird, I have 2 loops (target) and (exit loop), the first one to check if a number is on any of the array position (10 positions) and the second to exit with a 0 if it didn't find the number. Any ideas?
iterator = 4
N = 10
.data
vector: .word 2, 3, 5, 6, 8, 1, 3, 2, 5, 9
.text
main:
lw $t2, buscador
li $t1, iterator
la $s1, vector
move $s0, $zero
li $v0, 5
syscall
move $t0, $v0
target:
bgt $s0, N, exit_loop
mul $t3, $s0, $t1
addu $t3, $t3, $s1
lw $t3, 0($t3)
addi $s0, $s0, 1
bne $t0, $t3, target
li $s2, 1
move $a0, $s2
li $v0, 1
syscall
exit_loop:
li $s2, 0
move $a0, $s2
li $v0, 1
syscall
li $v0, 10
syscall
Upvotes: 1
Views: 118
Reputation: 33631
There appears to be nothing wrong with your program.
The symbol buscador
was not defined, so the program may not have assembled correctly. I commented out the given line. This may have been the only problem.
I did some slight cosmetic cleanup and added some annotations. I also simplified the print integer syscalls [yours would have worked, as is].
I did add an extra jump to a new label exit_program
to keep the pass/fail cases separate. Otherwise, you'd have a "fall through" from the pass case to the fail case and you'd get "10" instead of "1" for the pass case.
But, otherwise, I did not (i.e. did not need to) change your logic.
I tested the program and it works on both QtSpim
and mars
.
Anyway, just to be sure, here's a working version:
iterator = 4
N = 10
.data
vector: .word 2, 3, 5, 6, 8, 1, 3, 2, 5, 9
.text
main:
# NOTE: this symbol was _not_ defined, so spim may have silently not
# run the program
###lw $t2,buscador
li $t1,iterator # get sizeof(vector[0])
la $s1,vector # get vector base address
move $s0,$zero # set vector index to zero
# prompt user for number
li $v0,5
syscall
move $t0,$v0
target:
bgt $s0,N,exit_loop # vector end? if yes, fly
mul $t3,$s0,$t1 # offset = index * iterator
addu $t3,$t3,$s1 # current addr = base + offset
lw $t3,0($t3) # fetch word from vector
addi $s0,$s0,1 # increment the vector index
bne $t0,$t3,target # does value match? if no, loop
# value matches -- output a 1
li $a0,1
li $v0,1
syscall
j exit_program
# we did _not_ find a match -- output a 0
exit_loop:
li $a0,0
li $v0,1
syscall
exit_program:
li $v0,10
syscall
Upvotes: 0