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.
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.
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
This program determines if an integer is a prime number.
Enter a positive integer: 17
This integer is prime.
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
This program determines if an integer is a prime number.
Enter a positive integer: 9a9
Bad input. Quitting.
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.
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.
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.
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 function
divisible
more than once.
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.
MALtester
program as described in Program #2
to check.)
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.