LDX - Load The X Register Command

This is the command you use to load a value into the X register.
The X register is a temporary storage location where you can compare and manipulate a piece of data. Think of it as a second accumulator. It is also used to modify the LDA, STA, BRA, and CMP commands.
Every task has its own X register.

Syntax

LDX x
Where "x" is the value or memory location you want to put into the X register. You may use a raw value (hex or decimal), tilde memory, task port, or persistent memory. You can even load from the message buffer using the @ symbol to specify which byte to load from.
It can be any value from 0-255.

Ways you can load the X register

Examples

Example 1

Basic ways of loading to the X register.

{
LDX #11     //Loads the decimal value of 11 into the X register.
LDX #0x0A   //Loads the Hex value of 10 into the X register.
LDX ~2      //Loads whatever is stored in tilde memory ~2 into the X register.
LDX ^35,1   //Lux level Hi byte into the X register.
LDX ^10,0   //Loads keyboard port. (check what button was pressed)
LDX $10     //Load value from persistent memory location 10.
LDX $Area4_State  //Load value from persistent memory location named "Area4_State".
LDX #PrimaryArea  //Load value of the constant named "PrimaryArea".
LDX @3      //Loads the opcode from the message buffer into the X register.
}

Example 2

This example shows how you can use the X register as a second accumulator.

{
...
DimDownLoop:
    LDA ~15          //Load what Bank we are in.
    CMP #0x06        //If we are already at 0% we aren't getting any Dimmer.
    BRZ DimDownEnd
    CMP #0x05
    BRZ Down10
    BRA Skip
    Down10:
    LDX #DimToOFF    //Load our Dim to off setting. 0=no, 1=yes.  We use the X Reg so the Accululator can hold bank.
    CMPX #0x00       //Is it a no?
    BRZ DimDownEnd   //If so end. If not continue.
    Skip:
    Inc              //Increase it by 1.
    STA ~15          //Store the new value back into the message buffer bank byte.
    Tx ~10,2,7       //Then we send it out onto the network.
    Delay(RampDelay)
    BRA DimDownLoop

    DimDownEnd:
        Null
...
}