Reputation: 4240
I'm trying to write a program in MIPS assembly that simply prompts a user for their name and then prints their name back to them. So far my code is
#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
firstOutString: .asciiz "Hi ______, 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
li $v0, 8 #System call code for reading a string
syscall
sw $v0, name
li $v0, 4 #System call code for printing a string
la $a0, ($v0) #address of the string to print
syscall
It prompts the user for their name but as soon as you type one character the code blows up. I'm editing and executing using the MARS IDE for MIPS
Upvotes: 0
Views: 32052
Reputation: 11
our lecturer suggested us to write the code in a high-level language as explicit as possible first, and then convert it to MIPS.
Python example:
prompt = "Enter your name: "
hello_str = "Hello "
name = None
print(prompt)
name = input()
print(hello_str)
print(name)
MIPS assembly code:
.data
prompt: .asciiz "Enter name: (max 60 chars)"
hello_str: .asciiz "Hello "
name: .space 61 # including '\0'
.text
# Print prompt
la $a0, prompt # address of string to print
li $v0, 4
syscall
# Input name
la $a0, name # address to store string at
li $a1, 61 # maximum number of chars (including '\0')
li $v0, 8
syscall
# Print hello
la $a0, hello_str # address of string to print
li $v0, 4
syscall
# Print name
la $a0, name # address of string to print
li $v0, 4
syscall
# Exit
li $v0, 10
syscall
Hope it helps:)
Upvotes: 0
Reputation: 58762
You are not using the read string
system call correctly. I suspect you haven't actually looked at the documentation on how to use it. You have to pass two arguments:
$a0 = address of input buffer
$a1 = maximum number of characters to read
So you should do something like:
la $a0, name
li $a1, 20
Nevertheless, this shouldn't cause a crash since $a0
should still hold the address of firstPromptString
that you set up for the printing, earlier, and that is valid writable memory.
Upvotes: 2