1 Atari Assembler Course ----------------------------------- 3.1 Comparing and Testing Programming involves many decisions and branches based on the outcome of tests. In Assembly Language, decisions are based on the condition of the processor status flags after an operation. In addition to the logical functions, the Compare and Bit-test instructions are useful means for testing data. 3.1.1 Review of Logical Functions As discussed in Section 2.1.5, the AND, OR, and EOR functions are very useful tools for manipulating data bit-by-bit. But, these functions, and the ADC and SBC instructions, always effect the contents of the accumulator. Very often, a compare or test operation may need to be done several times using the same constant. In this case, the constant would need to be loaded for each test. For this reason, two commands were added that do these operations without saving the results in the accumulator - the CMP and BIT instructions. 3.1.2 Comparing ,-----. | CMP | - Compare Memory with Acc. '-----' A - M This instruction subtracts the contents of memory from the accumulator. Using the CMP effects the zero, negative and carry flags; the accumulator is not effected. The following example compares a value in memory to three constants and branches to a different location for each: LDA $2A0 ; variable to test. CMP #12 ;Is it twelve?... BEQ DOIT12 ;Yes...Go to DOIT12. CMP #66 ;No...then is it 66? BEQ DOIT66 ;Yes...Go to DOIT66. CMP #100 ;No...then is it 100? BEQ DOIT100 ;Yes...Go to DOIT100. ; No...(continue with program.) 3.1.3 Testing Bits... ,-----. | BIT | -- Test Bits in Memory '-----' with Accumulator M AND A The BIT instruction performs a logical AND between a memory location and the accumulator but does not store the result into the accumulator. It does effect the zero, negative and overflow flags. One use of this operation is illustrated below: MASK=1 ;Let's check first bit... LDA #MASK ;Load MASK into Acc. BIT $6F0 ;Check 1st location BNE YES1 ;Go to YES1 if bit1 is "1". BIT $6F1 ;Test second location. BEQ YES2 ;Go to YES2 if the bit is "0". ;etc... 3.1.4 Testing Index Registers There are Compare commands that test the X and Y registers just as the CMP looks at the accumulator. They are CPX and CPY. Here is a simple loop with a comparison to BASIC... In BASIC: 100 FOR I=1 TO 10 110 ...do something... 120 NEXT I In Assembly: 100 LDX #1 ;start at one. 110 LOOP ...do something... 120 INX ;add 1 to X-reg. 130 CPX #11 ;is it above 10? 140 BNE LOOP ;loop if no. 3.2 Branching Instructions The following instructions allow branching to a new program location depending on the condition of a status flag. If the condition is not met, the program continues with the next instruction. The branch instructions are essentially the IF-THEN-GOTO's of Assembly programming. They all use "Relative addressing" which requires a one byte operand. This specifies the number of bytes and the direction to jump in the program. Because of this one-byte ammount, the branch can only be +127 through -128. Fortunately, the Assembler does this calculation for us when we specify a label as the operand. 3.2.1 BEQ/BNE - Branch if Equal or Not-Equal ,-----. | BEQ | - Branch on Result = Zero '-----' ? Z = 1 ,-----. | BNE | - Branch on Result <> Zero '-----' ? Z = 0 3.2.2 BCC/BCS - Branch if Greater or Less-Than ,-----. | BCC | -- Branch on Carry Clear '-----' ? C = 0 ,-----. | BCS | -- Branch on Carry Set '-----' ? C = 1 3.2.3 Branch if Positive or Negative ,-----. | BPL | -- Branch on Result Plus '-----' ? N = 0 ,-----. | BMI | -- Branch on Result Minus '-----' ? N = 1 Here's an example that looks at the Console switches so that some particular action may take place: CONSOL=$D01F ;Function key location OPTKEY=4 ;Option is third bit. SELKEY=2 ;Select is second bit. STRKEY=1 ;Start is first bit. ;the bit=0 when pressed. FNTEST LDA #OPTKEY ;Load bit4 mask BIT CONSOL ;