5 posts / 0 new
Author
Message

Hi Guys,

Just wondering an easier way to do this:

```	adiw	XL,63				;most we can do here is 63
adiw	XL,1				;so do it twice
```

I am trying to add 64 to X every time in a loop.

Just don't understand why I can only do 63?

The above code works, just thought there might be a better way.

Thanks

This topic has a solution.
Last Edited: Sat. Dec 22, 2018 - 05:41 PM

I don't see a way to do it in less than 2 instructions. There is a more general way, though, that is subtracting -64 (0xFFC0) from XH:XL. Subtracting -64 is equivalent to adding 64, this is needed because there is no "adci" instruction.

```subi XL, 0xC0
sbci XH, 0xFF```

With this method you can add a larger range of numbers than with 2x adiw (and it's faster because adiw takes 2 cycles).

Last Edited: Sat. Dec 22, 2018 - 03:53 PM

newbie123 wrote:
Just don't understand why I can only do 63?
Have you read the AVR Instruction Set document?:

The instruction encoding allows for only 6 bits for the immediate value, which is unsigned.  Hence the range 0-63.

The only reason to use adiw is to save a word of flash when the immediate operand is within that range.  If it is not, use subi and sbci.  It both approaches have the same cycle cost.

El Tangas wrote:

```subi XL, 0xC0
sbci XH, 0xFF```

But:

```subi XL, lo8(-64)
sbci XH, hi8(-64)```

... is clearer.

 "Experience is what enables you to recognise a mistake the second time you make it." "Good judgement comes from experience.  Experience comes from bad judgement." "Wisdom is always wont to arrive late, and to be a little approximate on first possession." "When you hear hoofbeats, think horses, not unicorns." "Fast.  Cheap.  Good.  Pick two." "We see a lot of arses on handlebars around here." - [J Ekdahl]

Last Edited: Sat. Dec 22, 2018 - 05:06 PM
This reply has been marked as the solution.

Quote:

Just don't understand why I can only do 63?

Because that's how many bits (6) the designers decided to allow for the immediate value in the instruction.  Instruction set design and encoding is like tetris, trying to fit the most functionality into the limited number of instruction bits while leaving the fewest holes.

kk6gm wrote:

Quote:

Just don't understand why I can only do 63?

Because that's how many bits (6) the designers decided to allow for the immediate value in the instruction.  Instruction set design and encoding is like tetris, trying to fit the most functionality into the limited number of instruction bits while leaving the fewest holes.