IUP Computer Science
COSC 300 Fall 2000

Assignment #5
(Due 20 November 2000)

The book provides a procedure for adding two integer values of any length; it also provides a procedure for subtracting two integer values of any length. Modify these procedures as necessary so they may be used to deal specifically with 64-bit values. Both of these procedures should continue to use SI to point to one operand value, DI to point to the other operand value, and BX to point to the result value. Then, create another procedure which can multiply two 64-bit integers and produce a 128-bit product. Again, use SI and DI to point to the operands and BX to point to the result. Write a short main program to test your procedures on the following data:

Subtraction:   47FFC82046006DA1 - 633B05019CC0E334
          The Difference is   E4C4C31EA93F8A6D

Multiply: 0005432100012345  *  0000020000000040
          The Product is      000000000A864200039752400048D140 

Multiply: 6638FAEB10238A93  *  7FFC839DD2FFED75
          The Product is      331B191FC9FE9F8EA721BB99E5F56C2F

The program that you write should have at least five procedures. Their names should be Main, Add64, Sub64, Mul64, and ShowHex. The ShowHex procedure is to be used to display the results of the arithmetic actions. There are no library procedures to display large integer numbers. You need to write your own. ShowHex should assume that SI is pointing to where the first byte of the number is stored and that CX holds the number of bytes that the number occupies. ShowHex should then display the number in hexadecimal. The easy way to convert a byte to displayable form is to use shifting or some other operation to separate the two nibbles then add the appropriate amount to get the corresponding ASCII codes for the hex digits. For example, suppose a byte contains 5E. Separate this into two bytes, one containing 05 and the other containing 0E. Since the first is a value <= 9, add 30h to it to make the ASCII code for a '5'. Since the second is a value >= Ah, add 37h to it to made the ASCII code for a 'E'.

The suggested approach for writing the Mul64 procedure is to use the "limited calculator" approach. It works like this. Suppose you had a calculator that could represent only 4 digits but you wanted to multiply 8354 times 4196 and get the correct answer (all 8 digits). You could write the 8354 as 83*102 + 54 and the 4196 as 41*102 + 96. Or in more general terms, we can think of one number as 102X + Y where X is 83 and Y is 54 and the other as 102W + S where W is 41 and S is 96. To get the product, multiply the polynomials

(102X + Y) * (102W + S) = 104XW + 102(YW + SX) + YS

Since X, Y, W, and S are all 2-digit numbers, the calculator can be used to compute each of the small products. For these sample numbers, XW is 3403, YW is 2214, SX is 7968, and YS is 5184. The product can then be obtained by adding the pieces as

10000*3403   +   100*(2214   +   7968)   +   5184      or      34030000  +  1018200  +  5184
     to get    35053384
This technique can be adapted to limited binary arithmetic, where the base is 2, not 10. The separation shown here between the hundreds and the thousands digit can be handled in binary by making a similar separation between appropriate bytes. The largest integers for which the Intel machine has instructions are 32-bits. Thus, this project is like working with the limited calculator which can deal with no more than 32 bits at a time (although the Intel multiply instructions can produce 64-bit products).

Your program must work with only positive (unsigned) 64-bit values. You may declare the storage areas you need for all procedures in the single data segment. Hand in a printout of the .LST file produced by the assembler and a captured printout of the results of a simple program that subtracts the two given numbers and multiplies the other two pairs of numbers.


Notes: You will need to use the DQ directive to allocate storage to hold the 64-bit values that you are working with. You will also need to specify .386 for the program to make available the type of instructions you need.