2010-11-04 51 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