Chapter 12: Assembly Language Macro Tools and Directives

Well, you have finally made it to macros, although I tried to delay this moment as long as I could. The main part of this chapter can be classified as reference material. Why do I provide reference information in the middle of the book instead of the opening chapters? I am convinced that reference material in the first chapters may take away any interest you have in reading a book. Most material provided in this chapter is already known to you; consequently, you won't encounter any difficulties understanding it. Furthermore, the macros being considered here, in my opinion, prevent beginner programmers from feeling the beauty of the Assembly language. I have one practical goal: I want you to write programs that can be translated using both MASM and TASM.

Labels

A label followed by a colon defines the address of the command that follows the label.

The LABEL directive allows you to explicitly define the label type. The value of the label defined in this way is equal to the address of command or data that immediately follows it. For example: LABEL L1 DWORD .

The expression NAME PROC defines the label, to which the jump is usually carried out by CALL . The block of code starting from such a label is called a procedure. The jump to such a label can also be carried out using the JMP command, and the CALL command can be used for jumping to an ordinary label. This, of course, serves as proof of the power and flexibility of the Assembly language.

The line that follows the label can contain the data reservation directive, for example, ERR DB ˜ Error or NUM DWORD 0 . With a high-level language, you determine a global variable when proceeding in such a way. With Assembly language, there is no difference between the command and the data. Therefore, there also is no difference between the label defining a command and the label defining data. Since I have mentioned data, let me list their main types:

  • BYTE ( DB )1 byte

  • WORD ( DW )2 bytes

  • DWORD ( DD )4 bytes

  • FWORD ( DF )6 bytes

  • QWORD ( DQ )8 bytes

  • TBYTE ( DT )10 bytes

In terms of high-level languages, the EQU directive is used for defining constants. For example: MES EQU ERROR!, LAB EQU 145H . Using the EQU directive, it is possible to assign a value to the label only once. To the right of the EQU directive, an expression using arithmetical, logical, and bit operations can be placed. They are as follows: +, ˆ , *, /, MOD (the remainder from the division), AND , OR , NOT , XOR , SHR , and SHL . Comparison operations also can be used, for example, EQ , GE , GT , LE , LT , and NE . The expression containing a comparison operation is considered Boolean. It takes the value of 0 if the logical condition is not satisfied; otherwise , it takes the value 1. Using the = directive, it is possible to assign integer values only; however, it is also possible to reassign values. Note that the expression can be a command operand: MOV EAX , 16* 16-1 . To assign purely string constants, use angle brackets, for example, err equ <Incorrect procedure call> .

Assignment using the = operator is less universal and can be used for assigning numeric values.

The $ label always defines the current address.

In MASM, the labels within a procedure are automatically considered local. Consequently, label names in procedures can be duplicated . In TASM, all labels by default are interpreted as global. To make labels within a procedure local, precede them with the @@ prefix and insert the LOCALS directive in the start of the program (see Chapter 11).

Often it is required that the data block in a program starts at the boundary that is a multiple of a specific number of bytes. This is achieved by the ALIGN directive. This keyword is followed by the required number of bytes. For example: ALIGN 4 , ALIGN 16 . The following is a fragment of a program containing a code segment, in which this directive is used:

 _DATA SEGMENT     PATH   DB "C:.TXT"     HANDLE DD ?     ALIGN  4     BUF    DB 1000 DUP (0)     ...     _DATA ENDS 

The ALIGN 4 directive used here ensures that the buffer designated by the BUF label will always start from a boundary that is a multiple of 4 bytes.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net