2010-11-04 95 views
2

我正在嘗試編寫一個程序,將從控制檯輸入的小寫字母按字母順序排列。它必須丟棄大寫字母和任何非字母數字。這是我迄今爲止所擁有的。它工作正常,但我仍然需要實現代碼來刪除大寫字母和非字母數字。關於如何將其實施到此計劃中的任何想法?謝謝!MIPS丟棄大寫字母或非字母數字

哦,還有一件事。例如,當我按原樣運行此程序時,它會爲答案創建一個新行。

應該說

Please type the letters: cba* 
The alphabetized string is: abc 

相反,它說

Please type the letters: cba* 
The alphabetized string is: 
abc 

反正在這兒呢。

# $t0 -- Pointer to current spot in letters 
# $t1 -- Holds the "upstream compare character" 
# $t2 -- Holds the current character being analyzed 
# #t7 -- Pointer to the first character in string 
#### Data Segment #### 
.data 
letter_prompt: .asciiz "Please type the letters: " 
output_message: .asciiz "The alphabaetized string is: " 
inputString: .space 30 # space for input string 
     .text 
main: 
la $a0,letter_prompt #print prompt string 
li $v0,4 
syscall 
la $a0,inputString #read the input string 
li $a1,30  #at most 100 characters 
li $v0,8 
syscall 
la $t0,inputString 
la $t7,inputString 
j loop 
loop: 
lb $t1,0($t0)  #Load first two characters to be compared 
lb $t2,1($t0) 
beqz $t2, exit_loop  #if NULL, we are done 
blt $t1,0x61,no_change 
bgt $t1,0x7a,no_change 
ble $t1,$t2,no_change 
jal rev    #Characters not in correct order; go to reverse 
j loop   #Character in correct position; get next character 
no_change: addi $t0,$t0,1  #increment character 
j loop 
exit_loop: la $a0,output_message #output sorted string 
li $v0,4 
syscall 
li $v0,4 
la $a0,inputString 
syscall 
li $v0,10  #exit program 
syscall 

#Character reverse routine 
rev: 
sub $sp,$sp,4  #Store contents of $ra on the stack 
sw $ra,($sp)  #Decrement stack pointer. 
sb $t1,1($t0)  #Exchange two character positions 
sb $t2,0($t0) 
beq $t0,$t7,goBack #if at first position in the string, done 
sub $t0,$t0,1  #Decrement the letter pointer 
lb $t1,0($t0)  #Compare the letter to next "upstream" letter 
lb $t2,1($t0) 
ble $t1,$t2,goBack #If letter is properly placed, done 
jal rev   #Not done yet; move back another position 
goBack: 
addi $t0,$t0,1  #Reverse done; move back to current position 
lw $ra,($sp) 
addi $sp,$sp,4 
jr $ra 

爲什麼它看起來像這樣?我如何將它變成常規格式?

+0

縮進4個空格,而不是使用反引號代碼塊(實際上,避免一般的反引號)。您插入了許多空白行,以便反向選中的行將位於不同的行上,並且您可能需要在編輯後重新格式化代碼。 – 2010-11-08 16:06:20

回答

0

只需使用ASCII碼。 ASCII