2013-01-24 47 views
1

如何在MIPS中找到具有已知長度的用戶輸入字符串中的特定字符?然而,我看到的以及許多其他網站,似乎從根本上無法解釋如何操縱用戶輸入數據。在MIPS中查找字符串的字符

這是我到目前爲止有:

A_string: 
.space 11 
buffer: 
asciiz"Is this inputed string 10 chars long?" 
main: 

la $a0, buffer 
li $v0, 4 
syscall 

li $v0, 8 
la $a0, A_string 
li $a1, 11 
syscall 

回答

2

你將不得不遍歷讀緩衝區找你想要的特定字符。

例如,假設您想要在輸入數據中找到字符'x',並且假設此代碼段位於您的代碼之後,因此$a1已經具有讀取的最大字符數。您將不得不從緩衝區的開始處開始迭代,直到找到您期望的字符或您已遍歷整個緩衝區:

xor $a0, $a0, $a0 
search: 
    lbu $a2, A_string($a0) 
    beq $a2, 'x', found # We are looking for character 'x' 
    addiu $a0, $a0, 1 
    bne $a0, $a1, search 
not_found: 
    # Code here is executed if the character is not found 
    b done 
found: 
    # Code here is executed if the character is found 
done: