1

I am writing a MIPS program that asks you for your name and then prints the line Hi , how are you? However my code...

#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

Prints "Hi " and then ", how are you" on separate lines. I need the message to be on a single line

1 Answer 1

1

As far as I can tell, the '\n' (newline) comes from user input.
After the program prints "What is your name: ", the user types his name and enters it by hitting return. This places a '\n' after the name in the buffer. When the buffer is subsequently printed, the newline is printed with it. To prevent this, the '\n' needs to be replaced with '\0', aka the string terminator. So you need a function like memchr() or strchr() to look for the '\n' and replace it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. And since you probably don't have access to those ready-made functions, you can program a simple routine that does what you want: For each char in the string, if it is =='0' then goto end; if it is =='\n' then replace it with '0' and goto end; else increment pointer and goto next iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.