Maximum looping of a delay without nesting - Page 1
Forum section: electronics / general

author: abi072
author_rank: Newbie
timestamp: March 31, 2013, 02:18:07 pm
content: MOV R0,#05HHERE:DJNZ RO,HEREEND//THE ABOVE LOOP RUNS FOR 5 TIMES.MOV R0,#0FFHHERE:DJNZ R0,HEREENDBUT, I READ IN A BOOK THAT THE ABOVE PROGRAM CAN RUN FOR 256 TIMES. HOW IS THAT POSSIBLE? CAN ANYONE EXPLAIN?

author: digsys
author_rank: Unknown
timestamp: March 31, 2013, 02:27:25 pm
content: Incorrect - 1st example runs 4 timesIncorrect - 2nd example you load R0 with 0h to get 256 loopsDJNZ means Decrement FIRST and if = 0, DON'T jump. Run it step by step

author: abi072
author_rank: Newbie
timestamp: March 31, 2013, 02:50:40 pm
content: Thanks for your response,"DIGSYS".But,The attached image shows that R5 has to be initialized to FFh for the loop to repeat 256 timesAnd the example 3.1 of next image says that looping will be for 10 times if initialized to 10.Please explain that. I'm really confused.

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 31, 2013, 03:05:06 pm
content: MOV R0,#05HHERE:DJNZ RO (4) ,HEREHERE:DJNZ RO (3) ,HEREHERE:DJNZ RO (2) ,HEREHERE:DJNZ RO (1) ,HEREHERE:DJNZ RO (0) ,HEREENDSo, that is 5 times isn't it?The second one repeats 255 times, not 256.

author: abi072
author_rank: Newbie
timestamp: March 31, 2013, 03:18:53 pm
content: Ya, I agree to you PAOPBZ.But, the images I have posted tells a different story for my second example

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 31, 2013, 03:21:59 pm
content: Quote from: abi072 on March 31, 2013, 03:18:53 pmSo the example is wrong.

author: abi072
author_rank: Newbie
timestamp: March 31, 2013, 03:38:51 pm
content: okay. thanks

author: digsys
author_rank: Unknown
timestamp: March 31, 2013, 10:43:25 pm
content: Quote from: PA0PBZExcept on (0) there is no Jump, it's the exit point. So the routine that would "normally" live in that loop does NOT get executed !!Which is the whole point of the djnz AND one of it's drawbacks ie You CAN'T use it like a normal index > 00, always have to +1 (or -1)So to get 256 LOOPS, you load with 00(0?? you say) .. yes because the VERY FIRST instruction is -1cnt ! so the FIRST compare is FFhAnd abi ... there are MANY MANY cases of examples being wrong or mis-leading. Trust your own logic

author: abi072
author_rank: Newbie
timestamp: April 01, 2013, 05:14:14 am
content: If I modify the example a bit and write it as:Mov A,#00hMov R0,#05hhere: Inc aDJNZ R0,hereendThen after executing the above program the value at A=05.So,Initially R0=5,A=01: A=1. then R0=4. As 4!=0, it goes to here.2: A=2. R0=3. As 3!=0, it goes to here3:A=3. R0=2. As 2!=0,ite goes to here4:A=4.R0=1. As1!=0, it goes to here5.A=5. R0=0. 0=0. And it terminates.LOOP means having a common start and end points. RIGHT?? In the 5th case, it doesn't jumps. AGREED. But, The looping takes place 5 times.Because A=5.Is this logic of mine right?

author: digsys
author_rank: Unknown
timestamp: April 01, 2013, 05:40:36 am
content: Well, technically you're correct - yes it does pass that point 5 times ! But you shouldn't think like that -The purpose of these loops is to run a routine (the actual loop) an exact number of times, in this case 4 !!There are other types of indexed loops that WILL run it 5 times ! So, it's important to keep that clear in your head.So when you see a DJNZ Loop, remind to yourself - this will run #val -1 ! It's very easy to miss it and spend hourswondering why things don't work like they should.

author: andersm
author_rank: Super Contributor
timestamp: April 01, 2013, 12:27:15 pm
content: Quote from: digsys on April 01, 2013, 05:40:36 amThis is incorrect. The jump will betaken(count-1) times, but the instruction will bereached(count) times. If the DJNZ placed at the bottom of the loop, every time the it's reached counts as an iteration.Expressed in C, a typical DJNZ loop looks likeCode:do {// whatever} while (--count);

author: digsys
author_rank: Unknown
timestamp: April 01, 2013, 12:38:17 pm
content: Quote from: andersmUmmmm that's what I said -Quote from: digsys

author: andersm
author_rank: Super Contributor
timestamp: April 01, 2013, 01:52:20 pm
content: You said the loop body will be executed 4 times, which is clearly not the case.