Electronic – 24-bit two’s complement division in AVR assembly

assemblyavr

How do I perform a division by 2 of the 24-bit two's complement value in registers r6:r5:r4. (r6 is the most significant byte.)

From what I have this is what I tried:
ASR r6;ASR r5;ASR r4;
but I am not sure whether I did it correctly.

My thought process is that by shifting every bit to the right lets say a 4 bit binary 1110, by shifting to the right from MSB will be 0111. and therefore 0111 is half of 1110. But I am not too sure I have done it correctly.

Best Answer

In the mode that you tried you are just dividing the byte value in each of the three registers by two. To get this to work for multiple bytes like r6:r5:r4 you need to code this in a way that propagates the low bit from the first shift into the next byte and similar for the next byte. The proper instruction sequence to perform this would be:

ASR R6
ROR R5
ROR R4

Read the description of the ROR instruction to understand how the CY bit is used to propagate bits from register to register during the above sequence.