如何删除MIPS中的换行符?

时间:2012-11-29 17:07:31

标签: assembly newline mips

所以我正在忙于编写一个MIPS程序,它将获取一个输入字符串,然后打印该字符串的所有可能的UNIQUE排列。 (AKA,如果单词是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时。任何建议??

FIX:

这有效:

    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:

1 个答案:

答案 0 :(得分:1)

您可以在从系统调用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)