Question
In this assignment you will write a MIPS program that combines loops, I/O, and functions to play a simple game. The game chooses a random
In this assignment you will write a MIPS program that combines loops, I/O, and functions to play a simple game. The game chooses a random secret number between 1 (min) and max (variable) and requests guesses from the user, telling them whether their guess is too high or too low until the number is found. The name of the program is guess, and its assembler file is guess.s. There is a caveat of guess: It does its guessing in hexadecimal! See the end of the assignment for more info. Before you work on this assignment, you should have finished in-class exercises 5C and 5D, and understand how to use the support functions in util.s. Due to the large amount of details required to code in assembly, this assignment will take significant time and effort to complete, so start early and plan accordingly. I suggest you write the program in a C-like language then translate it to assembly language. In any case, you should develop a complete algorithm before you start any MIPS assembly. Design changes and bugs are very costly in assembly language. (If you want to get your program working first and you do not understand I/O in C, write a C++ or Java program with very simple C code and use C++ or Java I/O for testing.) Your program is required to have and use a number of functions. Four of these (plus main) you will have to write. One of these functions has been partially written for you - you just have to finish translating it to assembler. Their interface and parameters are below. Others are support functions and wrappers around standard syscalls provided in the file util.s. The use of functions (and of your main function) must conform to the MIPS procedure calling convention. The main program requires you to write four functions: A function to assemble the question from three strings. I have written the C code for this function and partially translated it for you in a file named create_question.s.skel in the asmt05 directory on hills. create_question() takes three strings as arguments, allocates space for a new string on the heap using malloc(), and creates the new string by concatenating the three argument strings. It then returns a pointer to the assembled question. char* create_question(char* part1, char* part2, char* part3); (one of the strings should be the encoded value of max (see the suggested algorithm below)) A pair of functions to deal with random numbers: InitRandom() and RandomIntRange(). Once the random number generator has been initialized by a call to InitRandom(), calling RandomIntRange() with a min and max value returns a random number in that range, inclusive. The two functions can be implemented by calls to some provided support functions in init.s, with some added arithmetic. They are described more below. A function to get a legal guess (i.e., one between min and max). The prototype is below: int get_guess(char *question, int min, int max); Using the question from create_question, get_guess() asks the user the question (which includes the encoded max value), and retrieves an integer response. It ensures the response (guess) is in the range [min,max], then returns it. Note that this means get_guess() must include a loop, since it cannot return until the user enters a legal guess (or chooses to quit). You must distinguish between an input guess that is out of range from a guess that is in range and does not match the secret number. Instead of using syscalls directly, get_guess() will use the functions InputDialogString and MessageDialog from util.s to do the syscalls. These are described below. get_guess() must allow the user to 'give up' ('quit' - this is provided by the Support Functions), in which case it returns a negative integer. Since the user guesses in hexadecimal, you need functions to translate between integers and their [hexadecimal] string representation. These are provided in the Support Functions. A suggested algorithm: [Initialization] At the start of the program, encode the max variable (from a static (.data) integer value) into a text buffer allocated on the stack by calling a function from util.s (see below). Then call create_question() once with your buffer and two static strings (with the remainder of the text of your question), saving the return value, which is the [pointer to the] resulting question. Then call InitRandom() once to initialize the random number generator. [The game starts here. Repeat until the user no longer wants to play] At the beginning of each guessing game, call RandomIntRange to get the secret number. Then [Repeatedly] call get_guess() to get the current guess. get_guess() does not return until the user enters a guess between min and max. Then compare the return value of get_guess() (the guess) to the secret number, providing feedback to the user whether their guess was too high, too low, or correct. This continues until the user guesses correctly or gives up. At the end of each game, ask the user if they want to play again. If so, start a new game. When the user doesn't want to play again, the program completes by returning from main(). Support functions A handful of support functions are needed by your program. These are provided in util.s. It is in the class public directory on hills. Here is a list of the functions you will most likely need from this package. For translating between integers and strings use int axtoi(int *num, char *string) int itoax(unsigned int num, char *string) Instead of using syscalls directly, you will call functions to do the syscalls. This will enable you to take your code later, combine it with C code to emulate the syscalls, and run the result in a virtual MIPS machine environment. A few functions from util.s that you will find useful in guess.s have been implemented as well for the MIPS VM. These are listed below. int InputDialogString (char *message, char *buf, int max) void MessageDialog(char *message, int type) The following functions will be used in your implementation of InitRandom, which is described below: void srandom(unsigned int seed); unsigned int time(int); // note: THE ARGUMENT SHOULD BE 0 The function unsigned int random(void); will be used in your implementation of RandomIntRange, which is described below. You can use any other functions from util.s that you want. InitRandom Before you start your first guessing game, you must initialize the random number generator by calling InitRandom, which you must implement: void InitRandom(int offset) InitRandom must initialize the random number generator by: getting the time-of-day (from time(0)) (Make sure you pass time the single argument of constant zero. This will allow your code to run on the MIPS virtual machine later.) adding a 4-digit number of your choice to the time (such as the last four digits of your student id), passed to InitRandom as offset passing the resulting seed to srandom() RandomIntRange Once the random number generator is initialized, you get a secret number by calling RandomIntRange, which you must implement. int RandomIntRange(int low, int high) This returns a pseudorandom integer in the range [low,high] by retrieving a random number from the generator using random(). This returns an integer in [0,MAX], where MAX is very large. converting the random number to a number in your range by performing appropriate arithmetic on it (see the More Help notes below) Putting it all together Your program will end up in several pieces, depending on how you write it. An example is: guess_main.s - your main program and get_guess RandomIntRange.s - RandomIntRange and InitRandom - the random number generator pieces create_question.s - the completed function to create the question - available in the asmt05 directory as create_question.s.skel util.s - a copy of util.s You can then create your program to run under MARS by cat-ing your files together and .include-ing util.s, or another way is by using the 'assemble all files in a directory' option. Requirements There are some requirements for how you write your program: Constant strings and integers may be initialized in static data (.data) and used by (but not modified by) functions in your program. Otherwise, data used by a function that is not kept in a register must be local (i.e., on the stack) or allocated dynamically (on the heap using malloc()). The values of min and max must be part of your question when asking for a guess. You must allow someone to change max easily by editing its static .data value before starting your program, and the current value, of course, must be displayed. You may assume min is 1. It must be clear to someone running your program what the limits are and that their guess will be interpreted as a hexadecimal number. You must write your functions as if neither the caller nor the callee knows what the other is doing, i. e., adhering strictly to the MIPS procedure calling convention. This must also be true of the function main() for the purposes of this assignment, although you can ignore the arguments to main() as usual. main must return when it is finished. Your program may not use syscalls - use the provided functions in util.s for system support. A few thoughts about randoms To get a reasonable random number when implementing the RandomIntRange function that differs from your neighbor running their code at the same time and from you running the code a second time, you must seed the random number generator with a value that contains the time (from a call to time(0)) plus a constant you define. This is the use of offset passed to InitRandom. Once you seed the generator you can get as many random numbers as you want from it - one per game, by calling RandomIntRange. To adjust the random number returned by random() to your range you will have to use a modulus operator. This is provided by the MIPS unsigned divide operator, divu. Read up on it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started