avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] asm in AVR assembler


From: Richard Urwin
Subject: Re: [avr-gcc-list] asm in AVR assembler
Date: Tue, 11 May 2004 09:24:25 +0100
User-agent: KMail/1.5.3

On Tuesday 11 May 2004 8:51 am, Muhammed wrote:
> Is it possible to add or multiply 32 bit numbers in AVR assembler by
> using 8-bit registers? If so, what kind of algorithm can be
> implemented? Thanks in advance

Assuming two numbers in r1-4 and r5-8:

ADD r1,r5
ADC r2,r6
ADC r3,r7
ADC r4,r8

The result is in R1-4.

Multiplication depends on whether you have a hardware multiplication 
instruction, (the smaller AVRs don't.) But it's basically exactly the 
same as you were taught in school:

Given two numbers x and y:

Take the least significant part of x (bit or byte)
Multiply it by y and add that into the result
Shift x right by the part you used (bit or byte), and y left by the same 
amount.
Repeat until you've done the whole thing.

You can optimise for register usage by not shifting y, and instead 
adding the partial result into the right part of the result.

Legend has it that DEC had a multiplication routine that only used the x 
and y registers and returned a double length result in x,y. But the 
algorithm has probably been lost. (Nobody who read it had the slightest 
idea how it worked.)

-- 
Richard Urwin


reply via email to

[Prev in Thread] Current Thread [Next in Thread]