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:
- to learn and use basic SAL syntax,
- to gain experience with the simulator,
- to learn how to do simple character I/O in SAL,
- to learn how to write procedures the SAL way,
- to learn about the representation of integers and characters.
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
-
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.
-
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.
-
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.
-
The legal bases for this program are greater than or equal to 2
and less than or equal to 16.
Your program must work for any valid input that generates
valid non-negative integers as results.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Your program is to operate the same as the samples given above.
Do not loop to repeat the program.
Hints
-
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.
-
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.)