Xreg - The X Register

The X Register is a temporary storage location for working with data. It stores a single byte of data at a time. Each task has its own X register.
The X register updates the zero and minus flags when updated.
Unlike the data accumulator the X register is also used for some special operations to dynamically modify commands such as CMP and BRA.

The X register is not destroyed when compared to or stored from. You can do multiple comparisons until you find the match or relation you are looking for or store the same value to multiple places.
Commands that modify the X Register
Commands that can be dynamically modified by the X Register
  • LDA - Load a value into the accumulator

  • CMP - Compare to the value in the accumulator

  • STA - Store from the accumulator

  • BRA - Branch Always

  • LDX - Load a value into the X register

  • CMPX - Compare to the value in the X register

  • STX - Store from the X register

Examples

{
LDX #4          //Load a value of 4 into the X register
CMPX ~11        //Compare the value in ~11 with the value in the X register
STX $44         //Store the value in the X register into persistent memory location $44
LDX #0          //Load a value of zero and set the zero flag
SWAP            //Swap the value in the x register and the X register
}

Example 2

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
...
}

Example 3

This task uses the X Reg for a jump table

Task2()
{
    Name="Linear Preset Jump Table"
Start(0x1c,10,x,0x65,x,x,x) //Start on an area 10 linear preset message
Copy @0,~0,7                //Copy the message to user ram
LDA ~2          //Load the preset number into the accumulator
MUL 4           //Multiply the value by 4. This creates an offset of 4
TAX             //Transfer the value in the accumulator to the x register
BRA PJumper,x   //Jump to the label plus the value in the x register
Null
PJumper:        //This is the jump table. The task will jump to the specified row.
    BRA P1
    BRA P2
    BRA P3
    BRA P4
    BRA P5
Null
      P1:
      //Do something for Preset 1
      Null
      ...
}

Example 4

Using the X register to offset the memory we store to.

#const AreaTracker ~0   //The origin for tracking what preset and area is in.
{
Start(0x1c,x,x,0x65,x,x,0xFF)   //Start off any linear preset.
LDX @1                          //Load the area into the X register
LDA @2                          //Load the preset into the accumulator
STA AreaTracker,x               //Store the preset value into the tilde memory with the same number as the area.
}

Example 5

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.
...
}