如何阻止用户输入在 MIPS 中打印新行? [英] How do I stop user input from printing a new line in MIPS?

查看:34
本文介绍了如何阻止用户输入在 MIPS 中打印新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的大学计算机科学课程有一项作业,要求我们使用用户输入打印出两个相邻的三角形.用户必须输入一个整数作为三角形的高度和一个用于打印三角形的字符.

I have an assignment for my college CS class that requires us to print out two triangles next to each other using user input. The user must enter an integer for the height of the triangles and a character that the triangles will be printed from.

我可以很好地接受三角形高度的整数,但是每次打印用户输入的字符都会开始一个新行,而不是打印在同一行上.

I can take in the integer for the triangles' height fine, but printing the character the user inputs starts a new line every time instead of printing on the same line.

这是我获取和打印输入的方式:

This is how I'm taking and printing the input:

    #Prompts user for character
    li $v0, 4
    la $a0, charPrompt
    syscall

    #Stores character from user
    li $v0, 8
    la $a0, userInput
    li $a1, 20
    move $t1, $a0
    syscall
    
    #Prints the character
    li $v0, 4
    la $a0, userInput
    syscall 

这是我的输出.为了弄清楚三角形的形状,我在.data下打印了"*"字符.所有 *" 都在正确的位置,但两个 e" 应该形成这些三角形的顶部.最终目标是使用用户输入的任何字符而不是 "*" 来打印那些精确的三角形.

Here is my output. In order to figure out the shape of the triangles, I printed out "*" character under .data. All of the "*" are in the correct place, but the two "e" are are supposed to form the tops of those triangles. The ultimate goal is to print those exact triangles using whatever character the user inputs instead of "*".

----e
-------------e

---*-*-----------***
--*---*---------*****
-*-----*-------*******
*********-----*********

我在 .data 下有它,如下所示:

I have it under .data as follows:

userInput: .space 1

推荐答案

使用系统调用 #8 时,MARS 将用户的换行符添加到您从系统调用收到的结果字符串中,他们用来终止输入.

When using syscall #8, MARS adds the user's newline character, which they used to terminate the input, to the resulting string that you receive from the syscall.

有多种选择:

  • 只需从系统调用返回的字符串中删除换行符

  • Simply remove the newline from the syscall returned string

从该输入中提取您想要的内容,例如第一个字符,忽略字符串的其余部分

Extract what you want from that input, like the first character, ignoring the rest of the string

使用系统调用 #12 代替逐个系统调用的输入

Use syscall #12 for input for character-by-syscall instead

因为 syscall #8 不返回长度,所以要删除换行符,搜索它,并用空字符替换它.

Because syscall #8 does not return a length, then to remove the newline, search for it, and replace it with a nul character.

这篇关于如何阻止用户输入在 MIPS 中打印新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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