4.4 Collating Sequence Test Program

  This section describes a program to test the collating sequence. Let's assume that two 8-bit data blocks (both unsigned) are stored in the RAM and you create a program to sort them in descending order. The program should also store larger data in the DATA1 (H'2000) address and smaller in the DATA2 (H'2001).
  As instructions to test conditions, the BHI (Branch HIgh) and BGT (Branch Greater Than) instructions have already been described. The BHI tests the collating sequence assuming data to be unsigned and the BGT assuming data to be signed. In combination with the CMP instruction, they are used as follows:

CMP.B R1L,R0L ; Compares the contents of the R1L and R0L
BHI ABC ; Branches to ABC if the contents of the R1L are greater
Instruction ; Otherwise, the next instruction is executed
Instruction
ABC: Instruction

  The BHI is an instruction to perform branching if data is greater. "If data is greater" means "if the data on the right is greater than that on the left based on comparison" by the CMP instruction. Attach a symbol to the instruction you want to branch. If the condition is satisfied, branching forward or backward occurs, skipping some instructions. Otherwise, the next instruction is executed.
  The BHI instruction performs branching when both Z and C flags in the CCR are 0. The CMP instruction subtracts the R1L from the R0L. If Z is 0 as a result of subtraction, two data blocks are not equal. If C is 0, no borrow has occurred. These two conditions are satisfied simultaneously when:
R0L > R1L
  The BHI instruction performs branching when the data on the right (R0L) is greater than that on the left (R1L) based on comparison by the CMP instruction.

The flowchart of this program is shown in Figure 4.3.

List 4.6 shows the source program.

Figure 4.3: Flowchart to Test Collating Sequence

List 4.6: Program to Test Collating Sequence





[Explanation with motion pictures and sound]