
Notes for Assignment 2 (sent to class)

0.  This assignment explicitly asks you to do all the arithmetic
	internally in binary notation.  This is an important
	point of this exercise.  The input and output are in
	decimal because we humans understand this best.  But
	for the computer, the most efficient method is binary
	notation.  You will learn about or gain the following
		(1) Use of Procedures
		(2) Algorithms for conversions to and from
			decimal and binary
		(3) Opportunity to use Based Indexed
			Addressing Mode.
		(4) Deeper understanding of internal representation
			of numbers, and in particular, bigNums.
			
1.  How many bits do we need to store a 40-digit number?
	Well, each digit can fit into 4 bits and so
	20 bytes should be sufficient.
    Thus if the input are each 40 digits, their
	binary representation needs only 20 bytes
	and their product can fit into 40 bytes.

2.  How do I represent an arbitrary size binary number?
	Let us call such a number a BigBin.
	Suppose we want to represent a BigBin P that
	uses less than 40 bytes.
	Then you need to reserve the space for P in
	the data segment:
		P DB 40 dup (0)
	I will assume that the least significant
	byte of P is always at the last byte of the
	reserved space, namely, at address
		P+39
	We also need to know the length (in bytes)
	of the actual number represented by P.  We
	recommend that you store this at the address
	P itself.

   Similarly, if we want to represent a decimal number of
	less than 40 digits, we can set aside an array P
	of 40 bytes.  Each byte stores a decimal digit
	in BINARY NOTATION.  Clearly this is a waste, as
	we already noted in item 1 above, it is possible to
	fit 2 decimal digits in each byte.  But this is convenient.
	Again, we can let the number of significant digits
	of P be stored in address P, and the least significant
	digit be stored in P+39.  Call this a BigDec.

3.  ReadIn:
	This is the procedure to read in from the keyboard
	a sequence of digits and store the result as a BigDec.

4.  Dec2Bin:
	This is the procedure to convert a BigDec to a BigBin.
	The basic procedure goes as follows.
	Assume that the decimal number has digits
		d_1,d_2,d_3,...,d_n
	And we want to convert this to a BigBin P.
	Then we go into a for-loop:
		P = 0
		FOR i=1 to n DO
		  P = P x 10
		  P = P + d_i
		OD
	You need to procedures for this purpose:
		MulDig:
			Multiplies a BigBin by a
			binary number in a single Byte
			Use the machine instruction MUL to do each
			of the byte-size multiplications.
		LongAdd:
			Adds two BigBins

	Note that what we need for the Dec2Bin is
	somewhat more specialized than MulDig and LongAdd.
	But these procedures will be useful for other things.

5. LongAdd:
	This adds to BigBin.
	This was explained in class, and its code was
	distributed.

6. LongMul (in handout, we also called this "Multiply")
	This multiplies two BigBins.
	It is the heart of the homework.  Basically
	it is an adaptation of the Shift and Add method
	of multiplication shown in the text book.
	Suppose you want to multiply two BigBins, Q and R.
	Let P denote their product.  If R has n bytes,
		R = (b_n,b_{n-1},...,b_2,b_1)
	then proceed as follows:
		P = 0
		FOR i=1 to n DO
		  P = P x 256 	; this amounts to shifting left one byte
		  QQ = Q x b_i  ; QQ is a temporary BigBin
		  P = P + Q
		OD
	Clearly, these steps can be accomplished using the
	MulDig and LongAdd procedures mentioned above.

		
	
