Question: Please explain in detail step by step Write a MIPS program that contains a function called generate _ echo _ string. The function should implement

Please explain in detail step by step
Write a MIPS program that contains a function called generate_echo_string. The function should implement the following C code:
// These should be global variables in your program's
//.data section.
char * prefix = "You typed: ";
char * prompt = "Enter a string: ";
/*
* This function accepts the address of a string that currently
* has no contents as an argument. The function will ask the user
* to type a string. The function will put the prefix string (declared
* above) at the beginning of string_to_modify, followed by whatever
* the user typed.
*
* So, if the user types "Hi there!", this function should place the
* following characters, in order, starting at the address specified
* by string_to_modify:
*
* You typed: Hi there!
*/
void generate_echo_string(char * string_to_modify, int max_bytes)
{
// Put a NULL byte at the end of string_to_modify, just to be
// safe.
string_to_modify[max_bytes -1]='\0';
int prefix_length = strlen(prefix);
strncpy(string_to_modify, prefix, max_bytes -1);
// It's possible that we've already used up all our bytes.
if (prefix_length >= max_bytes)
{
return;
}
// The address where you start writing the text entered
// by the user.
char * starting_point = string_to_modify + prefix_length;
// Now, let the user type, and copy as much as you can to
// string_to_modify.
InputConsoleString(prompt, starting_point, max_bytes - prefix_length);
}
You should also write a main function to test your function. When you submit, the tests will replace your main function, so what you include in it is up to you.
Once again, note that you will need to include util.s at the bottom of your program using a .include directive, like this:
.include "./util.s"
This assumes that util.s can be found in the same directory as your program. You should not submit util.s along with your program - just submit your program file.
strcpy:
move $v0,$a0
# int InputConsoleString (char *message, char * buf, int max)
# message is a message to be displayed
# buf is a pointer to a character buffer at least of length max.
#
# The return value is the number of characters stored in buf,
# not counting the null byte (thus equivalent to strlen(buf)).
#
# buf is always null-terminated and the newline does not appear in buf.
# If the user entered too much data, the extra data is lost.
#
.text
.globl InputConsoleString
InputConsoleString:
# int InputConsoleString (char *message, char * buf, int max),
# leaf function
# home arguments
sw $a0,0($sp)
sw $a1,4($sp)
sw $a2,8($sp)
li $t0,0
sb $t0,0($a1)
# Display prompt
li $v0,4
syscall
# Retrieve string
lw $a0,4($sp)
lw $a1,8($sp)
li $v0,8
syscall
#
# look for newline in buffer and store a nul byte on it
#
lw $t0,4($sp)
# unsigned strlen(const char *s)
# returns the length of s, not including the null byte
# s is assumed to NOT be NULL
#
.globl strlen
strlen:
# a0 has address of string
li $v0,-1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!