To program 8051 using djnz loop I know that if we give r0 a number the loop will be executed accordingly. For example:

Mov r0,#50
Back: instructions
Djnz r0,back

The loop will be executed 50 times.

But if I want to place djnz in the beginning should I initialize r0 as 51 to make the loop instructions skipped 50 times?

For example:

Ex: djnz r0, skip
Skip: end

I want to pass the instructions 50 times for an interrupt ISR. So can you tell me which number I should use?


1 Answer 1


It's really quite simple. the djnz instruction decrements the value of a register by one and then executes the jump if the result was nonzero. So, yes, if you want to skip a block of code 50 times, you need to set the register value to 51 to start with.

Note that if the register value is 1, the decrement will produce a result of 0 and the jump will NOT be taken. Therefore, in order to skip the code once, you need to start with a value of 2.

Note also that if the register is 0 to start with, the decrement will result in a value of 255, and the jump WILL be taken.


Not the answer you're looking for? Browse other questions tagged or ask your own question.