Topic: DMD0309 Casting |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Strong TypingLike 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Three Types of Cast OperationsIn 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
Aggregation Casts Groups multiple elements into a larger element
Format Casts - Reinterprets the format of data within the element or cast
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 OperationsValid Casts of Bit Elements - 'C', 'X', and 'Y' are Bit blocks
Valid Byte Casts - 'B' is a signed Byte Block and 'U' is an Unsigned Byte block
Valid Word casts - 'V' is an Unsigned Word block and 'N' is a Signed Word block
Valid DWord Casts - 'D' is a Signed DWord block and 'R' is a Real block
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
See Also:
User-Created Data Blocks and Structures
Cast Operations
Assignment Operations and Instructions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Related Topics:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|