gpt4 book ai didi

assembly - 如何在 MIPS 中删除换行符?

转载 作者:行者123 更新时间:2023-12-01 05:26:24 24 4
gpt4 key购买 nike

所以我正忙于编写一个 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

唯一不包含 '\n' 的情况是输入的字母数正好是 20。有什么建议吗??

使固定:

这有效:
    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:

最佳答案

您可以在从 syscall 8 返回时解析字符串以删除字符:

# your code to prompt the user        

xor $a2, $a2, $a2
loop:
lbu $a3, word($a2)
addiu $a2, $a2, 1
bnez $a3, loop # Search the NULL char code
beq $a1, $a2, skip # Check whether the buffer was fully loaded
subiu $a2, $a2, 2 # Otherwise 'remove' the last character
sb $0, word($a2) # and put a NULL instead
skip:

# your code continues here

另请注意,您没有为该词保留足够的空间。您应该保留 21 个字节
word: .space(21)

关于assembly - 如何在 MIPS 中删除换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com