C51 aritmetika s vetsim rozsahem

Jaroslav Buchta jaroslav.buchta@gmail.com
Pátek Listopad 7 09:45:11 CET 2008


No, je to par radek v assembleru, nutnosti bude prace s operandy v RAM...
Ja mam napr. Pro tyto ucely nasledujici fci:


;/////////////////////////////////////////////////////////////////////
;// Function multiply 32b operand *pdwOp by 32b operand Mul to 64b 
;// internal result and divide it by 32b operand Div to total 32b result
;// All parametters unsigned
;/////////////////////////////////////////////////////////////////////

?PR?_MulDivUSLL?ASMSUPP                  SEGMENT CODE
?DT?_MulDivUSLL?ASMSUPP                  SEGMENT DATA OVERLAYABLE

;?PR?PwOutCount?ASMSUPP                   SEGMENT CODE

PUBLIC	?_MulDivUSLL?BYTE
PUBLIC	_MulDivUSLL

	RSEG  ?DT?_MulDivUSLL?ASMSUPP
?_MulDivUSLL?BYTE:
pdwOp?00:		DS  1
Mul?00:			DS  4
Div?00:			DS  4
	ORG  9
tmpPar?01:		DS  4

;// MulDivUSLL (PDWORDI pdwOp, DWORD Mul, DWORD Div)

	RSEG  ?PR?_MulDivUSLL?ASMSUPP
	USING	0
;-----------------------------------------------------------------------
_MulDivUSLL:
	MOV		A, R7
	ADD		A, #3		;To LSB of operand
	MOV  	pdwOp?00,A

	;First multiply pdwOp by Mul -> Mul(HI):R7-4(LO)
	;Using register:
	; R0 .. 1st IDATA working pointer
	; R1 .. 2nd IDATA working pointer
	; R2 .. inner loop counter
	; R3 .. bit counter
	; R7(LSB) .. R4(MSB) - low dword of result
	; Mul used as high result & multiplier

	CLR		A
	MOV		R7, A
	MOV		R6, A
	MOV		R5, A
	MOV		R4, A
	MOV		R3, #32
_MulDivUSLL_L1:
	;SHL low result dword
	MOV		R0, #AR7	
	CLR		C
	LCALL	ShlDw
	;shl high result dword & multiplier
	MOV		R0, #Mul?00 + 3
	LCALL	ShlDw
	JNC		_MulDivUSLL_C11
	; if C, add multiplicand to  result low & high dword
	MOV		R0, #AR7
	MOV		R1, pdwOp?00
	CLR		C
	LCALL	AddDw
	; carry to high 
	MOV		R0, #Mul?00 + 3
	MOV		R2, #4
_MulDivUSLL_L11:
	CLR		A
	ADDC	A, @R0
	MOV		@R0, A
	DEC		R0
	DJNZ	R2, _MulDivUSLL_L11
	;adition completted
_MulDivUSLL_C11:
	DJNZ	R3, _MulDivUSLL_L1

	;Second divide 64b temporary result by Div
	;Using register:
	; R0 .. 1st IDATA working pointer
	; R1 .. 2nd IDATA working pointer
	; R2 .. inner loop counter
	; R3 .. bit counter
	; *pdwOp for low dword of temporary result(dividend) & result
	; Mul .. high dword of temporary result
	; R7(LSB) .. R4(MSB) - divide remainder
	
	;Copy R7-4 to *pdwOp (low temp res dw)
	MOV		R0, pdwOp?00
	MOV		@R0, AR7
	DEC		R0
	MOV		@R0, AR6
	DEC		R0
	MOV		@R0, AR5
	DEC		R0
	MOV		@R0, AR4
	;Clear divide remainder
	CLR		A
	MOV		R4, A
	MOV		R5, A
	MOV		R6, A
	MOV		R7, A
	;Main cycle for dividing
	MOV		R3, #64
_MulDivUSLL_L2:
	;SHL 64b dividend to remainder
	MOV		R0, pdwOp?00
	CLR		C
	LCALL	ShlDw
	;High dword
	MOV		R0, #Mul?00 + 3
	LCALL	ShlDw
	;Shift to remainder
	MOV		R0, #AR7
	LCALL	ShlDw
	CLR		A
	RLC		A
	MOV		B, A	;Upper remainder bit
	;Test if remainder >= divider
	MOV		R0, #AR7
	MOV		R1, #Div?00 + 3
	CLR		C
	LCALL	SubDw
	MOV		A, B
	SUBB	A, #0
	JNC		_MulDivUSLL_C21	
	;remainder < divider -> add back
	MOV		R0, #AR7
	MOV		R1, #Div?00 + 3
	CLR		C
	LCALL	AddDw
	SJMP	_MulDivUSLL_C22
_MulDivUSLL_C21:
	;remainder >= divider -> set lower result bit
	MOV		R0, pdwOp?00
	INC		@R0
_MulDivUSLL_C22:	
	DJNZ	R3, _MulDivUSLL_L2

	;Succesfuly completted
	RET  	
; END OF _MulDivUSLL
;/////////////////////////////////////////////////////////////////////
;// Supporting functions
;/////////////////////////////////////////////////////////////////////
?PR?ShlDw?ASMSUPP    SEGMENT CODE
?PR?AddDw?ASMSUPP    SEGMENT CODE
?PR?SubDw?ASMSUPP    SEGMENT CODE



-----Original Message-----
From: hw-list-bounces@list.hw.cz [mailto:hw-list-bounces@list.hw.cz] On
Behalf Of Va Sm
Sent: Friday, November 07, 2008 9:37 AM
To: HW-news
Subject: RE: C51 aritmetika s vetsim rozsahem


Neefektivnost by az tak nevadila - vypocet se bude provadet jen "obcas".
Potrebuji vydelit 64bitove cislo 32bitovym a vysledek ulozit do 32bitoveho
cisla. 64bitove cislo ma spodnich 32bitu nulovych. Urcite existuje nejake
jednoduche reseni:)


> ------------ Původní zpráva ------------
> Od: Jan Waclawek <konfera@efton.sk>
> Předmět: RE: C51 aritmetika s vetsim rozsahem
> Datum: 07.11.2008 09:28:40
> ----------------------------------------
> > Daval bych prednost C kodu,
> 
> Znamena to, ze nevadi, ze je to neefektivne?
> 
> Existuju nejake vseobecne portable kniznice "multiple precision 
> arithmetics", ale je to ozaj kanon na vrabce.
> 
> Co presne z toho potrebujete a na co to mienite pouzit?
> 
> wek
> 
> _______________________________________________
> HW-list mailing list  -  sponsored by www.HW.cz Hw-list@list.hw.cz
> http://list.hw.cz/mailman/listinfo/hw-list
> 
> 
> 
_______________________________________________
HW-list mailing list  -  sponsored by www.HW.cz Hw-list@list.hw.cz
http://list.hw.cz/mailman/listinfo/hw-list




Další informace o konferenci Hw-list