Section 06 Part 03 – The BEQ & BNE Instructions “A gentleman would be ashamed should his deeds not match his words.” ~Confucius

Introduction

These two are conditional branches that will branch depending on the Z flag.  It should be noted that these branch instructions have two sizes; .s for short and .w for word.  For details see Section 05 Part 03 (The BRA Instruction).

The BEQ Instruction

BEQ – Branch on EQual

If the Z flag of the CCR is set, the destination operand will be added to the PC, and the 68k will continue reading at the new offset held in PC.  If the Z flag is clear, the instruction is ignored.

Examples

You’ve seen this one used various times in a few examples, it is quite simple:

 cmpi.w    #\$0020,d0           beq.s     FlagZIsSet           move.w    #\$0000,d0   FlagZIsSet:

The CMP instruction runs a comparison of 0020 and the word inside d0:

• If they match, then the Z flag will be set.  And the 68k will branch to “FlagZIsSet
• If they do not match, then the Z flag will be cleared.  The 68k will continue to “move.w #\$0000,d0

The BEQ instruction will only branch if the Z flag is set.  If it is cleared, it will ignore the branch and continue.

The BNE Instruction

BNE – Branch on Not Equal

If the Z flag of the CCR is clear, the destination operand will be added to the PC, and the 68k will continue reading at the new offset held in PC.  If the Z flag is set, the instruction is ignored.

Examples

This is the opposite of BEQ:

 cmpi.w    #\$0020,d0           bne.s     FlagZIsClear           move.w    #\$0000,d0   FlagZIsClear:

The CMP instruction runs a comparison of 0020 and the word inside d0:

• If they do not match, then the Z flag will be cleared.  And the 68k will branch to “FlagZIsClear
• If they match, then the Z flag will be set.  The 68k will continue to “move.w #\$0000,d0

The BNE instruction will only branch if the Z flag is cleared.  If it is set, it will ignore the branch and continue.