所以我的程序在打印单词时会遇到无限循环:
它正在打印我的数组的第一个字符,但它没有递增到下一个字符。我不确定在这一点到底要做什么:添加i * 4 + 1遇到异常,i * 4只打印第一个字符。建议?
#Prints original string
#Prints each word in string
#Prints word count for each string
#Prints final concatenated word count
.data
phrases: .word string1
.word string2
.word string3
.word string4
.word string5
.word string6
.word string7
.word string8
.word string9
.word string10
.word string11
.word string12
numPhrases:
.word 12
string1: .asciiz "Four-score"
string2: .asciiz "and seven years"
string3: .asciiz "ago, our"
string4: .asciiz "fathers"
string5: .asciiz "brought forth on"
string6: .asciiz "this continent a"
string7: .asciiz "new nation, "
string8: .asciiz "conceived in liberty"
string9: .asciiz "and dedicated to the"
string10: .asciiz "proposition"
string11: .asciiz "that all men are"
string12: .asciiz "created equal."
# Your code goes below this line
origString: .asciiz "The original string:\n"
words: .asciiz "Words found in the string:\n"
wordCount: .asciiz "Word count is now "
finalCount: .asciiz "Final word count is "
newline: .asciiz "\n"
.text
main:
# Function prologue
addiu $sp, $sp, -24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save caller's frame pointer
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup main's frame pointer
#Initial loop conditions
la $t0, numPhrases
lw $s0, 0($t0) #$s0 = numPhrases
la $s1, phrases #$s1 now equals arr of strings
addi $s2, $zero, 0 #i = 0, loop each char of a string
addi $s3, $zero, 0 #j = 0, loop each address of phrases
addi $t2, $zero, 0 #word count = 0
loopPhrases:
#for (j < phrases.length)
# print phrases[j];
#j++
#exit condition
slt $t1, $s3, $s0 #$t1 = j < numPhrases
beq $t1, $zero, endPhrases #if j < numPhrases, end
#loop body
#print original string
la $a0, origString
addi $v0, $zero, 4
syscall
add $s4, $s1, $s3 #$address of phrases[j]
lw $s5, 0($s4) #$s5 = phrases[j]
addi $a0, $s5, 0
addi $v0, $zero, 4
syscall #print string
#newline
la $a0, newline
addi $v0, $zero, 4
syscall
#Print words
la $a0, words
addi $v0, $zero, 4
syscall
loopWords:
#while (charAt(i) != '\n', '\t', or ' '
#and i < string.length())
# print charAt(i)
# i++
#exit condition
add $t5, $s2, $s1 #address of string[i]
lw $s5, 0($t5) #string[i]
beq $s5, $zero, endWords #if string[i] == nul, end
#If whitespace, jump
addi $t1, $zero, 9 #$t0 = 9(tab)
beq $t0, $t1, nextWord #$t0 = tab
addi $t1, $zero, 10 #$t0 = 10(newline)
beq $t0, $t1, nextWord #$t0 = newline
addi $t1, $zero, 32 #$t0 = 32(space)
beq $t0, $t1, nextWord #$t0 = space
#Print word
lb $a0, 0($s5)
add $v0, $zero, 11
syscall
#i++
add $s2, $s2, $s2
add $s2, $s2, $s2
j loopWords
nextWord:
la $a0, newline
addi $v0, $zero, 4
syscall
addi $t2, $t2, 1 #wordCount++
addi $s2, $s2, 1 #i++
j loopWords
endWords:
#Print word count
la $a0, wordCount
addi $v0, $zero, 4
syscall
la $a0, 0($t2)
addi $v0, $zero, 1
syscall
#newlines
la $a0, newline
addi $v0, $zero, 4
syscall
la $a0, newline
addi $v0, $zero, 4
syscall
#j++
addi $s3, $s3, 4
j loopPhrases
endPhrases:
#Print final count
la $a0, finalCount
addi $v0, $zero, 4
syscall
la $a0, 0($t2) #word count
addi $v0, $zero, 1
syscall
done: #Epilogue for main -- restore stack & frame pointer
lw $ra, 4($sp) #get return address from stack
lw $fp, 0($sp) #restore the caller's frame pointer
addiu $sp, $sp, 24 #restore the caller's stackpointer
jr $ra #return to caller's code