Program 4
Purpose
This program is intended to provide practice implementing nested procedures in MAL. It is important to follow all MIPS conventions as specified in P4 Conventions when writing your program and each of the procedures.
Program Statement
Write a MAL program that decides if a user-entered (positive) integer is a prime number.
Use the following simple, but inefficient algorithm for determining if a
number is prime. PRIME
represents a boolean variable (or flag).
int = get_integer(base=10) if (get_integer indicates bad input) have bad input if (int is negative or 0) have bad input if ((int == 1) or (int == 2)) { int is PRIME } else { if (int is evenly divisible by 2) { int is NOT PRIME factor = 2 } else { factor = 3 int is PRIME while ( (factor < int) and (int is PRIME) ) { if (int is evenly divisible by factor) { int is NOT PRIME } else { factor = factor + 2 } } } }
See all sample runs. The user input is shown in bold. All other display is generated by the program.
Sample program run 1
This program determines if an integer is a prime number.
Enter a positive integer: 12
This integer is NOT prime.
It is evenly divisible by 2
Sample program run 2
This program determines if an integer is a prime number.
Enter a positive integer: 17
This integer is prime.
Sample program run 3
This program determines if an integer is a prime number.
Enter a positive integer: 51
This integer is NOT prime.
It is evenly divisible by 3
Sample program run 4
This program determines if an integer is a prime number.
Enter a positive integer: 9a9
Bad input. Quitting.
Requirements
- Your program must follow the input/output format shown in the sample runs.
- Write the program such that it runs once, and then exits. Do not incorporate a loop to execute the program more than once.
- Comment your program appropriately. Each register used in a procedure should be described in the header for that procedure.
- Follow all register useage conventions as given in P4 Conventions.
- For this program, an integer consists of optional (zero or more) space or tab characters, followed by one or more of the digit characters '0'-'9', and is terminated by the newline character (0x0a). There may not be white space characters (tab, space or newline characters) in the middle of the digits. Any input that does not meet this criteria is considered bad. Note that leading zeros must be handled correctly, since they are included (by this definition) as good input. (Note that a dash character, '-', representing a negative number, is considered illegal input.)
- The program will have and use 4 procedures/functions.
get_integer
reads user input to get an integer.print_integer
prints an integer value.prime
is a function that determines if an integer is prime or not.divisible
is a function that determines if one integer is evenly divisible by another integer. - The function
get_integer
receives one parameter, the base, which is passed in the first parameter register ($a0). The function returns the value of the integer (in $v0) entered by the user in the base given as an input parameter. A return value of 0 (in $v1) indicates a valid integer was read. A return value of -1 (in $v1) indicates that the user entered invalid digits (this includes any characters not used for that base) for the given base. Therefore, this function just returns the error code. It does not exit the program. The calling program must check the value and quit if there was an error. - Procedure
print_integer
displays an integer in a given base. The base is passed as the first parameter ($a0) and the integer value to print is the second parameter ($a1). There is no return value for this procedure. - The function
prime
determines if an integer (the parameter, passed in $a0) is a prime number or not. A return value of -1 (in $v0) will mean that the integer is not prime. A return value of 0 (in $v0) will mean that the integer is prime. If the integer is not prime, then $v1 will have the smallest factor of the integer. Note that this function will call functiondivisible
more than once. - The function
divisible
is a simple function to determine if one integer (the first parameter, passed in $a0) is evenly divisible by the second integer (the second parameter, passed in $a1). A return value of -1 (in $v0) will mean that the integer is not evenly divisible. A return value of 0 (in $v0) will mean that the integer is evenly divisible. - Make sure that all instructions in the program are part of the MAL
instruction set. (Use the
MALtester
program as described in Program #2 to check.)Hints
- Note that your default base for the "base" mentioned above is base 10. Therefore, storing the base in a .word at the top of your program, which is easy to change, would be appropriate. (When I grade your program, I check your result with different bases- ranging from base 2 to base 10.)
- You should continue to learn and use sim as the simulator. The user interface to sim is graphical and it gives much more information than simp. Note: sim and simp are scripts that run the same simulator; the difference is the user interface presented. The user interface that goes with sim shows register values, and it easily allows single stepping through code. For detailed information, read the document listed under Handouts and labeled Simulator graphical interface manual (postscript) on the cs354 home page.
- The simulator implements the MAL I/O instructions like procedures
or functions within the operating system.
The conventions for register usage are not followed as well as we
would like.
Assume that any I/O instruction has the potential to overwrite
the contents of any of registers $2-$7 ($v0, $v1, $a0-$a3).
Handing In the Program
You must "hand in" your program (the MAL source code) to the computer directly by
cp p4.s ~cs354-1/handin/username/P4/.
just once, where "p4.s" is the name of the file containing your MAL source code and "username" is your user login. No printouts will be turned in. I will run your program several times using different test data.