我想從 MIPS 匯編語言的字串中完全洗掉某些字符(數字)
我找到了一種方法來確定哪些需要洗掉并嘗試替換它們,但我沒有找到一個只是空白的ascii值(不是''中的空格,而只是'')并使用''導致一個錯誤。我的代碼在下面,附有詳細的注釋。它有效,但我還沒有找到合適的字符來使用。我的問題是有沒有辦法用替換洗掉一個字符?還是我需要做一些地址操作來丟失我不想要的字符?
.data
str: .asciiz "i wa32n099na r9epl87ac2156e al6l52 15numb5e1rs in here"
.text
main: nop #main starts here
la $a0, str #load address of str into a0
la $t5, str
li $t1, '*' #load '*' into t1 this is the replacing bit so i wanna load a blank here
li $t2, '0' #this will be used as the min val t2
li $t3, '9' #this is max val t3
loop:
lb $t0, ($t5) #load the first byte of str into t0
beq $t0, $zero,done #stop condition if equals \0
bgt $t0,$t3,continue #if first byte(asciival) is greater than '9' than it isnt a number, so continue. on false it will check if num is below 0
blt $t0,$t2,continue #if first byte(asciival) is smaller than '0' and than it isnt a number, so continue
#if we got here than byte is a num
sb $t1, ($t5) #store the byte in first position (of moved addr)
continue:
addi $t5,$t5,1 #increment the address
j loop #loop up!
done:
li $v0, 4 #load string read into v0
syscall
jr $ra
uj5u.com熱心網友回復:
如果想從整數/單詞陣列中洗掉一個元素,我們不會尋找一個特殊的值來將該元素標記為無效;可以按照彼得所說的做,通過重新定位后續元素來縮小差距,有效地縮短陣列。
這同樣適用于字串,它們也是陣列:字串是位元組/字符的陣列。
要理解為什么洗掉元素比覆寫它更好,考慮一下除了列印之外我們可以用字串做的其他事情:例如,我們可以比較它們,我們可以詢問它們的長度,我們可以提取單個位元組,搜索子字串,將它們保存到檔案中以供其他程式稍后使用等。
對于隨機位置的有趣字符,所有這些操作要么不起作用,要么需要特殊的大小寫。最好完全洗掉縮短字串的不需要的字符,而不是用具有特殊含義的字符覆寫它們。
此外,類似于沒有零寬度的 ASCII 字符,對于整數/單詞陣列,沒有特殊的元素值可供選擇——所有可能的值都已經具有數字含義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426975.html
