• No results found

4.4 Preparation for a Valid Branch Test

In some cases, the branch performs a test that requires the comparison of two bytes of data. For example, BGT is testing to see if one signed number is greater than another 1. Calculate the destination address for the following conditional branch instruction

if the CCR register contains $D3 before the branch is executed.

0181 2A 2C BPL T12

2. Calculate the destination address for the following conditional branch instruction if the CCR register contains $DC before the branch is executed.

01EE 26 05 BNE LAST

3. Calculate the destination address for the following conditional branch instruction.

0177 C6 32 LDAB #$32 0179 CB DE ADDB #$CE 017B 27 A3 BEQ TXNOW

104

signed number. If the first of the two numbers is the greater, the branch test will pass; otherwise, it will fail and the program will fall through. Since all conditional branch instructions test for some condition in the flags of the CCR, these flags must be updated prior to the execution of the BGT in order for a valid comparison to take place. Technically, the BGT instruction does not compare two numbers, but only tests to see if a greater-than condition existed after a comparison was done.

In order to allow the flags to be properly updated prior to a branch instruction that performs a comparative branch test, the HC11 provides a group of compare instructions.

Compare Instructions

Compare instructions are a special set of instructions that only affect status flags in the CCR. No data is changed by any of these instructions. Their job is to compare two numbers and manipulate the flags in the CCR to reflect the relationship of the two numbers. Compare instructions are only executed in preparation for a conditional branch test. Thus, they are used exclusively in conjunction with conditional branch instructions. The comparison of two data words is accomplished by subtracting the two words. Simple logic shows why a subtraction is necessary to complete the compare. For example, let A be the first word and B be the second word. If A > B, then the result of A – B must be a positive number. If A = B, then the result of A – B must be zero; and if A < B, then the result of A – B must be a negative number.

NOTE: A valid comparison of two data words can take place only if the second word is subtracted from the first word.

Remember that the compare instruction is not concerned with the value of the result. It performs the subtraction operation, updates the appropriate flags in the condition code register and then discards the result of the subtraction. A subtract instruction will cause the same status in the CCR; however, the result of the subtract operation will be saved and the data will be changed. Compare instructions are summarized in Figure 4.9.

The CBA instruction is responsible for comparing the data in AccA to the data in AccB. It operates in the inherent addressing mode. CMPA and CMPB compare the data from an 8-bit accumulator to a byte of data in memory. CPD, CPX and CPY compare the data from a 16-bit register to two bytes of data in memory. Each of these instructions operates in the IMM, DIR, EXT, INDX or INDY address modes. The compare

Mnemonic Description Function IMM DIR EXT INDX INDY INH REL S X H I N Z V C

CMPA CMPB Compare Acc to contents of memory (A) - (M) (B) - (M) X X X X X - - - ] ] ] ] CPD CPX CPY Compare 16-bit register to contents of memory (D) - (M):(M+1) (X) - (M):(M+1) (Y) - (M):(M+1) X X X X X - - - ] ] ] ]

CBA Compare A to B (A) - (B) - - - X - - - ] ] ] ]

Figure 4.9 Compare Instructions 105

instructions affect four status flags in the CCR: N, Z, V and C reflect the actual condition of the data after the compare operation.

Example 4.5

Problem: Compare $43 to $56 and update the condition code flags.

Address Machine Code Source Code 01D3 86 43 LDAA #$43 01D5 81 56 CMPA #$56

Solution: The compare instruction will subtract $56 from $43, update the condition codes and then discard the result of the subtraction.

$43 –$56 $ED

$ED is a negative number, thus N is set (N = 1). It is non-zero, thus Z is cleared (Z = 0), there was no sign overflow (the sign of the result is correct), so V is cleared (V = 0) and a borrow was necessary to complete the subtraction, so C is set (C = 1). Compare to Zero Instructions

Often there is a need to compare some value to zero. Rather than use a regular compare instruction, the HC11 provides special instructions that compare only to zero. They work identically to the compare instructions except that they never require that the second value be provided because it is assumed to be zero. These instructions are called test instructions and come in three mnemonic forms: TST, TSTA and TSTB. They are summarized in Figure 4.10.

TSTA and TSTB compare the contents of one of the 8-bit accumulators to the number zero. The TSTx instructions accomplish this by subtracting zero from the value being tested. They operate in the inherent addressing mode. TST compares a byte of data in memory to zero. It operates in EXT, INDX or INDY addressing modes. No support is

Figure 4.10 Compare to Zero Instructions

Mnemonic Description Function IMM DIR EXT INDX INDY INH REL S X H I N Z V C

TSTA TSTB

Test contents of Acc for zero or minus

(A) - $00

(B) - $00 - - - X - - - ] ] 0 0

TST Test contents of memory for zero or minus

(M) - $00

- - X X X - - - ] ] 0 0

106

provided for the DIR addressing mode. The compare instructions affect four status flags in the CCR: N and Z reflect the actual condition of the data after the compare operation. V = 0 and C = 0 because a sign error and borrow condition cannot occur when zero is subtracted from another number.

Example 4.6

Problem: Load a value in AccB and then do a compare to zero.

Address Machine Code Source Code 0102 86 01 LDAA #$01 0104 4D TSTA

Solution: The compare instruction will subtract $00 from $43, update the condition codes and then discard the result of the subtraction.

$01 –$00 $01

$43 is a positive number, thus N is cleared (N = 0). It is non-zero, thus Z is cleared (Z = 0); the V and the C flags are always cleared (V = 0, C = 0).

Self-Test Questions 4.4