如何在 MIPS 中删除换行符? [英] How to remove newline in MIPS?

查看:158
本文介绍了如何在 MIPS 中删除换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正忙于编写一个 MIPS 程序,它将接受一个输入字符串,然后打印该字符串的所有可能的唯一排列.(也就是如果这个词是 LoOp,LoOp 和 LOop 是一样的).

So I am busy writing a MIPS program that will take an input string, and then print all possible UNIQUE permutations of that string. (AKA if the word is LoOp, LoOp and LOop are the same).

为了做到这一点,我知道我不需要在输入字符串的末尾有换行符,但我不知道要确保它没有被添加.这是我目前所拥有的:

In order to do this, I know I need to NOT have a newline character on the end of my input string, but I don't know to make sure it's not added. Here is what I have so far:

.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.有什么建议吗??

The only time a '\n' isn't included is when the number of letters entered is exactly 20. Any suggestions??

修正:

这有效:

    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 返回时解析字符串以删除字符:

You can parse the string upon returning from syscall 8 to remove the character:

# 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

另请注意,您没有为单词预留足够的空间.你应该用

Also note that you didn't reserve enough space for the word. You should reserve 21 bytes with

word: .space(21)

这篇关于如何在 MIPS 中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆