我试图在 MIPS 中编写一个程序,该程序接受一个字符串并打印一个仅包含字符的结果(假设字符串是字母数字)。它适用于大小 < 4 的字符串,但是当出现第 4 个字符时,它会进入无限循环。
.data
string: .word 10
.data
result: .word 10
.data
message1: .asciiz "number\n"
.data
message2: .asciiz "letter "
.data
message3: .asciiz "finished loop\n"
.text
main:
li $v0 8 # take input as string
la $a0 string # store it in "string"
la $a1 10 # size of "string" is at most 10
syscall
la $s0 string # save address of "string" to s0
la $s1 result # save address of "result" to s1
Loop:
li $t0 10 # set t0 as '/n'
lb $t1 ($s0) # load character that we are currently checking
beq $t0 $t1 Exit # check if we are at the end of the string
li $t0 64 # set t0 to 64 to check if it is a letter (character that we are now checking must be greater than 64)
slt $t2 $t0 $t1 # t2 will store the result of comparison ($t1 - character)
li $t0 0 # set t0 to 0 to check the result of comparison
beq $t2 $t0 Increment # if 64 > ch, then we must just proceed
li $v0 4
la $a0 message2 # print message that it is a character
syscall
sb $t1 ($s1) # copy this character into our "result"
addi $s1 $s1 1 # increment the address of "result"
Increment:
li $v0 4
la $a0 message1 # print message that it is a number
syscall
addi $s0 $s0 1 # increment the address of "string" to proceed in loop
j Loop
Exit:
li $t0 10
sb $t0 ($s1) # add endline character to "result"
addi $s1 $s1 1
li $t0 0
sb $t0 ($s1) # add null character to "result"
li $v0 4
la $a0 message3 # print message that the loop has finished
syscall
li $v0 4
la $a0 result # print result
syscall
jr $ra
提前致谢。