Write a MIPS assembly language program that prompts the user to input a string (of maximum length 50) and an integer index. The program should then print out the substring of the input string starting at the input index and ending at the end of the input string. For example, with inputs “hello world” and 3, the output should be “lo world”.
Expert Answer
Here is MIPS code for the question. Output is also shown. Please rate the answer if it helped. Thank you.
.data
str: .space 50 #used to store user input of max 50 characters
msg1: .asciiz “nEnter a string: ”
msg2: .asciiz “nEnter the index: ”
msg3: .asciiz “nThe substring is: ”
.text
#prompt the user to enter a string
li $v0, 4 # sys call number for displaying a string
la $a0, msg1
syscall
#read user input string
li $v0, 8 # sys call number for reading string input
la $a0, str
li $a1, 50
syscall
#prompt the user to enter an index
li $v0, 4 # sys call number for displaying a string
la $a0, msg2
syscall
#read the index input from user
li $v0, 5 # sys call to read an int
syscall
move $t0, $v0 #$t0 now has the index
#display msg3
li $v0, 4 # sys call number for displaying a string
la $a0, msg3
syscall
#display substring
li $v0, 4 # sys call number for displaying a string
la $a0, str($t0) # load the address by offseting index from base address of str, $t0 has the index
syscall
output
Enter a string: hello world
Enter the index: 3
The substring is: lo world