; Div16_8B ; Version 1.0 ; Date: November 11, 2014 ; Written By : Yoseph Yegezu .INCLUDE .CSEG .DEF Denominator=R19 .DEF NL=r24 .DEF NH=r25 .DEF QL=r21 .DEF QH=r22 .ORG 0x0000 ldi r16,0xAA mov NH,r16 mov NL,r16 ldi Denominator, 0x55 //Call the 16 bit by 8 bit division rcall Div16_8 ret /************************************ * subroutine divides unside 16bit by 8bit * Quotient = Numerator/Denominator * * r22:r21 = r25:r24 / r19 * ***************************************/ Div16_8: clr r22 clr r21 // loop L1 stops when the numerator - denominator = less than the demoninator L1: //QL is going to increment by 1, everytime L1 loops inc QL //r22 //When QL reaches 255 or 0XFF and then goes back to 0, QH is going to increment by 1 cpi r21,0 brne No_Inc inc QH //r22 No_Inc: sub r24,Denominator // r19 sbc r25,r2 brcc L1 //Since r21 is incremented by 1 when the loop began, after the loop r21 is decremented dec QL //r21 //Notice L1 is going to branch off when the numerator is no lnger divisiable by the denominator //Which means L1 is branching off when r24-denominator results in a negative value. //Therefore, the denominator is going to be added to the r24 after the loop. add r24,Denominator //r24,r19 adc r25,r2 ret