Computer Sciences Dept.

CS/ECE 354 Machine Organization and Programming

Fall 2008 Section 1
Instructor Mark D. Hill
TAs Mark Liu & Somayeh Sardashti

Homework // Due Friday, November 7, before 5pm.

Send e-mail questions to Somayeh Sardashti, somayeh@cs.wisc.edu

Collaboration Policy

For this assignment, you may work in pairs (2 people). All students (whether working in a pair or not) must individually turn in the assignment by individually running the handin program. Therefore, 2 copies of the assignment will be turned in for those working as a pair. The grader will choose to grade only one copy of a pair's work, and assign the same score to both students.

If working in a pair, the names of BOTH students must appear at the top of the turned in assignment.


Program

Program Statement

Write a MAL program that follows the CS 354 parameter passing and register usage conventions when implementing functions. This program implements an enhanced version of assignment 2, and states whether or not a user-entered integer is coprime with each of the integers in the range 2-9. This program is specified to force students to deal with parameter passing as well as register usage for more than one level of nested function calls.

This MAL program requests, and reads in a user-entered integer. For this program, the integer is not restricted to exactly 1 digit. Instead the program reads all characters until it encounters the first newline character. The program then uses the integer to print out the set of output lines for whether or not that integer is coprime with the integers in the range 2-9. No array is used for the results. Any positive integer greater than 1 is accepted by the program.

Here are several examples of program execution. User input is shown in bold to distinguish it from program output.

Enter an integer: 25
25 and 2 coprime? Y 
25 and 3 coprime? Y 
25 and 4 coprime? Y 
25 and 5 coprime? N 
25 and 6 coprime? Y 
25 and 7 coprime? Y 
25 and 8 coprime? Y 
25 and 9 coprime? Y 

Here is another example.

Enter an integer: 200
200 and 2 coprime? N
200 and 3 coprime? Y 
200 and 4 coprime? N
200 and 5 coprime? N 
200 and 6 coprime? N
200 and 7 coprime? Y
200 and 8 coprime? N
200 and 9 coprime? Y

Here are several examples that show what the program does under unusual or erroneous user input. The difficulty is given in bold as a header.

User wants to enter a negative number, and starts with a '-' (dash) character.

Enter an integer: -96
Bad user input.  Quitting.

User enters an integer that is less than 2.

Enter an integer: 1
Bad user input.  Quitting.

The user enters a valid integer, but again it is less than 2.

Enter an integer: 0000000
Bad user input.  Quitting.

User enters a bad character as part of the requested integer.

Enter an integer: 3x7
Bad user input.  Quitting.

Again, the user enters a bad character as part of the requested integer.

Enter an integer: 218h
Bad user input.  Quitting.

Again, the user enters a bad character (a space) as part of the requested integer.

Enter an integer: 2 36
Bad user input.  Quitting.

Required Functions

Your program must have and utilize the following functions/procedures. This structure is reasonably simple, and it forces you to implement functions that are both parents and children, thereby forcing you to know and implement the parameter passing and register usage conventions.

  • main will be responsible for the following things:
    1. Print the prompt (Enter an integer: )
    2. Call get_integer().
    3. Check the validity of the entered integer. If invalid or the range is incorrect, print the error message, and exit.
    4. For each line of the output, set up parameters and call print_line().
  • get_integer is a function that reads user input, in order to return the integer read. The function receives no parameters. It returns 2 values! In $v0 is the value of the integer entered by the user. In $v1 is an indication of whether the input was OK or not. For good user input, $v1 returns the value 0. For bad user input, $v1 returns the value -1.

    For this program a good (valid) integer is one that contains only digit characters ('0' - '9'). Reading of characters ends with the newline character. If any character other than a digit character is read before the newline, or if the newline is the first character entered, then the user input is bad (invalid). Note that this implies that only 0 or positive integers are considered valid input.

    This program (hence, this function) ignores the possibility of overflow for the user input. We will not test your program with an integer that would overflow a 32-bit, two's complement integer. (You may learn more about detecting overflow without actually causing overflow in the compilers class, CS536.)

  • coprime is a function that returns the character 'Y' or 'N' indicating whether or not the first parameter is coprime with the second parameter. The first parameter is the user-entered integer. The second parameter is the integer from the range 2-9 that is currently being examined.
  • print_line is a procedure (a function that returns no value) that prints one line of the output. It receives 2 parameters. The first parameter is the user-entered integer. The second parameter is the integer from the range 2-9 that is currently being examined. Given these 2 parameters, this procedure is responsible for the following things:
    1. Call print_integer to print out the user-entered integer.
    2. Print the string " and ".
    3. Call print_integer to print out the integer that we will see if the user-entered integer is coprime with.
    4. Print the string " coprime? ".
    5. Call coprime to calculate and return whether or not the two numbers are coprime.
    6. Print out the value Y/N returnd by coprime
    7. Print the newline character.
  • print_integer receives a single parameter, which is the integer to be printed. Note that this integer will not be negative (0 or postitive) for this program.

Requirements

  1. Your program must follow style guidelines as given in Style Guidelines.

    Also include a comment at the top of the source code with your name (and your partner's name, if working in a pair).

    See Guidelines for Programs to see an indication of point allocation used in program grading.

  2. Name your source code program MALcoprime.s, and place the entire program into this file. This file is what will be turned in. It must have exactly this name to make the lab-supported handin program work. A suffix of .s is the convention used in Unix operating systems to indicate an assembly language source code file.
  3. Follow all parameter passing and register usage conventions as described in MIPS register usage. Most of the points in this program will be given based on following the conventions.
  4. Organize your program such that there is only one exit point (the done instruction) in the program.
  5. Your program is to operate and appear the same as the examples given above. Use the same prompts and strings. Do not produce output other than what is shown in the examples. Also, the program runs once, and then exits. Do not add looping to repeat the program's functionality.


Notes

  1. Remember that the I/O instructions getc, putc, and puts are implemented as functions within the operating system's code, and they overwrite registers $v0 and $4 (alias $a0).
  2. To get this program written and tested as fast as possible, use the following ordered steps:
    1. Write procedure print_integer, as well as a small amount of code within main to call print_integer with several different test values.
    2. Write function get_integer. Test that the function works by adding code to main that prints the required prompt and then reads in the desired integer. Also add just enough code to the main program to print out the integer read!
    3. Write a loop that calls print_line the correct number of times. Also add just enough code to print_line (maybe only a puts instruction) to see that the loop works.
    4. Add code to finish the print_line procedure. Also add just enough code to coprime to return an integer value correctly. It is during the testing of this code that you may encounter the most frustration from getting your activation records correct and making sure that registers are properly saved and restored.
    5. Add code to finish coprime.

Handing In the Program

Your MAL source code must be in a file named MALcoprime.s. Turn in your program by running the lab-supported handin program.


  /s/handin/bin/handin -c cs354-1 -a a4 -d .   

while your current working directory is the one containing the MALcoprime.s file. This program copies the specifically named file to a directory accessible by the instructor and TAs. (P.S. If you add the path /s/handin/bin within your .cshrc.local file, re-login, then you can omit /s/handin/bin/ from the command.)

 
Computer Sciences | UW Home