need a divider command

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i need to divide one register with another
but i can not fibd the divide command in the avr's instruction set
can anyone help me to solve this problems
thank u

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the AVR does not have a divide instruction. You will have to perform the division in software. Check out Atmels appnote AVR200 for code examples on how to do division in software.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Expanding on what Steve said: multiply by reciprocal is very fast when the divisor is constant (and shifting won't suffice). Even with today's desktop CPUs, division is still slow (4 times slower than multiplication on the Pentium family, IA64, and AMD64), though it's faster on some chips.

For example:

result = n / 10;
result = (n * 0x1A) >> 8;  //same result

in assembler, its even simpler:

; to divide r16 by 10:
ldi  r17, $1A
mul  r16, r17
; r1 is the result (no shift required; ignore low byte in r0)

The trick is getting the right constant to multiply by. There are free utilities out there for this, I sometimes use a free utility from AMD to do this, but there's yet another way. Take any modern optimising compiler (gcc, MSVC, etc.), and compile a quick main() that divides a number by a constant. Read the assembler output, and you'll see the required value.

Cheers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i am using avrstudio now
and already read those aplication note but still doesnt understand the algorhtym.
can any one help me giving the simplier code to perfome a divide
thanks a lot guys

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, it's the same as you learned in 4th grade, except in base 2 instead of base 10.

I just used the code straight out of the app note. Divides nicely.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't need a computer science degree to learn binary math. The internet is full of information if you look. :oops:

Here's two links to get you started.

http://home.carolina.rr.com/bigbare/Index.html
http://courses.cs.vt.edu/~cs1104/BuildingBlocks/divide.030.html

Spend a little time comparing the app notes information with the information in these links. Then write a small routine and do some simulations. Once you learn the basics, then you will be able to adapt these to perform larger and more complex operations.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks a lot for the information
already can figure out how to do this