Homework 5
Multiplication, Division, and Data Conversion

Due: 5pm, Tuesday, December 14, 2009

Objectives

In this assignment you will build upon the multiplication and division routines you developed in Lab 4, and use those routines to implement binary/ASCII conversions. Make sure you complete Lab 4 before starting this assignment.

Section 1

 

For example, if a = -7 and d = 3, either of these answers could be considered correct:

o   q = -2, r = -1

o   q = -3, r = 2

On machines with hardware integer division, the circuitry enforces either one choice or the other. We are doing this in software, so we have the choice. The 1999 ANSI C standard says that the modulus must have the same sign as the dividend, so follow that rule.

 

How should you handle this? If you are calculating ( q = a / d, r = a % d), then inside DIV you need to handle several cases. The best way is to start by making a chart for yourself showing what to do.

a

d

What to do

Positive

Positive

Call DIV1

Negative

Positive

Do something #1

Positive

Negative

Do something #2

Negative

Negative

Do something #3

Zero

Non-zero

Do something #4

Anything

Zero

Do something #5

 

#4 is easy: q = 0, r = 0.
#5 is an error, but you have to return something, so return something such as q = -32768, r = -32768 .

What about #1, #2, and #3? Since DIV only works with two positive numbers, you will need to turn a and d positive, then call DIV1, then fix up the signs of the answers. The easiest way to do this is to do some examples of the different cases, and see what you would have to do to turn the results of DIV1 into the desired results.

·      Be sure to test the functions with multiple sets of data. In fact, in your main program please call both MULT and DIV several times with different sets of data, so we can easily see the results. You do not need to do any I/O for this section. You will be passing in this part of the assignment as a separate file (See Deliverables, below).  

 

Section 2

 

For this part of the assignment, you will write a program which reads in two numbers from the user, multiplies them, and prints out the product. This may seem complicated at the assembly-language level, but is straightforward if you approach it systematically by dividing it into a series of small functions, each of which does one thing. We'll go through it step-by-step.

·         Copy your multest.asm into a new file, such as hw5.asm. From now on, work with this file only.

·         Write a function which converts a null-terminated string of ASCII characters (one per word) into a binary integer (we will assume the ASCII represents a decimal integer). This is basically the algorithm we discussed in class last week, with one difference: keep converting until you hit either a null word (value zero) or a non-numeric character. Hitting a non-numeric character is not considered an error; just stop at that point. The function must handle up to five digits (the integer needs to fit into a 16-bit word). The address of the string must be passed in through a register; the result must come back in a register. Use the MULT function you wrote.

·         Now update that function so it handles both positive and negative numbers. A number starting with a minus sign should be considered negative. A number starting with no sign at all should be considered positive.

·         Be sure to test the function thoroughly by trying it with several numbers of various lengths.

·         Now write another function which turns a 16-bit signed integer into a null-terminated string of ASCII characters. Use the DIV function you wrote. Pass the value in a register, along with a pointer to a buffer (a block of memory) where the answer should be put. Be sure the buffer is long enough!

·         Again, thoroughly test your new function.

·         Next, write a function which reads a string from the keyboard and stores it into a buffer (Note: this function does not do any prompting, either for individual characters or for the string as a whole.). Pass in the address of the buffer in a register. Also pass in the maximum length of the string. The function must wait for a character, read it, echo it, and store it into the buffer. Repeat this until the user hits the Enter key (usually the ASCII code called “LF”, which is decimal 10) or the maximum length is reached. Be sure to echo so the cursor moves to the start of the next line. Do not store the LF character in the string. Be sure to properly terminate the string.

·         Thoroughly test the function, especially when the number of characters reaches or exceeds the maximum.

·         OK, almost done. Change your main program so it does the following:

1)      Prompt the user to enter a number.

2)      Read in the ASCII string.

3)      Convert it and store it as one of two operands.

4)      Do the same thing again (prompt and read in a number, and convert) for the second operand.

5)      Multiply the two numbers.

6)      Display the result of the multiplication.

Deliverables

Using web-based turnin, submit your two source code files, multest.asm and hw5.asm.