Flylib.com

Books Software

 
 
 

Structures

Structures

The STRUC directive allows you to join several dissimilar data values of different types. These data items are called structure fields. First, it is necessary to define the structure template using the STRUC keyword; then, using the < > directive, it is possible to define any number of structures. Consider the following example:

STRUC COMPLEX
       RE  DD ?
       IM  DD ?
    STRUC ENDS
       ...
    ; In data segment
    COMP1 COMPLEX <?>
    COMP2 COMPLEX <0>
    ; Initialization by zero

Access to the structure fields is carried out by reference points, for example, MOV COMP1.RE , EAX .

Note 

For working with string constants, MASM32 and TASM32 provide a large set of string macros: CATSTR , INSTR , and so on. However, we are not going to practice macro programming in Assembly language, therefore, we won' t consider these possibilities.

Unions

Unions are similar to structures. They also consist of individual records. However, there is one significant difference: The length of the union instance in memory is equal to the length of its longest field. All fields will start with the same address the one, in which the union will start. The sense of a union is that the same memory area can be considered in the context of the same data type.

An example of a union appears as follows :

EXTCHAR   UNION
       ascii     DB ?
       extascii  DW ?
    EXTCHAR ENDS

This union is an extended definition of the ASCII code. To use a union in your program, it is necessary to define the union in a way similar to defining structures.

; In data segment
        easc EXTCHAR <0>

Now, you will have a 2-byte structure. Access to the entire word is carried out through the extascii field: MOV easc.extascii , AX . Access to the least significant byte of the word is through the ascii field: MOV easc.ascii , AL .

A Convenient Method of Working with Structures

There is one convenient method of working with structures and unions that deserves a special mention. It is based on the ASSUME directive. Actually, I make practically no use of this directive in this book. When programming for MS-DOS, this directive is mainly used to inform the assembler that the segment register points to the current segment. However, this directive can be used with more than segment registers. Here is the fragment that illustrates such a technique.

COMPLEX STRUC
            RE   DD ?
            IM   DD ?
    COMPLEX  ENDS
    .
    .
    .
    ; In  data segment
    COMP  COMPLEX <0>
    ; In  code segment
           MOV    EBX, OFFSET COMP
           ASSUME EBX:PTR C0MPLEX
           MOV    [EBX].RE, 10
           MOV    [EBX].IM, 10
           ASSUME EBX:NOTHING

Actually, the MOV [EBX] .RE , 10 command is equivalent to MOV DWORD PTR [EBX] , 10 , and the MOV [EBX].IM , 10 command is equivalent to MOV DWORD PTR [EBX+4] , 10 . You likely will agree that this is very convenient.

Conditional Assembling

Conditional assembling provides the possibility of bypassing specific program fragments when assembling. There are the following types of conditional assembling:

  1. IF
    
    expression
    
    ...
    ENDIF
    
  2. IF
    
    expression
    
    ...
    ELSE
    ...
    ENDIF
    
  3. IF
    
    expression 1
    
    ...
    ELSEIF
    
    expression 2
    
    ...
    ELSEIF
    
    expression 3
    
    ...
    ELSE
    ...
    ENDIF
    

The expression is considered to be satisfied if the expression takes a nonzero value; otherwise , the condition is not satisfied.

MASM and TASM support several special-purpose directives for conditional assembling.

  1. IFE expression
    ...
    ELSEIFE
    ...
    ENDIFE
    
  2. The IF1 and IF2 operators check the first and the second pass of assembling.

  3. The IFDEF operator checks whether a symbolic name is defined in the program; IFDEFN is an inverse operator.

    There are other types of IF operators. The required information can be found in any reference on Assembly language.

  4. There is the large set of directives starting with .ERR . For example, .ERRE will cause the assembling process stop and will display an error message if the condition takes the value 0.

Conditional assembling will be used in the end of this chapter to write the example program that can be translated using both MASM and TASM.