'MIPS string search function
I've been trying to code a search function in MIPS to find a specific string inside of an array and I'm failing miserably.
So far I have
Find:
#offset index
addi $s0, $s0, 4
addi $s6, $s6, 127 # Set limit of the search
la $t0, string2 # .space 200000
Find1:
lw $t0, myArray($s0)
addi $s0, $s0, 32
addi $t1, $t1, 1
beq $t1, $s6, None # Search limit
beqz $t0, Find1
Find2:
# prompt the user
la $a0,prompt2
li $v0,4
syscall
# read in the string
move $a0,$s2
li $a1,20
li $v0,8
syscall
cmploop:
lb $t5,($s2) # get next char from str1 / user
lb $t2,($t0) # get next char from str2 / table
#bne $t2,$t5,Find1 # are they different? if yes, go back
li $v0, 11
move $a0, $t2
syscall
li $v0, 4
move $a0, $t0
syscall
j main
None:
li $v0, 4
la $a0, none
syscall
j main
Which should compare string from the user and the table character by character but for some reason, its only comparing characters from the user input by itself.
Am I missing something?
Thanks
Edit 1:
The premise of the program is a memory allocation/ de-allocation program. As of now, I only have the allocation part working and I'm trying to identify allocated memory inside the array by using a string comparison.
- Program will allocate 32 bytes at a time
Full code :
.data
.align 2
c1: .word 32
string: .space 20000
string2:.space 20000
myArray:.space 4096
Start: .asciiz "\nWelcome to memory management:\n (1) Do you want to allocate?\n (2) Do you want to deallocate?\n (3) Find\n"
prompt: .asciiz "How much memory do you need?\n"
print: .asciiz " Done\n"
zero: .asciiz "Not enough memory\n"
Space: .asciiz "Free memory remaining:"
prompt2: .asciiz "Enter Variable name\n"
none: .asciiz "Memory is empty\n"
found: .asciiz "Variable name found\n"
notfound: .asciiz "Variable name not found\n"
.text
main:
#Reinitialize registers
addi $s0, $zero, 0
addi $s1, $zero, 0
addi $s3, $zero, 0
addi $s5, $zero, 0
addi $s6, $zero, 0
addi $t0, $zero, 0
addi $t1, $zero, 0
addi $t2, $zero, 0
addi $t3, $zero, 0
addi $t5, $zero, 0
addi $t7, $zero, 0
la $s2, string
jal Space_chk
#print prompt
li $v0, 4
la $a0, Start
syscall
li $v0, 5
syscall
beq $v0, 1, Alloc
beq $v0, 3, Find
j main
Alloc:
jal div1
jal Space_find
Alloc1:
# prompt the user for variable name
li $v0, 4
la $a0, prompt2
syscall
# get the string
move $a0,$s2
li $a1,20
li $v0,8
syscall
move $t5, $s3
# Save variable name and from chunk
addi $s3, $s3, 4
sw $a0,myArray($s3)
addi $s3, $s3, 20
sw $t5,myArray($s3)
addi $s3, $s3, -24
Alloc2:
addi $t2, $zero, 1
sw $t2, myArray($s3)
addi $t3, $t3, 1
addi $s3, $s3, 32
bne $t3, $s0, Alloc2
move $t7, $s3
addi $t5, $t5, 28
sw $t7, myArray($t5)
#print prompt
li $v0, 4
la $a0, print
syscall
j main
Adder:
#add a chunk if the remainder is greater than 0
addi $s0, $s0, 1
jr $ra
Space_find:
lw $t2, myArray($s3)
beqz $t2, Alloc1
addi $s3, $s3, 32
j Space_find
div1:
#print prompt
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
blt $s5, $v0, err
move $t1, $v0
addi $t0, $zero, 32
div $s0, $t1, $t0 # s0 quotient
mfhi $s1 # s1 remainder
#mult $t0, $s0
#mflo $s0 # product
bgt $s1, 0, Adder
jr $ra
err:
li $v0, 4
la $a0, zero
syscall
j main
Space_chk:
lw $t2, myArray($s3)
beqz $t2, Space_chk1
addi $s3, $s3, 32
j Space_chk
Space_chk1:
addi $t0, $t0, 4096
sub $s5, $t0, $s3
#print
li $v0, 4
la $a0, Space
syscall
li $v0, 1
move $a0, $s5
syscall
jr $ra
Find:
#offset index
addi $s0, $s0, 4
addi $s6, $s6, 127 # Set limit of the search
la $t0, string2 # .space 200000
Find1:
lw $t0, myArray($s0)
addi $s0, $s0, 32
addi $t1, $t1, 1
beq $t1, $s6, None # Search limit
beqz $t0, Find1
Find2:
# prompt the user
la $a0,prompt2
li $v0,4
syscall
# read in the string
move $a0,$s2
li $a1,20
li $v0,8
syscall
cmploop:
lb $t5,($s2) # get next char from str1 / user
lb $t2,($t0) # get next char from str2 /table
bne $t2,$t5,Find1 # are they different? if yes, go back
beq $t5,$zero,Found # at EOS? yes, fly (strings equal)
addi $t0,$t0,1 # point to next char
addi $t3,$t3,1 # point to next char
j cmploop
None:
li $v0, 4
la $a0, none
syscall
j main
Found:
li $v0, 4
la $a0, found
syscall
j main
Found2:
li $v0, 4
la $a0, notfound
syscall
j main
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|