SWAP- Swap ACC and XReg Command

Swaps the value in the accumulator with the value in the X register.
If the X register contains a value of zero the SWAP command will cause the Zero Flag to be set.
This command is particularly helpful when using an array and the jump command or temporarily storing something in the X register so you can use the accumulator again.
This is a combination of the TXA and TAX commands.

Syntax

SWAP
Notice that there are no values attached to the command. It simply swaps the values of the accumulator and X register.

Examples

Example 1

Some simple examples of using SWAP.

{
LDA ~5   //Copy the value in tilde 5 to the Accumulator
ADD #10  //Add the fixed decimal value 10 to the value in the Accumulator
SWAP     //Swaps the new value to the X Reg.

LDA ~5   //Copy the value in tilde 5 to the Accumulator
SWAP     //Moves the value to the Xreg.
...
}

Example 2

Stores an incoming preset value if daytime.

{
Start(0x1c,x,x,x,x,x,x) //Trigger on all 1c messages
Copy @0,~0,7 //When we see a message we copy the first 7 pieces of data into the memory
LDA ~3          //Load the opcode (Preset)
SWAP            //Move it into the X Reg.
LDA $2          //Load the time of day you stored into persistent memory 2.
CMP #1          //Is it "day"?
BRZ StorePreset //Branch to the label "Store Preset".
Null            //If not daytime end.
StorePreset:
    STX ~8      //Store the preset into ~8 for later reference.
    Null
}

Example 3

Loading from an array using the X register.

PresetOpcodeArray: Send2(0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D)
{
...
LDA ~15     //Load the preset number we want to send
ADD #4      //Add 4.  Arrays have an index of 4
SWAP        //Swap it into the X Register
LDA PresetOpcodeArray,x   //Grab the correct opcode from the array
STA ~3      //Store it into the opcode byte of the classic preset message.
TX ~0,2,7   //Send the preset message
Null
...
}