A program written in assembler MASM, may consist of several parts, called modules, each of which can be determined by one or more data segments, stack, and code. Any complete program in assembly shall include one main, or primary (main), the module from which to carry it out-set. The main module can contain program segments, data segments and power-ka declared with simplified guidelines. In addition, before the announcement of segments to specify a memory model with the directive. MODEL. Since the vast majority of today's applications are 32-bit, the focus in this section we will pay it to such programs, although not go around attention-tion and 16-bit programs, which are still in use. Let's start with the 16-bit programs.
The following example shows a 16-bit program in assembler, which uses simplified assembler directives MASM:
. Model small, c; This directive specifies prior to the announcement
, Segments
. Stack 100h; stack size of 256 bytes
. Data; beginning of the data segment
. . .
, Data
. . .
. Code; program segment begins here
main:
. . .
, Assembly instructions
. . .
end main
end
Here the operator end main points to the main entry point to the main procedure. The operator closes the last segment of the end, and marks the end of the source program. In 16-bit MS-DOS applications, you can initialize the segment registers so that they point to the desired logical data segment. Code Listing 4.1 demonstrates this.
Listing 4.1. Examples of addressing segments in the program MS-DOS.model large
. Data
s1 DB "TEST STRING $"
. Code
mov AX, @ data
mov DS, AX
lea DX, s1
mov AH, 9h
int 21h
mov ax, 4c00h
int 21h
end
Here, the display screen will contain the string s1. With the following commands in the segment register DS-element fits the data segment address specified by the directive. Data: mov AX, @ data
mov DS, AX
Then the string s1, addressable through registers DS: DX, is displayed on the screen with the use vaniem interrupt function 9h 21h MS-DOS. Try commenting out the two lines of code analyzed and look at the output of the program.
For a 32-bit application template source code is different:. Model flat
. Stack
. Data
; Aaiiua
. Code
main:
. . .
, Assembly instructions
. . .
end main
end
The main difference from the previous example - another memory model (flat), expect it to be a 32-bit linear addressing the attribute near.
As you can see, the "classic" pattern 32-bit application contains a data area (as defined by the directive. Data), the stack region (Directive. Stack) and the domain code (Directive. Code). It may be that the 32-bit applica-tions in assembly will require several separate segments of data and / or code. In this case, the developer can create them with the directive SEGMENT. Directive wa SEGMENT defines logical segments and can be described as follows: The name of the attribute list SEGMENT
. . .
name ENDS
I note that the SEGMENT directive can be used with any model of memory, not just flat. When Using SEGMENT need to tell the compiler that all segment registers are set according to the memory model flat. This can be done with the directive ASSUME: ASSUME CS: FLAT, DS: FLAT, SS: FLAT, ES: FLAT, FS: ERROR, GS: ERROR
Registers FS and GS programs are not used, so they indicate at-ribut ERROR.
Now we will review the code 32-bit assembly language procedure (called _seg_ex), which uses two logical data segment. Of the procedure for copying the string src, located in the data segment data1, in the region of memory in the data segment dst data2 and contains a single logical program segment-tion code (code segment).
Soothe readers unfamiliar with the principles of the procedure (which is covered later in the book): in this case we are interested in the code within the procedure _seg_ex (commands found between directives _seg_ex proc and _seg_ex endp). The source code _seg_ex procedure is shown in Listing 4.2.
Listing 4.2. The use of two logical segments of data in 32-bit pro-tsedure.586
. Model flat
option casemap: none
data1 segment
src DB "Test STRING To Copy"
len EQU $-src
data1 ends
data2 segment public
dst DB len +1 DUP ('+')
data2 ends
code segment
_seg_ex proc
assume CS: FLAT, DS: FLAT, SS: FLAT, ES: FLAT, FS: ERROR, GS: ERROR
mov ESI, offset data1
mov EDI, offset data2
cld
mov CX, len
rep movsb
mov EAX, offset data2
ret
_seg_ex endp
code ends
end
If using a flat data access is via a 32-bit offset, so the meaning of the commands listed below, loads the address of logical segments (as well as the address lines of src and dst) to the registers ESI and EDI, I think, is clear: mov ESI, offset data1
mov EDI, offset data2
The group performs the following commands to copy the string src dst, while re-giste CX contains the number of bytes copied: cld
mov CX, len
rep movsb
The register EAX returns the row address receiver dst. Pay attention chi-Tutelo that any initialization segment registers do not need to, it is done with the directive. Model flat. Another important point is the program that uses the model flat, running on the same physical segment, although logical segments can be several, as in our case.
The efficiency of the procedure is easy to check by calling it from the program for Visual C + +. NET (only need to include the object file to the project application procedure). Application source code is shown in Listing 4.3.
Listing 4.3. Program that calls the procedure seg_ex # include <stdio.h>
extern "C" char * seg_ex (void);
int main (void)
{
printf ("EXTERNAL MODULE EXAMPLE:% s \ n", seg_ex ());
return 0;
}
Here the procedure is external seg_ex so declared as extern.
The result of the program will be a line on the screen displeyaEXTERNAL MODULE EXAMPLE: Test STRING To Copy +
Macro - tools (means) the modification of code during its broadcast.
Macro designed to facilitate writing programs in assembly language and to better understand the source code.
The main idea. Loop which describes the program in a special way (macro) is called, and then in the right places of the program is used to refer to it. When you create the object code instead of the link itself is inserted fragment, ie substitution occurs fragment instead of the link.
Broadcaster assembly consists of two parts: makrogeneratora directly translator. Broadcaster with makroasseblerom call. It was such a macro assembler, we use the system MASM (macroassembler language).
Treatment programs with Macro is a translator in two stages. Stage one has makrogenerator that produces a replacement for all macros, and the second phase of the program has transformed text is translated into object code.
The following example shows a 16-bit program in assembler, which uses simplified assembler directives MASM:
. Model small, c; This directive specifies prior to the announcement
, Segments
. Stack 100h; stack size of 256 bytes
. Data; beginning of the data segment
. . .
, Data
. . .
. Code; program segment begins here
main:
. . .
, Assembly instructions
. . .
end main
end
Here the operator end main points to the main entry point to the main procedure. The operator closes the last segment of the end, and marks the end of the source program. In 16-bit MS-DOS applications, you can initialize the segment registers so that they point to the desired logical data segment. Code Listing 4.1 demonstrates this.
Listing 4.1. Examples of addressing segments in the program MS-DOS.model large
. Data
s1 DB "TEST STRING $"
. Code
mov AX, @ data
mov DS, AX
lea DX, s1
mov AH, 9h
int 21h
mov ax, 4c00h
int 21h
end
Here, the display screen will contain the string s1. With the following commands in the segment register DS-element fits the data segment address specified by the directive. Data: mov AX, @ data
mov DS, AX
Then the string s1, addressable through registers DS: DX, is displayed on the screen with the use vaniem interrupt function 9h 21h MS-DOS. Try commenting out the two lines of code analyzed and look at the output of the program.
For a 32-bit application template source code is different:. Model flat
. Stack
. Data
; Aaiiua
. Code
main:
. . .
, Assembly instructions
. . .
end main
end
The main difference from the previous example - another memory model (flat), expect it to be a 32-bit linear addressing the attribute near.
As you can see, the "classic" pattern 32-bit application contains a data area (as defined by the directive. Data), the stack region (Directive. Stack) and the domain code (Directive. Code). It may be that the 32-bit applica-tions in assembly will require several separate segments of data and / or code. In this case, the developer can create them with the directive SEGMENT. Directive wa SEGMENT defines logical segments and can be described as follows: The name of the attribute list SEGMENT
. . .
name ENDS
I note that the SEGMENT directive can be used with any model of memory, not just flat. When Using SEGMENT need to tell the compiler that all segment registers are set according to the memory model flat. This can be done with the directive ASSUME: ASSUME CS: FLAT, DS: FLAT, SS: FLAT, ES: FLAT, FS: ERROR, GS: ERROR
Registers FS and GS programs are not used, so they indicate at-ribut ERROR.
Now we will review the code 32-bit assembly language procedure (called _seg_ex), which uses two logical data segment. Of the procedure for copying the string src, located in the data segment data1, in the region of memory in the data segment dst data2 and contains a single logical program segment-tion code (code segment).
Soothe readers unfamiliar with the principles of the procedure (which is covered later in the book): in this case we are interested in the code within the procedure _seg_ex (commands found between directives _seg_ex proc and _seg_ex endp). The source code _seg_ex procedure is shown in Listing 4.2.
Listing 4.2. The use of two logical segments of data in 32-bit pro-tsedure.586
. Model flat
option casemap: none
data1 segment
src DB "Test STRING To Copy"
len EQU $-src
data1 ends
data2 segment public
dst DB len +1 DUP ('+')
data2 ends
code segment
_seg_ex proc
assume CS: FLAT, DS: FLAT, SS: FLAT, ES: FLAT, FS: ERROR, GS: ERROR
mov ESI, offset data1
mov EDI, offset data2
cld
mov CX, len
rep movsb
mov EAX, offset data2
ret
_seg_ex endp
code ends
end
If using a flat data access is via a 32-bit offset, so the meaning of the commands listed below, loads the address of logical segments (as well as the address lines of src and dst) to the registers ESI and EDI, I think, is clear: mov ESI, offset data1
mov EDI, offset data2
The group performs the following commands to copy the string src dst, while re-giste CX contains the number of bytes copied: cld
mov CX, len
rep movsb
The register EAX returns the row address receiver dst. Pay attention chi-Tutelo that any initialization segment registers do not need to, it is done with the directive. Model flat. Another important point is the program that uses the model flat, running on the same physical segment, although logical segments can be several, as in our case.
The efficiency of the procedure is easy to check by calling it from the program for Visual C + +. NET (only need to include the object file to the project application procedure). Application source code is shown in Listing 4.3.
Listing 4.3. Program that calls the procedure seg_ex # include <stdio.h>
extern "C" char * seg_ex (void);
int main (void)
{
printf ("EXTERNAL MODULE EXAMPLE:% s \ n", seg_ex ());
return 0;
}
Here the procedure is external seg_ex so declared as extern.
The result of the program will be a line on the screen displeyaEXTERNAL MODULE EXAMPLE: Test STRING To Copy +
Macro - tools (means) the modification of code during its broadcast.
Macro designed to facilitate writing programs in assembly language and to better understand the source code.
The main idea. Loop which describes the program in a special way (macro) is called, and then in the right places of the program is used to refer to it. When you create the object code instead of the link itself is inserted fragment, ie substitution occurs fragment instead of the link.
Broadcaster assembly consists of two parts: makrogeneratora directly translator. Broadcaster with makroasseblerom call. It was such a macro assembler, we use the system MASM (macroassembler language).
Treatment programs with Macro is a translator in two stages. Stage one has makrogenerator that produces a replacement for all macros, and the second phase of the program has transformed text is translated into object code.
Комментариев нет:
Отправить комментарий