A “data type” describes a set of values. A variable of a given type can have any of a set of values within the range specified for that type.
The intrinsic types for MASM 6.0 are BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, FWORD, QWORD, and TBYTE. These types define integers and binary coded decimals (BCDs); they are discussed in Chapter 6. The signed data types SBYTE, SWORD, and SDWORD are new to MASM 6.0. They are useful in conjunction with directives such as INVOKE (for calling procedures) and .IF (introduced in Chapter 7). The REAL4, REAL8, and REAL10 directives can be used to define floating-point types. See Chapter 6.
Previous versions of MASM have separate directives for types and initializers. For example, BYTE is a type and DB is the corresponding initializer. The distinction has been eliminated for MASM 6.0. Any type (intrinsic or user-defined) can be used as an initializer.
MASM does not have specific types for arrays and strings. However, it allows a sequence of data units to be treated as arrays, and character (byte) sequences to be treated as strings. (See Section 5.1, “Arrays and Strings.”)
Types can also have attributes such as langtype and distance (NEAR and FAR). See Section 7.3.3, “Declaring Parameters with the PROC Directive,” for information on these attributes.
You can also define your own types with STRUCT, UNION, and RECORD. The types have fields that contain string or numeric data, or records that contain bits. These data types are similar to the user-defined data types in high-level languages such as C, Pascal, and FORTRAN. (See Chapter 5, “Defining and Using Complex Data Types.”)
Summary: The TYPEDEF directive defines aliases and pointer types.
You can define new types, including pointer types, with the TYPEDEF directive, which is also new to MASM 6.0. TYPEDEF assigns a qualifiedtype (explained below) to a typename.
NOTE:
The concept of the qualifiedtype is essential to understanding many of the new features in MASM 6.0, including prototypes and the .IF and INVOKE directives. Descriptions of these topics in later chapters refer to this section.
Once assigned, the typename can be used as a data type in your program. Use of the qualifiedtype also allows the CodeView debugger to display information on the type. You cannot use a qualifiedtype as an initializer, but you can use a type defined with TYPEDEF.
The qualifiedtype is any MASM type (such as structure types, union types, record types, or an intrinsic type) or can be a pointer to a type with the form
[[distance]] PTR [[qualifiedtype]]
where distance is NEAR, FAR, or any distance modifier. See Section 7.3.3, “Declaring Parameters with the PROC Directive,” for more information on distance.
The qualifiedtype can also be any type previously defined with TYPEDEF. For example, if you use TYPEDEF to create an alias for BYTE, as shown below, then you can use that CHAR type as a qualifiedtype when defining the pointer type PCHAR.
CHAR TYPEDEF BYTE
PCHAR TYPEDEF PTR CHAR
Section 3.3, “Accessing Data with Pointers and Addresses,” shows how to use the TYPEDEF directive to define pointers.
Since distance and qualifiedtype are optional syntax elements, you can use variables of type PTR or FAR PTR. You can also define procedure prototypes with qualifiedtype. See Section 7.3.6, “Declaring Procedure Prototypes,” for more information about procedure prototypes.
Several rules govern the use of qualifiedtype:
The only component of a qualifiedtype definition that can be forward-referenced is a structure or union type identifier.
If distance is not specified, the right operand and current memory model determine the type of the pointer. If the operand following PTR is not a distance or a function prototype, the operand is a pointer of the default data pointer type in the current mode. Otherwise, the type of the pointer is the distance of the right operand.
If .MODEL is not specified, SMALL model (and therefore NEAR pointers) is the default.
A qualifiedtype can be used in seven places:
Use | Example |
In procedure arguments | proc1 PROC pMsg:PTR BYTE | |
In prototype arguments | proc2 PROTO pMsg:FAR PTR WORD | |
With local variables declared inside procedures | LOCAL pMsg:PTR | |
With the LABEL directive | TempMsg LABEL PTR WORD | |
With the EXTERN and EXTERNDEF directives, EXTERN pMsg:FAR PTR BYTE EXTERN MyProc:PROTO | ||
With the COMM directive | COMM var1:WORD:3 | |
With the TYPEDEF directive | PPBYTE TYPEDEF PTR PBYTE PFUNC TYPEDEF PROTO MyProc |
Section 3.3.1 shows ways to write a TYPEDEF type for a qualifiedtype. Attributes such as NEAR and FAR can also be applied to a qualifiedtype.
You can also determine an accurate definition for TYPEDEF and qualifiedtype from the BNF grammar definitions given in Appendix B. The BNF grammar defines each component of the syntax for any directive, showing the recursive properties of components such as qualifiedtype.