Author Topic: Maximum looping of a delay without nesting  (Read 5097 times)

0 Members and 1 Guest are viewing this topic.

Offline abi072Topic starter

  • Newbie
  • Posts: 5
Maximum looping of a delay without nesting
« on: March 31, 2013, 02:18:07 pm »
MOV R0,#05H
HERE:DJNZ RO,HERE
END
//THE ABOVE LOOP RUNS FOR 5 TIMES.

MOV R0,#0FFH
HERE:DJNZ R0,HERE
END
BUT, I READ IN A BOOK THAT THE ABOVE PROGRAM CAN RUN FOR 256 TIMES. HOW IS THAT POSSIBLE? CAN ANYONE EXPLAIN?
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2239
  • Country: au
    • DIGSYS
Re: Maximum looping of a delay without nesting
« Reply #1 on: March 31, 2013, 02:27:25 pm »
Incorrect - 1st example runs 4 times
Incorrect - 2nd example you load R0 with 0h to get 256 loops
DJNZ means Decrement FIRST and if = 0, DON'T jump. Run it step by step
Hello <tap> <tap> .. is this thing on?
 

Offline abi072Topic starter

  • Newbie
  • Posts: 5
Re: Maximum looping of a delay without nesting
« Reply #2 on: March 31, 2013, 02:50:40 pm »
Thanks for your response,"DIGSYS".
But,
The attached image shows that R5 has to be initialized to FFh for the loop to repeat 256 times

And 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.
« Last Edit: March 31, 2013, 03:06:03 pm by abi072 »
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5508
  • Country: nl
Re: Maximum looping of a delay without nesting
« Reply #3 on: March 31, 2013, 03:05:06 pm »
MOV R0,#05H
HERE:DJNZ RO (4) ,HERE
HERE:DJNZ RO (3) ,HERE
HERE:DJNZ RO (2) ,HERE
HERE:DJNZ RO (1) ,HERE
HERE:DJNZ RO (0) ,HERE
END

So, that is 5 times isn't it?  8)

The second one repeats 255 times, not 256.
« Last Edit: March 31, 2013, 03:06:56 pm by PA0PBZ »
Keyboard error: Press F1 to continue.
 

Offline abi072Topic starter

  • Newbie
  • Posts: 5
Re: Maximum looping of a delay without nesting
« Reply #4 on: March 31, 2013, 03:18:53 pm »
Ya, I agree to you PAOPBZ.

But, the images I have posted tells a different story for my second example
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5508
  • Country: nl
Re: Maximum looping of a delay without nesting
« Reply #5 on: March 31, 2013, 03:21:59 pm »
But, the images I have posted tells a different story for my second example

So the example is wrong.
Keyboard error: Press F1 to continue.
 

Offline abi072Topic starter

  • Newbie
  • Posts: 5
Re: Maximum looping of a delay without nesting
« Reply #6 on: March 31, 2013, 03:38:51 pm »
okay. thanks  :-+
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2239
  • Country: au
    • DIGSYS
Re: Maximum looping of a delay without nesting
« Reply #7 on: March 31, 2013, 10:43:25 pm »
Quote from: PA0PBZ
MOV R0,#05H
HERE:DJNZ RO (4) ,HERE
HERE:DJNZ RO (3) ,HERE
HERE:DJNZ RO (2) ,HERE
HERE:DJNZ RO (1) ,HERE
HERE:DJNZ RO (0) ,HERE
END
So, that is 5 times isn't it?  8)
Except 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 FFh
And abi ... there are MANY MANY cases of examples being wrong or mis-leading. Trust your own logic
Hello <tap> <tap> .. is this thing on?
 

Offline abi072Topic starter

  • Newbie
  • Posts: 5
Re: Maximum looping of a delay without nesting
« Reply #8 on: April 01, 2013, 05:14:14 am »
If I modify the example a bit and write it as:

Mov A,#00h
Mov R0,#05h
here: Inc a
DJNZ R0,here
end

Then after executing the above program the value at A=05.

So,

Initially R0=5,A=0

1: A=1. then R0=4. As 4!=0, it goes to here.
2: A=2. R0=3. As 3!=0, it goes to here
3:A=3. R0=2. As 2!=0,ite goes to here
4:A=4.R0=1. As1!=0, it goes to here
5.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?
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2239
  • Country: au
    • DIGSYS
Re: Maximum looping of a delay without nesting
« Reply #9 on: April 01, 2013, 05:40:36 am »
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 hours
wondering why things don't work like they should.
Hello <tap> <tap> .. is this thing on?
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: Maximum looping of a delay without nesting
« Reply #10 on: April 01, 2013, 12:27:15 pm »
So when you see a DJNZ Loop, remind to yourself - this will run #val -1 !
This is incorrect. The jump will be taken (count-1) times, but the instruction will be reached (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 like
Code: [Select]
do {
    // whatever
} while (--count);

Offline digsys

  • Supporter
  • ****
  • Posts: 2239
  • Country: au
    • DIGSYS
Re: Maximum looping of a delay without nesting
« Reply #11 on: April 01, 2013, 12:38:17 pm »
Quote from: andersm
This is incorrect. The jump will be taken (count-1) times, but the instruction will be reached (count) times
Ummmm that's what I said -
Quote from: digsys
... yes it does pass that point 5 times .... (the actual loop) .... in this case 4 .... 
Hello <tap> <tap> .. is this thing on?
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: Maximum looping of a delay without nesting
« Reply #12 on: April 01, 2013, 01:52:20 pm »
You said the loop body will be executed 4 times, which is clearly not the case.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->