Quantcast
Channel: nesdev.org
Viewing all articles
Browse latest Browse all 785

Descending Loop Termination: BPL or BNE?

$
0
0
A lot of tutorials about looping in descending order use BPL, like so:

Code:

        LDY #$13@loop:        LDA apu_regs_init,Y        STA $4000,Y        DEY        BPL @loop
Now, personally I would normally choose BNE instead because in my mental model, I think of "While Y is not 0" rather than "While Y is positive".

So I'm just curious if this is just a question of personal preference, or if there are any other reasons to prefer one or the other?

One thing I can think of is that numbers between $80-$FF would also terminate the loop since those are not positive, so if I have a loop that decrements Y by more than 1 each iteration, I can underflow e.g., from $01 to $FE (DEY 3 times during the loop iteration) - but that seems like a poor choice by default since stopping the loop when it's less than $00 sounds like a bug unless it's deliberate.

Cost in Bytes and Cycles is identical. So yeah, just personal preference?

Statistics: Posted by MenhirMike — Fri Jul 05, 2024 7:31 pm — Replies 1 — Views 63



Viewing all articles
Browse latest Browse all 785

Trending Articles