I am trying to read a random word from a text file but sometimes depending on the value of $s5 an infinite loop happens and the value of $t2 never changes.
Sorry for the block of text. I've never used stack overflow before and I don't know how much of my code I should provide. Thank you very much.
.data
randomword: #word to be found inside the file
.asciiz "\0\0\0\0\0\0\0"
fin:
.asciiz "dictionarysmall.txt" # filename for input
fileContents:
.word 0
sizeOfFile:
.word 0
str2: .asciiz "loop 2"
str3: .asciiz "loop 3"
str4: .asciiz "loop 4"
fileBuffer:
.asciiz
.text
###############################################################
# Open (for reading)
li $v0, 13 # system call for open file
la $a0, fin # input file name
li $a1, 0 # Open for writing (flags are 0: read, 1: write)
li $a2, 0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
###############################################################
li $v0, 42
li $a0, 9
li $a1, 250 #if using dictionary.txt value is 171900
syscall
move $t1, $a0 #generate a random number within 0 to 171920
move $s5, $t1 #$s5 contains the random number
# Read file just opened
li $v0, 14 # read from file
move $a0, $s6 # file descriptor
la $a1, fileBuffer # address of buffer from which to write
la $a2, ($s5) #buffer length
syscall # read from file
###############################################################
# Close the file
li $v0, 16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file
###############################################################
#****HERE IS WHERE THE ERRORS BEGIN OCCURRING.
li $s5, 60 #changing this number decides whether the program works or fails
wordfindloop1: #run through bits until random number is reached
la $t7, fileBuffer
la $t6, randomword
li $t1, 13 #newline
li $t2, 0
add $t7, $t7, $s5
j wordfindloop2
wordfindloop2: #go through bits until \n is found and then store word
#li $v0, 4
#la $a0, str2
#syscall
#li $v0, 11
#la $a0, ($t7) #prints out contents of $t7 which are very strange
#syscall
beq $t2, $t1, wordfindloop3 #0 is the newline char which would mark the start of a word
lb $t2, ($t7) #$t2 contains the next char
addi $t7, $t7, 1
j wordfindloop2
wordfindloop3: #bad code probably but add 2 to the address
#li $v0, 4
#la $a0, str3
#syscall
addi $t7, $t7, 1
wordfindloop4:
#li $v0, 4
#la $a0, str4
#syscall
lb $t2, ($t7)
beq $t2, 13, finish
sb $t2, ($t6)
addi $t7, $t7, 1
add $t6, $t6, 1
j wordfindloop4
finish:
li $v0, 4
la $a0, randomword
syscall