我正在忙于编写一个MIPS程序,它将接收一个输入字符串,并打印出该字符串的所有可能唯一的排列组合。(即如果单词是LoOp,则LoOp和LOop是相同的)。
为了实现这个目标,我知道我需要确保我的输入字符串末尾没有换行符,但我不知道如何做到这一点。以下是我目前的代码:
.data
newLine:
.asciiz "\n"
promptUser:
.asciiz "Enter a 20 letter or less word:\n"
word:
.space 21
.text
main:
la $a0, promptUser
li $v0, 4 # Ask User for Input
syscall
la $a0, word
li $a1,21 # Max number of characters 20
li $v0,8
syscall # Prompting User
la $a0,newLine # Newline
li $v0, 4
syscall
la $a0, word # Printing Word
li $v0, 4
syscall
当输入的字母数恰好为20个时,'\n'不会包含在内。有什么建议吗?
修复:
这样可以解决问题:
li $s0,0 # Set index to 0
remove:
lb $a3,word($s0) # Load character at index
addi $s0,$s0,1 # Increment index
bnez $a3,remove # Loop until the end of string is reached
beq $a1,$s0,skip # Do not remove \n when string = maxlength
subiu $s0,$s0,2 # If above not true, Backtrack index to '\n'
sb $0, word($s0) # Add the terminating character in its place
skip:
$zero
的寄存器中读取。在MIPS中,需要依赖于输入来进行内存依赖顺序(与std::memory_order_consume
所暴露的相同)。 - Peter Cordessubiu
(它实际上不是真正的机器指令,只是带有负数的addiu)。sb $zero, word-2($a2)
。或者更好的方法是进行指针增量,这样即使word
不在低地址空间或高32KiB中,它也可以在没有伪指令的情况下运行。 - Peter Cordes