BPL-BMI-BRANCHING-AND-FEW-OTHER-THINGS-FOR-NEXT-LOOP -------------------------------------------------------------------------------------------------- TOOL: ML-Monitor {Commodore C128} Language: Assembler Extra: - Author: Misel Zivanovic Note: (C) 26th FEB 2026 - www.lived.ch, mzretro.ch --------------------------------------------------------------------------------------------------- We know BEQ, BNE, BCS and BCC so far and can use them efficiently or at least we believe it so! ;-) Now there are two more branching fellows, namely BPL and BMI. One is branching on positive result, the other one on negative. And they can be used for the FOR NEXT LOOPS too if it is done smart and, of course, YES WE CAN! Let's see the branching first. a 01300 ad 00 14 lda $1400 a 01303 10 4b bpl $1350 a 01305 30 59 bmi $1360 a 01307 60 rts a 01308 a 01350 a9 05 lda #$05 - green color a 01352 8d 20 d0 sta $d020 a 01355 ee 21 d0 inc $d021 - adding next color to existing value a 01358 60 rts a 01359 a 01360 a9 02 lda #$02 - red color a 01362 8d 21 d0 sta $d021 a 01365 ee 20 d0 inc $d020 - adding next color to existing value a 01368 60 rts a 01369 ------------------------------------ poke 5120,100:sys 4864:rem branching As you can see, we will read the value at the location $1400, dec $5120 and branch then depending on value. Because we are in charge, we can set the value to see how branching is behaving in under our command! The value of 100 is branching to where? GREEN or RED?... - It was GREEN, of course, it is a positive number! poke 5120,150:sys 4864:rem branching - It went to RED screen, but why exactly? 150 looks pretty positive too! Actually, how do you write a negative number in assembler? Sure not like this -100, or!? That is true, -100 won't do the job. In 8 bit the usual suspects are the 8-bit guys and the values are from 0 to 255 = 256 values! The first half of it is considered as a positive area, and the second part is then the negative area. Actually it's that easy to understand what is considered a positive and what a negative number {area}. Accordingly, branching shouldn't be a mystery anymore. So, we got GREEN in first example because that is the first area {BPL}, branch if the result is positive. 150 is beyond the first area and is therefore a negative value. Ergo, we jumped with BMI to RED and BPL was ignored. 0 - 127 = POSITIVE NUMBERS 128 - 255 = NEGATIVE NUMBERS That is clear now! But how do we use it for the NEXT FOR LOOPS? EXAMPLE 1: 15 little balls $80> QQQQQQQQQQQQQQQ <$8e 0123456789abcde a 01300 a2 8e ldx #$8e a 01302 a9 51 lda #$51 a 01304 9d 00 04 sta $0400,x a 01307 ca dex a 01308 30 fa bmi $1304 a 0130a 60 rts a 0130b x ready. sys 4864 As you can see, it will not go below $80, dec 128, because that is a positive area! BMI and BPL can be used for many things in order to save cycles and bytes where needed or just for fun of it! Or even because of why not! Hehe... In this case below, we have following scenario: Screen start address $0400, dec 1024 + #$8e, dec 142 + dex {decrement the value} down to 128 because that is the first negative number and if BMI sees #$7f, dec 127 {positive area}, the loop will end and exit. EXAMPLE 2: {sys 4864 or sys 5120} a 01300 a2 00 ldx #$00 a 01302 a9 ff lda #$ff a 01304 9d 00 0e sta $0e00,x a 01307 e8 inx a 01308 e0 40 cpx #$40 a 0130a d0 f8 bne $1304 a 0130c 60 rts a 0130d this example will fill the first sprite area with #$ff, dec 255 and it will give us a rectangle (as seen in the next screenshot) a 01400 a2 40 ldx #$40 a 01402 a9 00 lda #$ff a 01404 9d 00 0e sta $0e00,x a 01407 ca dex a 01408 10 fa bpl $1404 a 0140a 60 rts this one will do the same, just with two bytes less!