This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: c A.H.Dixon CMPT 150: Week 12 (Mar 26 - 28, 2008) 101 38 BRANCHING AND PROGRAM STRUCTURE Branching is the second of the programming constructs identified by Bohm and Jacopini as being necessary in any programming language. A branching statement is used when a choice must be made as to what instruction should be executed next. The most general form of branching has the following organiziation: IF condition, (true or false) THEN Steps to perform if condition is true ELSE Steps to perform if condition is fales END Steps to perform regardless of the condition, after the test is made. The HC-12 assembly language provides a number of branching instructions, among them: • BEQ : Branch if z = 1 • BGE : Branch if n = 0 • BLT : Branch if n = 1 • BNE : Branch if z = 0 • BRA : Branch unconditionally. To use these branch statements effectively in an assembly language program, the general organization of a branch must be implemented in a sequence of assembly language instructions: CMPinst ;to set N, Z in CCR BXXinst IFtrue ;to test condition IFfalse ... ;instructions to perform if condition false ... BRA END ;exit the branch IFtrue ... ;instructions to perform if condition true ... END ... CMPinst can be any instruction that sets the CCR status bits N, Z, etc. This includes the CMPA, CMPB, CPX, and CPY instructions. Bxx denotes any HC-12 branch instruction, including BEQ, BNE, BLT, and BGE. To illustrate, suppose that a location FLAG is to be set to 1 if register A < 0 and set to 0 if register A ≥ 0. This can be implemented as follows: c A.H.Dixon CMPT 150: Week 12 (Mar 26 - 28, 2008) 102 CMPA #0 BLT IFtrue IFfalse CLRB STAB FLAG BRA ENDif IFtrue LDAB #1 STAB FLAG IFend ... Other ways may exist, depending on circumstances: 1. Assume the true case. Then test to see if the condition is true, and if not, then change the outcome to the false case: LDAB #1 STAB FLAG CMPA...
View Full Document
This note was uploaded on 06/20/2010 for the course CMPT 150 taught by Professor Dr.anthonydixon during the Spring '08 term at Simon Fraser.
- Spring '08