Talk:Learning 6502 assembly
Add topicPurpose of using BPL
[edit source]I am trying to figure out why one would ever use BPL (and BMI). The following example shows an idiom that sees some use:
LDA #1 LDY #$7F LOOP: STA $200, Y DEY BPL LOOP ; If $80 > Y >= 0, branch to LOOP
If we switch BPL to BNE, the loop body will never see the index value of 0. A limitation is that the initial index value can be at most #$80. On the other hand, we can move DEY above the loop body and do without BPL:
LDA #1 LDY #$80 LOOP: DEY STA $200, Y BNE LOOP ; If Y is not 0, branch to LOOP
However, the latter only works because the loop body after DEY does not impact Z flag. If z flag was impacted by the loop body, one would need an additional CPY #0 before the BNE; and thus, using BPL is cheaper.
Another use case seems to be as a test for the most significant bit e.g. in the Atari BASIC floating point representation, where the first of the 6 bytes features the sign and the exponent, and one would surely want to test for that sign.
--Dan Polansky (discuss • contribs) 11:46, 21 August 2024 (UTC)