Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Back to basics - math questions

2004-08-30 by Mark Jordan

I'm dealing with two-complement math using 16bit registers.
	Please take a look at my routines and tell me if I'm doing
anything wrong or if there is a better way of doing it:

---------

	Adding two signed numbers is:

	add	numl, fatl
	adc	numh, fath

---------

	For subtracting two signed numbers I'm using this:

	sub	numl, fatl
	sbc	numh, fath

	Maybe I should use this instead (I guess the result is the same):

	com	fatl
	com	fath
	adiw	fatl, $01
	add	numl, fatl
	adc	numh, fath

---------

	For 16x16bit signed multiply	:

	clr	zero
	muls	auxh, genh		; (signed)S1h * (signed)S0h
	movw	matl, prdl

	mul	auxl, genl		; S1l * S0l
	mov	inth, prdh

	mulsu	genh, auxl		; (signed)S0h * S1l
	sbc	math, zero		; Sign extend
	add	inth, prdl
	adc	matl, prdh
	adc	math, zero

	mulsu	auxh, genl		; (signed)S1h * S0l
	sbc	math, zero		; Sign extend
	add	inth, prdl
	adc	matl, prdh
	adc	math, zero		; Result here

---------

	Thanks,
	Mark Jordan

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.