Title: Branches and Loops
1Branches and Loops
2Loops and Branches
- Loops and other control structures are made using
a GoTo-type instruction - Branches in the HC11
- Unconditional branches use the BRA or JMP
instruction
ORG 2300 BRA SPIN skip next
instruction LDAA 0 too bad were not
executing this... SPIN ADDA 1 keep adding to
acc. A JMP SPIN do it again, please...
3Branch Addressing
- Branches use relative addressing
- Specifies the destination address for the branch
- Expressed as a number of bytes away from the
beginning of the NEXT instruction - In eight-bit twos complement form
2302 2 2304
2300 ORG 2300 2300 20 02 BRA SPIN 2302 86
00 LDAA 0 2304 8b 01 SPIN ADDA 1 keep adding
to acc. A 2306 20 fc BRA SPIN do it
again, please... 2308 16 TAB this will
never be executed
2308 -4 2304(fc -4 in 2s comp.)
Note JMPs use absolute addressing...
4Condition Codes
- Condition codes are set by most arithmetic
operations according to the result of the
operation - ADDA 2020 if carry is produced, Carry CC is
set - Condition codes are valid on the instruction
after they are set - They remain valid until they are changed by
another instruction
Sets Condition codesC, Z, N, V, and H
ADDA 2020 BCC FOO
Looks at the C cond.code from the ADD
- Basic condition codes
- C - Carry bit (for arithmetic operations)
- Z - Zero (true if operation produced zero)
- N - Negative (true if operation produced a
negative result) - V - Overflow (for arithmetic operations)
- H - Half Carry from bit 3 to 4
5Bxx Instructions
Instructions below make sense if the previous
instruction subtracted N1 - N2
For Unsignedarithmetic
For 2s Comp.arithmetic
6The Compare Instruction
- The compare instruction is used to set the
condition codes - CMPA 2030
Sets the Condition Codes As if A - mem2030 had
been performed
- CMP prepares the CCs for a following Conditional
Branch instruction
LDAA 0 LOOP ADDA 1 CMPA 32 BNE LOOP
Loops until A 32
Checks for Z0
Variants CMPA, CMPB, CBA, CPD, CPX, CPY
7Using CMP and Bxx
Computes A - ltZgt
Execute the instruction CMPA ltZgt and follow
it by a Bxx instruction, it means
Instruction Branch if BEQ A Z BNE A ¹ Z BLT A
lt Z BLE A Z BGT A gt Z BGE A ³ Z
Instruction Branch if BEQ A Z BNE A ¹ Z BLO A
lt Z BLS A Z BHI A gt Z BHS A ³ Z
For Unsigned Integers.
For Signed Integers.
Bxx instructions are most meaningful after a CMP
instruction
8The TST instruction
TSTA sets CCs as if A - 0 had been executed
TSTA is the same as CMPA 0
Instruction Branch if BEQ A 0 BNE A ¹ 0 BLT A
lt 0 BLE A 0 BGT A gt 0 BGE A ³ 0
Note Only BEQ, BNE make sense for unsigned ints
Variants TST ltoprgt, TSTA, TSTB
9Other Instructions
- INC, DEC - Increment or Decrement by 1
- CLR - clear (set to zero)
- NEG - negate
- BRCLR, BRSET - Branch if values specified in mask
are clear (zero) or set (one) - BRSET 20 81 SKIP branches if bits 0 and
7 of Mem20 are set - BRCLR 5,X 04 SKIP
- Supports only direct and indexed modes
- Some assemblers may have different syntax
81 10000001
- SWI - Software Interrupt
- Causes the program to stop and returns to the
Buffalo monitor
10Loop Execution Time
executed 10000 times
Time 3 (10000 10) cycles 1/2MHz
(100003 cycles) 5E-7 s 0.0500015 s 50 ms