CMP - Compare Command
The CMP command will compare the value currently stored in the accumulator with the argument.
The argument can be either a defined value (decimal or hex) such as #10 or #0x0a, or it can be a reference to a port or memory location.
The result of the comparison is used to make decisions in the task by setting/clearing the zero, carry, and minus flags.
Syntax
Basic Use
CMP x
Where x is the value or memory you want to compare to the accumulator.
The CMP comparison subtracts the value of its argument from the value in the accumulator.
While the result of the comparison is stored in the devices’s memory it does not change the value in the accumulator.
This comparison value is used for branching commands. (BRZ, BNE, BCC, BPL, BMI, BRC)
Comparison |
Zero Flag |
Carry Flag |
Minus Flag |
Equal |
Set |
Cleared |
Cleared |
ACC Greater Than |
Cleared |
Cleared |
Set if difference is greater than 128 |
ACC Less Than |
Cleared |
Set |
Set |
Advanced Use
CMP Label,x
Where "Label" is the Label you want it to compare to offset by the value in the X register.
Arrays have an index of 4 so you must load a value of 5 into the X register to get the first value in the array. An array does not need to be in a task. It can be before or in between tasks in the task editor. |
Examples
Example 1
Basic ways of using the CMP command.
{
CMP #11 //Compares the decimal value of 11 to the accumulator.
CMP #0x0A //Compares the Hex value of 10 to the accumulator.
CMP ~2 //Compares whatever is stored in tilde memory ~2 to the accumulator.
CMP ^35,1 //Compares lux level Hi byte to the accumulator.
CMP ^10,0 //Compares keyboard port to the accumulator.
CMP $10 //Compares value from persistent memory location 10 to the accumulator.
CMP $Area4_State //Compares value from persistent memory location named "Area4_State" to the accumulator.
CMP #PrimaryArea //Compares value of the constant named "PrimaryArea" to the accumulator.
CMP @3 //Compares the opcode from the message buffer to the accumulator.
}
Example 2
Here is a common use of CMP.
{
Start(0x1c,10,x,x,x,x,x) //Watches all incomming 1C messages for area 10.
copy @0,~0,7 //Copy the msg to ~0-6. ~1 is area, ~3 is preset.
LDA ~3 //Get Preset byte (The Op code)
CMP #0x00 //Does the Preset Byte match Preset1?
BRZ P1 //If it matches branch to P1: Label. If not continue.
CMP #0x03 //Does the Preset Byte match Preset4?
BRZ P4 //If it matches branch to P4: Label. If not continue.
Null //Stop. Do not pass go. Do not collect $200.
P1: //This is where you do something if the preset was P1.
Null
P4:
Null
}
Example 3
Here are the different ways you can use CMP.
{
CMP #13 //Compares the value in the accumulator with the decimal value of 13.
CMP #0x0d //Compares the value in the accumulator with the Hex value of 13.
CMP ~11 //Compares the value in the accumulator with the value in tilde memory ~11
CMP $44 //Compares the value in the accumulator with the value in persistent memory $44
CMP $OccupancyState //Compares the value in the accumulator with the value in persistent memory named "OccupancyState"
CMP ^14 //Compares the value in the accumulator with the value in the box number port
CMP #LightingArea //Compares the value in the accumulator with the value in the constant named "LightingArea"
}
Example 3
Comparing with a value in an array using the X register.
PresetOpcodeArray: Send2(0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D)
{
...
LDA ~15 //Load the current preset
ADD #4 //Add 4. Arrays have an index of 4
SWAP //Swap it into the X Register
LDA ~3 //Load the incomming preset
CMP PresetOpcodeArray,x //See if they are the same
BRZ END //If the preset hasn't changed just end.
...
}