Arithmetic & Logic Operations

The TML offers the possibility to perform the following operations with the TML data:

Addition
Subtraction
Multiplication
Division
Left and right shift
logic AND / OR

Except the multiplication, the result of these operations is saved in the left operand. For the multiplication, the result is saved in the dedicated product register. The operands are always treated as signed numbers and the right shift is performed with sign-extension.

Addition: The right-side operand is added to the left-side operand

The left side operand can be:

A 16-bit TML data: TML parameter or user variable
A 32-bit TML data: TML parameter or user variable

The right side operand can be:

A 16-bit immediate value
A 16-bit TML data: TML parameter, variable or user variable
A 32-bit immediate value, if the left side operand is a 32-bit TML data
A 32-bit TML data: TML parameter, variable or user variable, if the left side operand is a 32-bit data too

Programming Examples

       int_var += 10;                // int_var1 = int_var1 + 10

       int_var += int_var2;        // int_var = int_var + int_var2

       long_var += -100;        // long_var = long_var + (-100) = long_var – 100

       long_var += long_var2;        // long_var = long_var + long_var2

       fixed_var += 10.;        // fixed_var = fixed_var + 10.0

       fixed_var += fixed_var2; // fixed_var = fixed_var + fixed_var2

Subtraction: The right-side operand is subtracted from the left-side operand

The left side operand can be:

A 16-bit TML data: TML parameter or user variable
A 32-bit TML data: TML parameter or user variable

The right side operand can be:

A 16-bit immediate value
A 16-bit TML data: TML parameter, variable or user variable
A 32-bit immediate value, if the left side operand is a 32-bit TML data
A 32-bit TML data: TML parameter, variable or user variable, if the left side operand is a 32-bit data too

Programming Examples

       int_var -= 10;                // int_var1 = int_var1 - 10

       int_var -= int_var2;        // int_var = int_var - int_var2

       long_var -= -100;        // long_var = long_var - (-100) = long_var + 100

       long_var -= long_var2;        // long_var = long_var - long_var2

       fixed_var -= 10.;        // fixed_var = fixed_var - 10.0

       fixed_var -= fixed_var2; // fixed_var = fixed_var - fixed_var2

Remark: At addition and subtraction, when the left operand is a 32-bit long or fixed TML data and the right operand is a 16-bit integer value, it is treated as follows:

Sign extended to a 32-bit long value, if the left operand is a 32-bit long
Set as the integer part of a fixed value, if the left operand is a 32-bit fixed

Multiplication: The 2 operands are multiplied and the result is saved in a dedicated 48-bit product register (PREG). This can be accessed via the TML variables: PRODH – the 32 most significant bits, and PROD – the 32 least significant bits of the product register. The result of the multiplication can be left or right-shifted with 0 to 15 bits, before being stored in the product register. At right shifts, high order bits are sign-extended and the low order bits are lost. At left shifts, high order bits are lost and the low order bits are zeroed. The result is preserved in the product register until the next multiplication.

The first (left) operand can be:

A 16-bit TML data: TML parameter, variable or user variable
A 32-bit TML data: TML parameter, variable or user variable

The second (right) operand can be:

A 16-bit immediate value
A 16-bit TML data: TML parameter, variable or user variable

Remark: The result is placed in the product register function of the left operand. When shift is 0:

In the 32 least significant bits, when the left operand is a 16-bit integer. The result is a 32-bit long integer
In all the 48 bits, when the left operand is a 32-bit fixed. The result has the integer part in the 32 most significant bits and the fractional part in the 16 least significant bits
In all the 48 bits, when the left operand is a 32-bit long. The result is a 48-bit integer

Programming Examples

long_var * -200 << 0;           // PROD = long_var * (-200)

fixed_var * 10 << 5;           // PROD = fixed_var * 10 * 25 i.e. fixed_var *320

int_var1 * int_var2 >> 1; // PROD = (int_var1 * int_var2) / 2

long_var * int_var >> 2; // PROD = (long_var * int_var) / 4

long_var = PROD;                   // save 32LSB of PROD in long_var

long_var = PROD(H);           // save 32MSB of PROD in long_var i.e. bits 47-15

 

Division: The left operand – the dividend, is divided by the right operand – the divisor, and the result is saved in the left operand..

The first (left) operand is a 32-bit TML data: TML parameter or user variable.

The second (right) operand is a 16-bit TML data: TML parameter, variable or user variable

The result, saved in the first operand is a fixed value with the integer part in the 16 most significant bits and the fractional part in the 16 least significant bits.

Programming Examples

long_var /= int_var;        // long_var = long_var / int_var

fixed_var /= int_var;        // fixed_var = fixed_var / int_var

 

Left and right shift: The operand is left or right shifted with 0 to 15. The result is saved in the same operand. At right shifts, high order bits are sign-extended and the low order bits are lost. At left shifts, high order bits are lost and the low order bits are zeroed.

The operand can be:

A 16-bit TML data: TML parameter, variable or user variable
A 32-bit TML data: TML parameter, variable or user variable
The 48-bit product register with the result of the last multiplication

Programming Examples

long_var << 3;                //  long_var = long_var * 8

int_var = -16;                //  int_var = -16 (0xFFF0)

int_var >> 3;                //  int_var = int_var / 8 = -2 (0xFFFE)

PROD << 1;                          //  PREG = PREG * 2

Remark: The shifts instructions having PROD as operand are performed on all the 48-bits of the product register.

 

Logic AND / OR: A logic AND is performed between the operand and a 16-bit data (the AND mask), followed by a logic OR between the result and another 16-bit data (the OR mask).

The operand is a 16-bit TML data: TML register, TML parameter or user variable

The AND and OR masks are 16-bit immediate values, decimal or hexadecimal.

Programming Examples

int_var = 13;                // int_var = 13 (0xD)

SRB int_var, 0xFFFE, 0x2;//  set int_var bit 0 = 0 and bit 1 = 1

                                       // int_var = 12 (0xC)

The SRB instruction allows you to set/reset bits in a TML data in a safe way avoiding the interference with the other concurrent processes wanting to change the same TML data. This is particularly useful for the TML registers, which have bits that can be manipulated by both the drive/motor and the user at TML level.

Remark: The SRB instruction, use a short address format for the operand. The short address format requires an operand address between 0x200 and 0x3FF or between 0x800 and 0x9FF. This restriction is respected now by all the predefined or user-defined TML data, hence you can use the above assignment instructions without checking the variables addresses.

However, considering possible future developments, the TML also includes a similar instruction SRBL using a full address format where the operand address can be any 16-bit value. The SRBL command has the following mnemonic:

SRBL TMLvar, 0xFFFE, 0x2;// set bit 0 = 0 and bit 1 = 1 in TMLvar with

// using full addressing

See also:

TML Description