我正在尝试编写一个程序,将从控制台输入的小写字母按字母顺序排列。它必须丢弃大写字母和任何非字母数字。这就是我到目前为止所拥有的。它工作正常但我仍然需要实现代码来删除大写和非字母数字。关于如何将其实施到此计划中的任何想法?谢谢!
哦还有一件事。当我按原样运行此程序时,它会为答案创建一个新行。例如。
应该说
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
为什么会这样?如何将其变为常规格式?