Logic is too simple. For better understanding i would suggest you somthing.
Use a scientific calci and in hex mode go on adding 0x01 each time. And then study my code. You will have a better understanding.
CJNE R5,#0FFh,end ; Observe this line carefully and think after it