- 8051

8051 code to use R4 (LSB) and R5(MSB) as a single 16-bit counter and decrement the pair until number is 0000h

The logic is easy. Go on decrementing LSB . Let me consider an example.

let the number be 0xffff . this means 0xff in both R4 and R5 registers. go on decrementing R4(LSB).once it reaches 0x00,then decrement R5(MSB) which makes it 0xfe in R5. Then reset the value of R4 to 0xff and follow the same process…
The following code is as follows :
ORG 000h
MOV R4,#0FFh   ;you can store any number u like
MOV R5,#0FFh 
main:
         DJNZ R4,main
         
         DJNZ R5,end    ;There is a specific reason for placing it here *
         DEC R5
         MOV R4,#0FFh 
         SJMP main
end: NOP
END
So why did i place    ‘ DJNZ R5,end  ‘  there, just think,then you go on decrementing msb and it becomes 0x00 , u still have 0xff in lsb. So you still have to execute another loop .

Enjoy coding…..XD 

Leave a Reply