ATTiny 85 divide a 16 bit number by two

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

Code written in assembly, no C.

LSR is a 8 bit function, is there any code available that shows how to use the LSR function to shift an entire 16 bit word to the right by two?

BADBAUD

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

Use the carry bit. Clear carry first. Do the shift on the high byte and the carry has the bit that dropped out then shift the low byte. Job done.

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

No need to clear the carry bit first:

        lsr     r17     ; Shift MSB, MSB.7 resets, MSB.0 goes to CY
        ror     r16     ; Rotate LSB, CY goes to LSB.7

Job done :)

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Thanks, then I just repeat that twice to divide a 16 bit number by two.

BADBAUD

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

No you don't. The code above does this alone.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Quote:

is there any code available that shows how to use

Find the AVR app note on "16-Bit Arithmetics".
Quote:

no C.


Going at it blind, eh? :twisted:

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.