Source file: /~heha/Mikrocontroller/avr-inc.zip/mul2.i90

;16-bit-Multiplikation für ATmega
;h#s 01/06
#ifndef MUL2_I90
#define MUL2_I90

;Quelle: 2 16-bit-Zahlen
;Ziel: 32-bit-Zahl
;Vorzeichenbehaftete und vorzeichenlose Version

;	Mulu2u2(r17:r16,r19:r18)	;2 vorzeichenlose Argumente
;	Muls2s2(r17:r16,r19:r18)	;2 vorzeichenbehaftete Argumente
;Das Ergebnis ist R5:R4:R3:R2 (4 Bytes)
;Mit dem Symbol NOS2S2 wird die vzb. Variante ausgeklammert.

.include "makros.i90"

Mulu2u2:
;Multiplizieren 16bit
;PE: r17:r16 = vzl. Faktor 1
;    r19:r18 = vzl. Faktor 2
;PA: r5:r4:r3:r2 = vzl. Produkt (32 bit)
;VR: R0-R5
	mul	r16,r18
	movw	r3:r2,r1:r0
	mul	r17,r19
	movw	r5:r4,r1:r0
	mul	r16,r19
	add	r3,r0
	adc	r4,r1
	adc0	r5
	mul	r17,r18
	add	r3,r0
	adc	r4,r1
	adc0	r5
	ret

#ifndef NOS2S2

Muls2s2:
;Multiplizieren 16bit
;PE: r17:r16 = vzb. Faktor 1 (wird zerstört)
;    r19:r18 = vzb. Faktor 2 (bleibt erhalten)
;PA: r5:r4:r3:r2 = vzb. Produkt (30 bit + Vorzeichen)
;VR: R0-R5, R16,R17
	subi	r17,0x80
	subi	r19,0x80
;vzl. multiplizieren (a+8000h)(b+8000h)
	rcall	Mulu2u2
;Ergebnis korrigieren
	clr	r0
	subi	r19,0x80	;"b" zurückwandeln
	add	r16,r18
	adc	r17,r19		;"a+8000h+b"
	sbrc	r19,7
	 clc
	ror	r17
	ror	r16
	ror	r0		;()*8000h
	sub	r3,r0
	sbc	r4,r16
	sbc	r5,r17		;subtrahieren
	ret
;Der Berechnungstrick, ohne Konvertierung zu vzl. Zahlen:
;(a+8000h)(b+8000h) = a*b + a*8000h + b*8000h + 40000000h
;		    = a*b +(a+8000h + b)*8000h
;also:
;a*b = (a+8000h)(b+8000h) - (a+8000h+b)*8000h

#endif//NOS2S2
#endif//MUL2_I90
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded