Ending a DJNZ loop prematurely

edited November 2013 in Development
How do you exit a DJNZ loop before its finished, I've tried loading b with zero but cant get it to stop.
Post edited by daveysludge on

Comments

  • edited November 2013
    How do you exit a DJNZ loop before its finished, I've tried loading b with zero but cant get it to stop.

    LD B,1 would make it the last pass*, LD B,0 is the max loop B can do as it overspins on the DJNZ to 255!

    * or if it's a subroutine couldn't you just conditionally exit?

    eg;
    CALL ROUTINE

    ROUTINE
    LD B,n
    LP do stuff
    check thing
    RET Z (or whatever)
    DJNZ LP
    RET
  • edited November 2013
    Cheers R-Tape, the conditional return did the trick!
  • edited November 2013
    R-Tape wrote: »
    LD B,1 would make it the last pass*, LD B,0 is the max loop B can do as it overspins on the DJNZ to 255!

    * or if it's a subroutine couldn't you just conditionally exit?

    eg;
    CALL ROUTINE

    ROUTINE
    LD B,n
    LP do stuff
    check thing
    RET Z (or whatever)
    DJNZ LP
    RET

    Additional info:
    When returning you can reuse the state of the flags to see if you exitted from the condition or end of loop by. Even by the Zero-flag since DJNZ will NOT set the zero flag!!
  • edited November 2013
    Dr BEEP wrote: »
    Additional info:
    When returning you can reuse the state of the flags to see if you exitted from the condition or end of loop by. Even by the Zero-flag since DJNZ will NOT set the zero flag!!
    And you can check the b register when you've returned to see how much of the djnz loop was executed*. This is another good reason why making a different test for exiting is better than setting b to 1.

    *That is, as long as you weren't fiddling with the b register as part of your return test. And even if you were, you'd have needed some way to recover b or you'd have broken the djnz loop when not making the early return.
  • edited November 2013
    And you can check the b register when you've returned to see how much of the djnz loop was executed*. This is another good reason why making a different test for exiting is better than setting b to 1.

    *That is, as long as you weren't fiddling with the b register as part of your return test. And even if you were, you'd have needed some way to recover b or you'd have broken the djnz loop when not making the early return.

    The check isn't needed. The condition of your flag can be reused without ANY check at all.

    Something like this

    CALL ROUTINE
    JR Z, OUTINLOOP ; test was done on ZERO-flag!
    BIS0:
    NOP ; other, B = 0 from end of loop
    
  • edited November 2013
    Dr BEEP wrote: »
    The check isn't needed. The condition of your flag can be reused without ANY check at all.

    Something like this

    CALL ROUTINE
    JR Z, OUTINLOOP ; test was done on ZERO-flag!
    BIS0:
    NOP ; other, B = 0 from end of loop
    
    :)

    Yes, of course, if ROUTINE conveniently sets a flag one way or another before returning then you can reuse it without having to do other checks.

    I wasn't suggesting that you'd want to do other checks, I was just suggesting that b could have some additional uses after exiting the djnz as long as you don't exit the loop by setting b = 1 and continuing to the next djnz. If b = 0 then the loop completed all iterations, if nz then it terminated early etc etc
Sign In or Register to comment.