View Single Post
  #3   Spotlight this post!  
Unread 30-12-2002, 16:42
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
add it all up

To multiply two large numbers you can add the 8-byte value to itself multiple times in a loop. You will need to reserve an extra byte for the results. This is probably painfully slow, I do not endorse this at all.

Here is a basic algorithm, I'm not sure whether it will work. I don't see a reason for needing this, but here is a crack at it anyways.

Code:
'inputs for the sub that will add the numbers together
bigVar 		VAR BYTE  'pointer to the low-order byte in RAM
resultVar   	VAR BYTE  'pointer to the results in RAM
smallVar    	VAR BYTE  '8-bit operand
numBytes  	VAR BYTE  'number of bytes in bigVar

'working variables
op1           	VAR BYTE
op2           	VAR BYTE
temp     	VAR WORD
position     	VAR BYTE
counter      	VAR BYTE

'64-bit operand starts at ram location 1
'this represents the value $0807060504030201
PUT 1, $01
PUT 2, $02
PUT 3, $03
PUT 4, $04
PUT 5, $05
PUT 6, $06
PUT 7, $07
PUT 8, $08

'set up values for the call to the subrountine
bigVar = 1	'pointer to RAM location for operand 1
resultVar = 10	'pointer to RAM location for results
numBytes = 8	'number of bytes in operand 1
smallVar = 100	'multiply operand 1 by 100

GOSUB FindBigProduct

'now, the results are stored in RAM locations
'10 - 18 (9 bytes total)

'----------------------------------------------
'subroutine to multiply a multi-byte value 
'stored in RAM by a byte. The multi-byte value
'should be stored in sequential RAM locations
'in order from least to most significant byte
'-----------------------------------------------
FindBigProduct:

	'initialize the results to 0. there 
	'will be numBytes + 1 bytes in result
	FOR counter = 0 TO numBytes
		PUT resultVar + counter, 0
	NEXT

	'exit if the other operand is 0
	IF smallVar = 0 THEN FindBigProduct_Return

	'loop through and add the value in 
	'bigVar to itself smallVar times.
	FOR counter = 1 TO smallVar
		'reset the running sum so 
		'that it handles overflow
		temp = 0

		'loop through and add each
		'byte together, saving overflow
		FOR position = 1 TO numBytes
			'get the operands 
			GET bigVar + position - 1, op1
			GET resultVar + position - 1, op2
			
			'add them together
			temp = temp + op1 + op2
			'save the low-order byte
			PUT resultVar + position - 1, temp.byte0
			
			'shift the overflow
			temp = (temp & $FF00) >> 8
		NEXT
	NEXT
	
	'save the highest order byte		
	PUT resultVar + numBytes, temp.byte0

FindBigProduct_Return:

	RETURN

Last edited by seanwitte : 30-12-2002 at 17:08.