not logged in | [Login]
Label's and the branch (and branch_if ... branch on result of a comparison) functions are the equivalent of 'goto' in other simple languages ... they are used to implement programme logic.
label(LABEL) - target of a branch or conditional branch (branch_if - bxx function), LABEL is of your own devising.
b(LABEL) - branch to (goto a) named label
Example;
@micropython.asm_thumb
def test():
mov(r0, 42) #put 42 in register 0
b(END) #branch to label END
mov(r0, 666) #put 666 in register 0
label(END)
>>> test()
42
r0 never got set to 666, because we skipped past that operation to go to **label(END)***
'branch on logic' (aka if aka 'make a decision') is a 2 step operation;
cmp(reg, val) compare reg with val (reg-val), put the result into CPSR flags
cmp(regA, regB) compare regA with regB (regA-regB), put the result into CPSR flags
cmn(regA, regB) compare regA with regB (regA+regB), put the result into CPSR flags doesn't this turn the branch comparisons upside down? if so it's "gnarly"
tst(regA,regB) compare regA with regB (regA®B), put the result into CPSR flags
CPSR flags are some magic place for holding the result of a comparison, that is 'inspected' by the bxx operations in the next step, the branch_if instruction.
bgt(LABEL) branch if greater than
Example;
@micropython.asm_thumb
def greater_than_life(r0):
cmp(r0, 42)
bgt(GT) #r0>42 goto END
mov(r0, 0) #r0 = 0 (False in uPy)
b(END) #goto END
label(GT)
mov(r0, 1) #r0 = 1 (True in uPy)
label(END)
>>>greater_than_life(15)
0
>>>greater_than_life(42)
0
>>>greater_than_life(43)
1
beq(LABEL) branch if equal
bne(LABEL) branch if not equal
bge(LABEL) branch if greater than or equal to (>=)
bgt(LABEL) branch if greater than (>)
blt(LABEL) branch if less than (<)
ble(LABEL) branch if less than or equal to (<=)
bcs(LABEL) branch if ??? no clue ???
bcc(LABEL) branch if ??? no clue ???
bmi(LABEL) branch if ??? no clue ???
bpl(LABEL) branch if ??? no clue ???
bvs(LABEL) branch if ??? no clue ???
bvc(LABEL) branch if ??? no clue ???
bhi(LABEL) branch if ??? no clue ???
bls(LABEL) branch if ??? no clue ???
Last edited by Peter Hinch, 2015-03-11 14:28:13