Topic: DMD0309

Help File Version: 2.9.4.37

Casting


Strong Typing

Like most modern programming languages, Do-more's program data is strongly typed. Strong typing means that the controller is aware of what the size and format of each data element is, and uses the data as appropriate without additional guidance from the user. Data blocks consist of elements of the same type. Structures consist of one or more fields that can be different types. Both blocks and structures are built from a base group of simple data types.

 

Data Types

Size

Range

Format

Bit

1 bit

0 to 1

Unsigned Binary

Unsigned Byte

8 bits

0 to 255

Unsigned Binary

Signed Byte

8 bits

-128 to 127

2's Complement

Unsigned Word

16 bits

0 to 65535

Unsigned Binary

Signed Word

16 bits

-32768 to 32767

2's Complement

Signed DWord

32 bits

-2147483648 to 2147483647

2's Complement

Real

32 bits

-3.4e+038 to +3.4e+38

IEEE-754 Floating Point 32-bit Single Precision

 


Three Types of Cast Operations

In most cases, the built-in typing mechanism is all that is required in your program. There are occasions when it is necessary to reinterpret the data in an element, and we use casting to do this. There are three basic types of cast actions: 1) isolate smaller parts of Byte, Word, or DWord elements, 2) aggregate multiple Bit, Byte, or Word elements into a larger element, or 3) reinterpret the format of the element. Casting only works on block data types; fields of structures cannot have a cast. A cast is appended to the end of a block reference, expressed as the cast operator ':', followed by the cast definition.

Extraction Casts - Isolates sub portions of a larger element

Access Size

Syntax

Range

Description

Bit

:#

# = 0 to 7 (Bytes), 0-15 (Words), 0 to 31 (DWords)

Isolates a Bit from a Byte, Word, or DWord element

Byte

:B#

# = 0 to 1 (Words), 0 to 3 (DWords)

Isolates a Byte from a Word or DWord element

Word

:W#

# = 0 to 1 (DWords)

Isolates a Word from a DWord element

Aggregation Casts Groups multiple elements into a larger element

Access Size

Syntax

Description

Byte

:B

Combines 8 bits into a Byte

Word

:W

Combines 16 bits or 2 Bytes into a Word

DWord

:D

Combines 32 bits, 4 Bytes, or 2 Words into a DWord

Format Casts - Reinterprets the format of data within the element or cast

Format

Syntax

Description

Unsigned Integer

:U

Reinterprets data as Unsigned Integer

Signed Integer

:S

Reinterprets data as Signed Integer

Real

:R

Reinterprets data as Floating Point

 

Extraction and aggregation cast operators can be combined with format operators, and generally are in practice. There is only one valid possibility for the format of a bit, but with any of the non-bit extraction and aggregation casts, it is perfectly acceptable to add a format designator. The floating point operator 'R' implies DWord size and cannot be used with any other size cast. Aggregation casts must be applied on boundaries of the cast's size: Bytes on Byte boundaries, Words on Word boundaries, and DWords on DWord boundaries.

 

Casts can be used with array references, but certain caveats apply. Because it is impossible to know beforehand whether an aggregation cast will be properly aligned, the Do-more controller handles this at runtime. If your index value results in an unaligned cast, the operating system will force alignment to the appropriate boundary and complete the data access. It will then set a system warning bit $IndexRealigned (ST151) you let you know that something wasn't quite right and the system was forced to adjust it.

 

The Element Browser dialog contains a very helpful tool called 'Cast Builder'. You can access it by pressing the 'More >>' button in the lower right corner of the 'Element Browser'. The cast builder provides an interactive form that helps you mix and match cast operators, and provides error messages to better understand why certain casts are invalid. The user is encouraged to use this tool to become better acquainted with casting rules.

 


Examples of Common Cast Operations

Valid Casts of Bit Elements - 'C', 'X', and 'Y' are Bit blocks

Cast

Example

Description

Notes

:SB

C8:SB

Aggregates C8 through C15 as a Signed Byte

Must be Byte aligned.

:UB

Y24:UB

Aggregates Y24 through Y31 as an Unsigned Byte

Must be Byte aligned.

:SW

X0:SW

Aggregates X0 through X15 as a Signed Word

Must be Word aligned.

:UW

C16:UW

Aggregates C16 through C31 as an Unsigned Word

Must be Word aligned.

:SD

Y32:SD

Aggregates Y32 through Y63 as a Signed DWord

Must be DWord aligned.

:R

X64:R

Aggregates X64 through X95 as a Real

Must be DWord aligned.

Valid Byte Casts - 'B' is a signed Byte Block and 'U' is an Unsigned Byte block

Cast

Example

Description

Notes

:#

B5:4

Extracts Bit 4 from B5

Bits 0 through 7 are valid

:S

U4:S

Reinterprets U4 as Signed

Using :S with 'B' has no effect

:U

B10:U

Reinterprets B10 as Unsigned

Using :U with 'U' has no effect

:SW

U6:SW

Aggregates U6 to U7 as a Signed Word

Must be Word aligned.

:UW

B10:UW

Aggregates B10 to B11 as an Unsigned Word

Must be Word aligned.

:SD

U12:SD

Aggregates U12 to U15 as a Signed Word

Must be DWord aligned.

:R

B20:R

Aggregates B20 to B23 as a Real

Must be DWord aligned.

Valid Word casts - 'V' is an Unsigned Word block and 'N' is a Signed Word block

Cast

Example

Description

Notes

:#

N21:13

Extracts Bit 13 from N21

Bits 0 through 15 are valid

:SB#

V10:SB1

Extracts Byte 1 from V10 and reinterprets as Signed

Bytes 0 to 1 are valid

:UB#

N55:UB0

Extracts Byte 0 from N55 and reinterprets as Unsigned

Bytes 0 to 1 are valid

:S

V42:S

Reinterprets V42 as Signed Word

Using :S with 'N' has no effect

:U

N13:U

Reinterprets N13 as an Unsigned Word

Using :U with 'V' has no effect

:SD

V20:SD

Aggregates V20 to V21 as Signed DWord

Must be DWord aligned.

:R

N100:R

Aggregates N100 to N101 as a Real

Must be DWord aligned.

Valid DWord Casts - 'D' is a Signed DWord block and 'R' is a Real block

Cast

Example

Description

Notes

:#

D10:23

Extracts Bit23 from D10

Bits 0 to 31 are valid

:SB#

R5:SB2

Extracts Byte 2 from R5 and reinterprets as Signed

Bytes 0 to 3 are valid

:UB#

D73:UB3

Extracts Byte 3 from D73 and reinterprets as Unsigned

Bytes 0 to 3 are valid

:SW#

R11:SW0

Extracts Word 0 from R11 and reinterprets as Signed

Words 0 to 1 are valid

:UW#

D222:UW1

Extracts Word 1 from D222 and reinterprets as Unsigned

Words 0 to 1 are valid

:R

D31:R

Reinterprets D31 as Real

Using :R with 'R' has no effect

:S

D13:S

Reinterprets D13 as Signed DWord

Using :S with 'D' has no effect

 


See Also:

Built-in Data Blocks

 

Structures and their Fields

 

User-Created Data Blocks and Structures

 

Arrays, Pointers and Tables

 

Cast Operations

 

Assignment Operations and Instructions

 

Using Constant Values

 

System Nicknamed Locations

 


Related Topics:

Element Browser

 

Element Picker