在MIPS中打印变量后跟一个字符串

时间:2015-01-26 04:56:00

标签: assembly mips

我正在写一个MIPS程序,询问你的名字,然后打印出你好的行,你好吗?不过我的代码......

#Program that fulfills the requirements of COS250 lab 1
#Nick Gilbert

.data #Section that declares variables for program
firstPromptString:  .asciiz     "What is your name: "
secondPromptString: .asciiz     "Enter width: "
thirdPromptString:  .asciiz     "Enter length: "

name:           .space      20

firstOutStringOne:     .asciiz     "Hi "
firstOutStringTwo:  .asciiz     ", how are you?"
secondOutString:        .asciiz     "The perimeter is ____"
thirdOutString:     .asciiz     "The area is _____"

.text #Section that declares methods for program
main:
    #Printing string asking for a name
        la $a0, firstPromptString #address of the string to print
        li $v0, 4 #Loads system call code for printing a string into $v0 so syscall can execute it
        syscall #call to print the prompt. register $v0 will have what syscall is, $a0-$a3 contain args for syscall if needed

        #Prompting user for response and storing response
        la $a0, name
        li $v0, 8 #System call code for reading a string
        li $a1, 20
        syscall

        #Printing response to name
        la $a0, firstOutStringOne
        li $v0, 4 #System call code for printing a string
        syscall

        la $a0, name
        li $v0, 4 #System call code for printing a string
        syscall

        la $a0, firstOutStringTwo
        li $v0, 4 #System call code for printing a string
        syscall

打印"嗨"然后",你好吗"分开的。我需要将消息放在一行

1 个答案:

答案 0 :(得分:1)

据我所知,'\n'(换行符)来自用户输入。
在程序打印"What is your name: "后,用户输入他的名字并输入点击返回。这会在缓冲区中的名称后面放置一个'\n'。随后打印缓冲区时,将使用它打印换行符。为了防止这种情况,'\n'需要替换为'\0',即字符串终止符。因此,您需要memchr()strchr()这样的函数来查找'\n'并替换它。