//****************************************************************************************
//                                                                                       *
//   R8C Program Collection No. 11                                                       *
//   CPU        : R8C/Tiny                                                               *
//                                                                                       *
//****************************************************************************************


//========================================================================================
//	Title: Multiplying 32 bits
//	Outline: Multiplies 32-bit data together using registers
//	Input:	------------------------------>	Output:
//	R0 (Lower half of multiplicand)			R0 (Lower part of multiplication result)
//  R1 (Lower half of multiplier)			R1 (Upper part of multiplication result)
//	R2 (Upper half of multiplicand)			R2 (Middle part of multiplication result)
//	R3 (Upper half of multiplier)			R3 (Most significant part of multiplication result)
//	A0 ( )									A0 (Indeterminate)
//	A1 ( )									A1 (Indeterminate)
//	Stack amount used: 6 bytes
//	Notes: R2R0 X R3R1
//	       Calculation result is output in order of R3, R1, R2, and R0 beginning with the most significant bits.
//========================================================================================


.text 
.global	_MULTIPLE32 
.type	_MULTIPLE32, @function



#pragma inline_asm(_MULTIPLE32) 
_MULTIPLE32:											
	PUSH.W		r1									// Saves lower half of multiplier
	PUSH.W		r3									// Saves upper half of multiplier
	PUSH.W		r3									// Saves upper half of multiplier

	MULU.W		r2,r1								// Multiplies upper half of multiplicand by lower half of multiplier
	MOV.W		r3,a1								// Saves calculation result
	MOV.W		r1,a0								// 
	POP.W		r1									// Restores upper half of multiplier

	MULU.W		r0,r1								// Multiplies lower half of multiplicand by upper half of multiplier
	ADD.W		r1,a0								// Adds to intermediate calculation value and saves result
	ADC.W		r3,a1								// Holds carry until next addition is made
	POP.W		r1									// Restores upper half of multiplier

	MULU.W		r2,r1								// Multiplies upper half of multiplicand by upper half of multiplier
	ADCF.W		r3									// Adds carry to the most significant bit
	POP.W		r2									// Restores lower half of multiplier

	MULU.W		r2,r0								// Multiplies lower half of multiplicand by lower half of multiplier
	ADD.W		a0,r2								// Adds intermediate value to middle part
	ADC.W		a1,r1								// Adds intermediate value to upper part
	ADCF.W		r3									// Adds carry to the most significant bit

	RTS												// 

.END									

					
	