Manual error - on goto and branch

radiosparks

Member
Hi

I've been reading the manuals, front to back to front :geek: Yup, the smiley looks like me.
Could it be a coding error found in the sample code of the online and PDF manuals?

Example for ON GOTO, BRANCH has the same issue.

The label bnt0 will never be executed, because, variable b1 is incremented (INC b1) before the ON GOTO or BRANCH statement.

Code from online manual:::
Code:
reset1:
    let b1 = 0
    low B.0
    low B.1
    low B.2
    low B.3

main:
    inc b1
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4

btn0:
    high B.0
    goto main

btn1:
    high B.1
    goto main

btn2:
    high B.2
    goto main

btn3:
    high B.3
    goto main

btn4:
    high B.4
    goto main
Here is the amended code, I think it should be:::

I've placed the (INC b1) after the ON b1 GOTO. Also added a GOTO MAIN to close the loop, in case of a fall through to the btn labels below.

Code:
reset1:
    let b1 = 0
    low B.0
    low B.1
    low B.2
    low B.3

main:
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4
    inc b1
    goto main
    
btn0:
    high B.0
    goto main

btn1:
    high B.1
    goto main

btn2:
    high B.2
    goto main

btn3:
    high B.3
    goto main

btn4:
    high B.4
    goto main
Does this help anyone??
 

Aries

New Member
This question has come up before - see this thread
As I said at the time, it could be deliberate to make the point that GOTO counts from zero and not 1
 

tmfkam

Senior Member
In the original code, wouldn't btn0 be executed after btn4?

b1 would initially be 0, incremented to 1, btn1 would be "branched" to, b1 incremented, then btn2, btn3, btn4. As b1 is next incremented it would be greater than 4 so reset to 0 and btn0 "branched" to. The sequence would then repeat from there.
 

radiosparks

Member
OOPs my mistake, the inc b1 would never be executed. Would be stuck in a loop with b.0 high all the time.

Here is the correct code (tested, working) . In reset1: BNT4 was missing and the reset value for b1 should be -1.

Code:
#picaxe 14m2

reset1:
    let b1 = -1
    low b.0
    low b.1
    low b.2
    low b.3
    low b.4

main:
    inc b1
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4

btn0:
    high b.0
    goto main

btn1:
    high b.1
    goto main

btn2:
    high b.2
    goto main

btn3:
    high b.3
    goto main

btn4:
    high b.4
    goto main
 

tmfkam

Senior Member
No - because Reset1 sets it to zero, and it is then incremented in Main before the branch.
Code:
main:
    inc b1
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4
I was reading this section, posted as the original?

With the 'reset1' directly above the 'on' goto' code I would expect 'btn0' to be executed, but last as opposed to first.
 

Aries

New Member
From Post#1:

Code:
reset1:
    let b1 = 0
    low B.0
    low B.1
    low B.2
    low B.3

main:
    inc b1
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4
reset1 is directly above main, which increments b1 before the on..goto
 

tmfkam

Senior Member
From Post#1:

Code:
reset1:
    let b1 = 0
    low B.0
    low B.1
    low B.2
    low B.3

main:
    inc b1
    if b1 > 4 then reset1
    on b1 goto btn0, btn1, btn2, btn3, btn4
reset1 is directly above main, which increments b1 before the on..goto

Aaahh... I see what you're saying there. [At last]
I was expecting an implied 'return' would happen after the 'low b.3' command. Which doesn't happen. Possibly I was sidetracked by the lack of the 'goto' before 'reset1'.

I stand corrected.
 
Top