четверг, 20 сентября 2012 г.

Directive segmentation

Recall that the microprocessor has six segment registers, through which can run simultaneously:

• With one code segment;

• with one segment of the stack;

• with one segment of the data;

• three additional data segments.

Again, that is a physical segment of the memory occupied by the teams and (or) data, the addresses of which are calculated relative to the value in the corresponding segment register.

It is important to note that the functional purpose of the segment is wider than simply splitting the program into blocks of code, data, and stack. Segmentation is part of a more general mechanism associated with the concept of modular programming. It involves the unification of design object modules produced by the compiler, including those with different programming languages. This feature allows programs written in different languages.

The segments are described in assembler directive SEGMENT.

Syntactic description of the segment is the following construction

<Segment name> SEGMENT [the alignment] [type of combination]

[Class segment] [type segment size]

<Body segment>

<Segment name> ENDS

There are many possible parameter values.

And with the directive ASSUME you can tell the compiler which segment to which segment register-element attached.

Format guidelines:

ASSUME <segment register> <segment name>

SEGMENT directive and ASSUME - standard guidelines segmentation.

A description of these directives is rather complicated and requires serious knowledge assembly and architecture, so

simple programs containing one segment for code, data, and stack commonly used simplified descriptions of each business.

Translators MASM and TASM are provided for use of simplified segmentation directives (instead SEGMENT).

! Standard and simplified segmentation directives do not exclude each other.

! Standard guidelines are used when the programmer wants to take complete control over the placement of segments in memory, and their combination with the segments of the other modules.

! Simplified guidelines should be used

1) for simple programs

2) programs designed to bind to the software modules written in high level languages ​​(this allows the linker to effectively bind modules in different languages ​​through the standardization of communication and control).

Macros

This is a description of the macro. Macro syntax:

<Name of the macro> MACRO [formal parameters]

macro body

ENDM

Directive MACRO - is the title of the macro. It identifies the name and separated by commas formal parameters, if necessary.

Formal parameters allow you to copy the macro is not intact, and with the changes. Those values ​​which need to be changed are described formal parameters.

Remark. The names of the formal parameters are local to the macro, ie they can match the names in the main program, makrogenerator understands them as parameters.

Ends the macro directive ENDM. ! No need to repeat the name of the macro.

Example 1, setting the data segment

initds macro

mov ax, @ data

mov ds, ax

endm

Macros can be placed:

1. At any point in the program.

! Certainly before the first reference to it.

2. In a separate file.

To make the macros available in the program, it is necessary at the beginning of the program to use the directive INCLUDE <filename>. In this stage of makrogeneratora text of the file is fully inserted in place of the directive.

Example. Masm

model small

include Mymacro.inc

. . .

You can write macros universal in one file, in the so-called makrobiblioteku. Connect it with the use of include.

That the text of the program does not include the extra macro, you can use the directive

PURGE <comma-delimited list of names of macros>

The directive specifies what macros should not be included in the text of the program.

Example. . . .

include mymacro.inc

purge outstr, initds

. . .

The structure of the program in assembler MASM

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.