CIS-77 Home http://www.c-jump.com/CIS77/CIS77syllabus.htm
MASM programs consist of file modules made up of segments.
Logical segments contain the three components of a program: code, data, and stack.
MASM organizes all three logical segments for you, so they occupy physical segments of memory at runtime.
Segment registers CS, DS, and SS contain either physical memory segments (real mode), or logical segments (protected mode).
Simplified segment directives hide many of the details of segment definition and assume the same conventions as are used by Microsoft high-level languages.
The simplified segment directives are .MODEL, .CODE, .CONST, .DATA, .DATA?, .FARDATA, .FARDATA?, and .STACK.
Every program written only in MASM has one main module, where program execution begins.
Main module can contain code, data, or stack segments defined with all of the simplified segment directives.
Any additional modules should contain only code and data segments.
Every module that uses simplified segments must begin with the .MODEL directive.
Example of a minimal main module using simplified segment directive structure is this:
; your_program_name.asm ; Brief description of what program does .586P ; Enable assembly of all Pentium instructions, ; including privileged. .MODEL FLAT, STDCALL ; Change calling convention, if necessary. ; MODEL directive is required before ; other simplified segment directives. .STACK ; Use default 1-kilobyte stack .DATA ; Begin data segment ; Put your data declarations here .CODE ; Begin code segment _start: ; Start of main program code ; Put your main application code here ret ; Exit program PUBLIC _start ; Make entry point public ; Put your application procedures here END
The .DATA and .CODE statements do not require any separate statements to define the end of a segment.
.DATA and .CODE close the preceding segment and then open a new segment.
The .STACK sizeoptional directive opens and closes the stack segment (with segment name STACK). Directive does not close the current segment. Default stack size is 1024.
The END statement closes the last segment and marks the end of the source code. It must be at the end of every module.
The .STARTUP and .EXIT directives are available only in 16-bit addressing modes:
. .CODE ; Begin code segment .STARTUP ; Generate start-up code (not for 32-bit segments) ; Put your main application code here .EXIT ; Generate termination code (not for 32-bit segments) ; Put your application procedures here END
The .MODEL directive defines the attributes that affect the entire module:
memory model
default calling and naming conventions
operating system
stack type.
.MODEL directive
enables use of simplified segments
controls the name of the code segment
controls the default distance for procedures.
.MODEL must appear in source file before any other simplified segment directive. The syntax is:
.MODEL memorymodel, options-optional
The memorymodel can be TINY, SMALL, COMPACT, MEDIUM, LARGE, HUGE, or FLAT, it determines size of code and data pointers. This field is required.
For example,
.MODEL small ; Small memory model .MODEL large, c, farstack ; Large memory model, ; C conventions, ; separate stack .MODEL medium, pascal ; Medium memory model, ; Pascal conventions, ; near stack (default)
When writing assembler modules for a high-level language, you should use the same memory model as the calling language.
Choose the smallest memory model available that can contain your data and code, since near references operate more efficiently than far references.
The traditional memory models recognized by many languages are small, medium, compact, large, and huge.
Small model supports one data segment and one code segment.
All data and code are near by default.
Large model supports multiple code and multiple data segments.
All data and code are far by default.
Medium and compact models are in-between.
Medium model supports multiple code and single data segments
Compact model supports multiple data segments and a single code segment.
Huge model implies that individual data items are larger than a single segment, but the implementation of huge data items must be coded by the programmer.
Tiny-model programs run only under MS-DOS. Tiny model places all data and code in a single segment. Therefore, the total program file size can occupy no more than 64K.
Since the assembler provides no direct support for this feature, huge model is essentially the same as large model.
In each of the models, you can override the default. For example, you can make large data items far in small model, or internal procedures near in large model.
The flat memory model is a non-segmented configuration available in 32-bit operating systems.
Flat model is similar to tiny model in that all code and data go in a single 32-bit -addressable block of memory.
To write a flat model program, specify the .386 or .486 directive before .MODEL FLAT:
.386 .MODEL FLAT ; All data and code, including system resources, are in a single 32-bit segment.
The operating system automatically initializes segment registers at load time.
A program may need to modify segment registers only when mixing 16-bit and 32-bit segments in a single application.
CS, DS, ES, and SS all occupy the supergroup named FLAT.
Addresses and pointers passed to system services are always 32-bit near32 addresses and pointers.
Flat model does not require far addresses. By default, all addressing is relative to the initial values of the segment registers.
Therefore, far addressing does not apply to flat model programs.
The stack is the section of memory used for pushing or popping registers and storing the return address when a subroutine is called.
The stack often holds temporary and local variables.
If your main module is written in a high-level language, that language handles the details of creating a stack.
Use the .STACK directive only when writing a main module in assembly language.
The .STACK directive creates a stack segment.
By default, the assembler allocates 1K of memory for the stack. This size is sufficient for most small programs.
To create a stack of a size other than the default size, give .STACK a single numeric argument indicating stack size in bytes:
.STACK 2048 ; Use 2K stack
The .DATA directive creates a near data segment.
This segment contains the frequently used data for your program.
Data segment can occupy
up to 64K in MS-DOS
or up to 512 megabytes under flat model in Windows NT.
.DATA? and .CONST enhance compatibility with Microsoft high-level languages. In Microsoft languages, .CONST is used to define constant data, such as strings and floating-point numbers that must be stored in memory.
The .DATA? segment is used for storing uninitialized variables. You can follow this convention if you want. If you use C startup code, .DATA? is initialized to 0.
The .CODE directive in your program instructs the assembler to start a code segment.
The next segment directive closes the previous segment; the END directive at the end of your program closes remaining segments.
With near code, the assembler names every code segment _TEXT, causing the linker to concatenate these segments into one. You can override the default name by providing an argument after .CODE.