Division by 2 (unsigned number) coincides with a shift of all bits by one position to the right. For example, the number 100 is in binary format:
01100100
Moving all positions to the right gives
00110010
which is a binary representation of 50.
The ROR command moves all positions to the right. The new byte MSB will be equal to the old value of the carry flag, and the new value of the carry flag will be equal to the old low byte of the byte.
If a 16-bit number is not specified, just shift the upper and lower bytes of the number to the right:
LSR counter_hi ROR counter_lo
LSR and ROR shift their argument to the right, but LSR makes MSB counter_hi 0 and shifts LSB counter_hi to the carry flag, while ROR makes MSB of counter_lo equal to the (old) LSB counter_hi.
If the number is signed, you need to save the sign bit and make sure that the sign bit of the new number is the same. This is what the first two commands of the code you quoted do. Please note that this works because the number is stored in two additions .
source share