University of Wisconsin Computer Sciences Header Map (repeated with 
textual links if page includes departmental footer) Useful Resources Research at UW-Madison CS Dept UW-Madison CS Undergraduate Program UW-Madison CS Graduate Program UW-Madison CS People Useful Information Current Seminars in the CS Department Search Our Site UW-Madison CS Computer Systems Laboratory UW-Madison Computer Sciences Department Home Page UW-Madison Home Page

Program 1

CS/ECE 354, Fall 2003


Send e-mail questions to Eric Robinson erobinso@cs.wisc.edu
Due Friday, September 26, before 5pm.


Purpose

The purpose of this first SAL programming assignment is:

Program Statement

Write a SAL program that reads in two user-entered non-negative integers. The first integer is a decimal integer greater than or equal to 2 and less than or equal to 16. It is used to specify the base of the second integer. The program then prints the second integer out twice, first in decimal, and then in hexadecimal.

Here is a first sample execution of the program. Characters entered by the user are printed in bold.


Enter the integer's base (in decimal): 2
Enter the integer: 00010011111
The integer in decimal is 159
The integer in hexadecimal is 0x9f

Here is a second sample execution of the program:


Enter the integer's base (in decimal): 16
Enter the integer: abc
The integer in decimal is 2748
The integer in hexadecimal is 0xabc

Here are sample executions of the program to illustrate error cases:


Enter the integer's base (in decimal): 3
Enter the integer: 22140
Bad user input encountered.  Quitting.


Enter the integer's base (in decimal): 22
Base out of range.  Quitting.


Enter the integer's base (in decimal): x2
Bad user input encountered.  Quitting.

Requirements

  1. Your program must contain adequate comments (documentation). Such comments include your name and section number at the top of the program. They include a description of the program, as well as variable descriptions, comments about the code itself, and a description of each procedure or function. See Guidelines for Programs for further clarifications on documentation.
  2. For this program, a valid integer consists of optional white space characters (tab or space characters), followed by one or more of the legal digit characters for a valid input base ('0', '1',. . ., '9', 'a', etc.), and is terminated by the newline character (0x0a). There may not be white space or other non-digit characters between digits of an integer or at the end of the line. Any input that does not meet these criteria is considered bad. For the case of bad input, your program should print out an appropriate error message indicating bad input and then exit. Note that leading zeros and the value zero are valid, given this definition of an integer.
  3. Clarification added Thurs. Sept. 18: Your program needs to correctly handle the case of reading in letters (such as 'a', 'b', where appropriate) of user input in lower case only. Your program may also handle upper case letters, but we will not use this as a test case.
  4. The legal bases for this program are greater than or equal to 2 and less than or equal to 16.
  5. Your program must work for any valid input that generates valid non-negative integers as results.
  6. Your program is restricted to use the SAL get and put instructions with an operand of type .byte. This restriction requires you to read in the user input character by character, forming an internal representation of an integer from the characters. It also requires you to print out an integer character by character. There is no restriction on the use of puts.
  7. Your program must perform procedure call and return using the SAL conventions. Section 2.7 of chapter 2 details the proper way to implement procedures in SAL.
  8. Your program must have and use the two procedures/functions/methods: get_integer and print_integer.

    get_integer reads in a single integer character by character. The base of the integer is given by a parameter, which is assumed to be greater than or equal to 2 and less than or equal to 16. It provides for the main program both the integer and an indication of whether an error in input was detected.

    print_integer prints an integer in a base. It is given both the integer to be printed and the base in which to print the integer. The base will be greater than or equal to 2 and less than or equal to 16.

    Write the program such that the program calls get_integer twice.

    Also write the program such that the print_integer is called twice.
  9. Do not use an array (a stack, actually) to hold an integer for printing. Instead, find an algorithm that calculates and prints digits starting with the most significant digit.
  10. Organize your program such that there is only one done statement in the program, and it is in the main program. This implies that there will be only one exit point from the program. This is good style.
  11. Your program is to operate the same as the samples given above. Do not loop to repeat the program.

Hints

  1. We must deal with the type mismatch between our input (characters) and our internal representation (integers) in this program. Mixing types within a single instruction can lead to difficulties, unless you know exactly what the program will do with the instruction. When an add or sub instruction is given of the form
    add int1, int2, ch
    the character is 8 bits, and the integers are 32 bits. Our simulator sign extends the character to 32 bits, and then does a 32-bit operation, producing a 32-bit sum. If, instead, the operation is done on 32-bit operands, but the variable for the result is only 8 bits, then the least significant 8 bits of the 32-bit result are used.
  2. An algorithm for reading input character by character and calculating the integer represented, as well as an algorithm for printing an integer character by character are given in the section on character representation in Karen's notes on Chapter 4.

Handing In the Program

Turn in your SAL program by running the script:


  handinP1 program1.s

where program1.s is an example name for the file containing your SAL program. The handin script submits your program for you. No printout will be turned in. (We assume that you have put /p/course/cs354-common/public/bin in your path by now.)