Section 06 Part 07 – Unsigned Branches (BCC, BHI, BLS, BCS)

 

“If you would lift me up you must be on higher ground.” ~Ralph Waldo Emerson

 

 

 

Introduction

 

These branches are designed to branch if larger or smaller than a specific value.  These deal with unsigned results, that is to say for using a byte; 00 is the lowest value whilst FF is the highest.

 

So when comparing say FE to 02.  FE will be considered higher than 02.

 

 

 

The BCC Instruction

 

BCC – Branch on Carry 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 C flag is clear.  Otherwise, the instruction is ignored.

 

 

 

Examples

 

This instruction branches provided the C flag is cleared, interestingly, the C flag is usually clear when the result is “higher than or equal”:

 

          cmpi.w    #$0020,d0

          bcc.s     Is20OrHigher

          move.w    #$0000,d0

 

Is20OrHigher:

 

 

We’ll pretend d0 contains 00009800, the CMP is word, so the comparison is 0020 & 9800.

 

 

Now let’s pretend d0 contains 0000001F.  The comparison is 0020 & 001F.

 

 

You can use the mnemonic BHS (Branch on Higher than or Same) instead of BCC if you wanted to.  BHS is another mnemonic for BCC to make it easier to remember one of its main purposes.  The assembler will convert it to BCC when assembling.

 

 

 

The BHI Instruction

 

BHI – Branch on HIgher than

 

The destination operand will be added to the PC, and the 68k will continue reading at the new offset held in PC, if the C and Z flags are both clear.  Otherwise, the instruction is ignored.

 

 

 

Examples

 

This instruction uses the C and Z flags together to detect if the result is higher than:

 

          cmpi.w    #$0020,d0

          bhi.s     IsHigher

          move.w    #$0000,d0

 

IsHigher:

 

 

We’ll pretend d0 contains 00000020, the CMP is word, so the comparison is 0020 & 0020.

 

 

Now let’s pretend d0 contains 0000F492.  The comparison is 0020 & F492.

 

 

 

 

The BLS Instruction

 

BLS – Branch on Lower than or Same

 

The destination operand will be added to the PC, and the 68k will continue reading at the new offset held in PC, if the C or Z flag is set.  Otherwise, the instruction is ignored.

 

 

 

Examples

 

This instruction is the polar opposite of BCC (BHS):

 

          cmpi.w    #$0020,d0

          bls.s     Is20OrLower

          move.w    #$0000,d0

 

Is20OrLower:

 

 

We’ll pretend d0 contains 00009800, the CMP is word, so the comparison is 0020 & 9800.

 

 

Now let’s pretend d0 contains 00000001.  The comparison is 0020 & 0001.

 

 

 

 

The BCS Instruction

 

BCS – Branch on Carry 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 C flag is set.  Otherwise, the instruction is ignored.

 

 

 

Examples

 

This instruction is the polar opposite of BHI:

 

          cmpi.w    #$0020,d0

          bcs.s     IsLower

          move.w    #$0000,d0

 

IsLower:

 

 

We’ll pretend d0 contains 00000020, the CMP is word, so the comparison is 0020 & 0020.

 

 

Now let’s pretend d0 contains 00000004.  The comparison is 0020 & 0004.

 

 

You can use the mnemonic BLO (Branch on LOwer) instead of BCS if you wanted to.  BLO is another mnemonic for BCS to make it easier to remember one of its main purposes.  The assembler will convert it to BCS when assembling.

 

 

 

BHS & BLO

 

BHS and BLO are mnemonics that can be used in place of BCC and BCS (respectively), but it must be noted that BHS & BCC are the same instruction, and BLO & BCS are the same instruction.  They are simply different names to interpret the same overall function.

 

 

 

Homework

 

At this stage, you should know all about branches, conditional branches, and jumps.  Here is a small section of code which uses a few of the instructions mentioned in the last two sections.

 

          move.w    #$0100,d0

          clr.l     d1

          move.w    #$0400,d4

          clr.l     d2

          move.w    #$1000,d3

 

NotReached:

          addi.b    #$10,d2

          add.w     d0,d1

          cmp.w     d1,d4

          bgt.s     NotReached

          sub.w     d2,d1

          subi.w    #$1000,d3

          bpl.s     NotReached

          move.w    d1,d0

          swap      d0

          move.w    d3,d0

 

What will the contents of d0 be once all of these instructions are processed?  (This is a real tricky one, because it means keeping track of all of the registers and looping around several times, but you know what they say, you have to be cruel to be kind).

 

 

 

 

Previous Part

Main Page

Next Part