The code segment

advertisement
http://www.bandwidthco.com/whitepapers/programming/asm/Assembly%20Language%20Program%2
0Structure.pdf
BASIC ASSEMBLY LANGUAGE PROGRAM STRUCTURE
By
Mark E. Donaldson Revised January 1, 2009 Page 1 of 5 SEGMENT DEFINITION &
SEGMENT ORDER
1. An assembly language program must contain at least three segments:
Stack Segment containing the program’s stack.
Data Segment for variables used by the program.
Code Segment containing the program’s machine instructions.
2. These segments are defined using two directives:
SEGMENT directive which marks the start of the segment.
ENDS directive which marks the end of the segment.
3. Each segment must have a name, and the name must be used twice in the definition:
Once before the SEGMENT directive.
Again before the ENDS directive.
4. The stack segment must be defined with the STACK directive following the SEGMENT
directive. This definition tells DOS which segment address to load into the SS register.
5. The size of the stack segment is dictated by the definition of some data within the segment.
Enough stack space must be defined to cover any possible need the program may have, plus
the needs of DOS and any interrupt service routines (including loaded TSR’s) that may be
active while the program is running. If enough space is not defined, there may be a stack crash,
which will terminate the program and possibly crash the machine. Decide how much stack
space is realistically needed, and allocate twice as much. Use the DB and DUP directives to
allocate space.
6. The segments are order independent. They may be placed in any order without changing the
way the segments work together, nor the way the assembler treats them. However, it is
recommended the convention of defining the stack segment first, the data segment second, and
the code segment third be used.
DATA DEFINITIONS FOR VARIABLES & STACK SPACE
1. Variables and space for the stack must be allocated during assembly. The DB, DW, DD, and
DUP directives are the most common means to do this.
2. DW allocates word sized (16 bit) variables typically, to contain register sized values. DD
allocates double word sized (32 bit) variables, typically for full addresses containing both
segment and offset.
These definitions have a form as such:
Label DW OFFFH
3. The DB directive is designed to allocate byte sized (8 bit) quantities like characters and
register halves. It has the special property of being able to allocate strings as well. Elements of
the string may be numbers, characters, or quoted strings, separated by commas. The following
are
legal
DB
variable
definitions:
PROGRAM STRUCTURE
BASIC ASSEMBLY LANGUAGE
By Mark E. Donaldson Revised January 1, 2009 Page 2
of
5
Label DB 042H
Label DB 17
Label DB Insert some text
Label DB ODH,OAH
4. The DUP directive with a question mark (?) may be used to allocate a variable or buffer
without specifying any initial values. The ? Sets aside memory but stores nothing in it. The DUP
directive can also be used to store repeated patterns into larger variables and buffers. This can
make buffers and variables easy to spot when a hex dump from DEBUG is performed. The DUP
directive will take the following forms:
Label DB 10 DUP (02H,04H,06H,08H)
Label DB 64 DUP (STACK)
SETTING UP CODE SEGMENT
1. Like any segment, the code segment must have a name, and the name must be given at the
start and end of the segment definition, before the SEGMENT and ENDS directives. Although
the name is not important and probably won’t be reference anywhere in the code, it must be
there or an assembler error will be received.
2. An ASSUME directive must be included in the program. Its purpose is to tell the assembler
which of the defined segments is to be used for the code segment, and which segment is to be
used for the data segment. Unlike the stack segment, which has the directive STACK to tell the
assembler what sort of segment it is, nothing in the code or data segments specifies which sort
of segment they are. It isn’t enough that there are variables defined in the data segment or
machine instructions in the code segment.
3. An assembly language program should have its machine instructions grouped together in a
named procedure with the PROC directive. This is not strictly necessary unless the program is
broken down into procedures or modules. However, it is advised that the main program
portion of any assembly language program into a procedure called Main to help make the
program more readable.
4. What is essential is to provide a label that marks the place where program execution is to
begin. It is recommended that the label Start: be used as a convention, but the label can be any
legal identifier. Whatever the label, mark the main program’s starting point with the label and a
colon. Place the same label minus the colon after the END directive, which marks the end of the
source code file. Placing the start label after the END directive tells the assembler that there is
no more source code, and that the label is the point at which execution is to begin.
SEGMENT REGISTER ASSUMPTIONS
Where allowed, segment assumptions can be overridden with the segment override prefixes.
These are DS: SS: CS: ES:. For example:
mov ES:[BX],AX
The assumptions are these:
1. When the offset is specified in BX, SI, or DI, the assumed segment register is DS.
2. When the offset is specified in SP, the assumed segment register is SS. CANNOT BE
OVERRIDDEN.
3. When the offset is specified in BP, the assumed segment register is SS.
4. For string instruction LODS, the assumed segment is DS and the assumed offset is DI.
CANNOT
BE
OVERRIDDEN.
PROGRAM STRUCTURE
of 5
BASIC
ASSEMBLY
LANGUAGE
By Mark E. Donaldson Revised January 1, 2009 Page 3
5. For string instruction STOS and SCAS, the assumed segment is ES and the assumed offset
is DI. CANNOT BE OVERRIDDEN.
6. For string instruction MOVS, the source must be pointed to be DS:SI and the destination
must be pointed to by ES:DI. CANNOT BE OVERRIDDEN.
Rules For External Procedures Modules and Libraries.
RULE #1: Declare the code segments Public in all modules, and give them all the same
name.
RULE #2: Declare the data segments Public in all modules, and give them all the same
name.
RULE #3: Declare all exported procedures, entry points, and variables as Public. Place
the PUBLIC directive inside the segment where the exported items are declared.
RULE #4: Declare all imported procedures, entry points, and variables as external. Put
the external directive inside the segment where the imported items are to be used. Data
is used in the data segment, code in the code segment.
RULE #5: Make sure that there is a common ASSUME statement in the code segment of
every module associating the CS register with the shared code segment and the DS
register with the shared data segment.
RULE #6: Don’t forget to add the names of all external modules to the linker command
line in the link setup.
DOS DEBUG COMMON COMMANDS
-D Hex Dump
-E Enter New Data (Change Bytes In Memory)
-Q Quit
-R Register (Register Dump or -R Specific Register)
-A Assemble (Assemble Directly To Memory Incrementing CS:IP)
-T Trace (Execute Machine Instructions At CS:IP)
-W Write (Save Altered Memory Image Back To Disk)
-G GO (Begins Execution At The Address CS:IP)
DOS DEBUG FLAG STATE SYMBOLS
FLAG Set Symbol Clear Symbol
OF - Overflow Flag OV NV
DF - Direction flag DN UP
IE - Interrupt enable flag EI DI
SF - Sign Flag NG PL
ZF - Zero Flag ZR NZ
AF - Auxiliary carry flag AC NA
PF - Parity flag PE PO
CF - Carry flag CY NC
BASIC ASSEMBLY LANGUAGE PROGRAM
STRUCTURE
By Mark E. Donaldson Revised January 1, 2009 Page 4 of 5
<name>SEGMENT STACK<data definition><name>ENDS<name> SEGMENT<variables><name>
ENDS<name>
SEGMENT<name>
PROC
ASSUME
CS:<name>
DS:<name><start
label>:<instructions>
<name>
ENDP<name>
ENDSStackSegmentDataSegmentDataSegmentThe segments are independent and
may appear in any order. This order is a suggestion.The size of this data definition becomes the size of the
stack.END <start label>
BASIC ASSEMBLY LANGUAGE PROGRAM
STRUCTURE By Mark E. Donaldson Revised January 1, 2009 Page 5 of 5
TEXT EDITORSOURCE
FILE.ASM.OBJ.OBJ.OBJ.OBJLINKERMASM/TASMFINISHED
FILE.EXESTARTHERECHANGE SOURCEFILEBACK TO THE EDITORHAS
BUGSDEBUGGERPROGRAMCOMPLETEDNOERRORSTRY
PROGRAMLINKERERRORSASSEMBLERERRORSNO ERRORSPREVIOUSLY ASSEMBLED
MODULESWORKSGREATBACK TO THE EDITOR
http://mcs.uwsuper.edu/sb/324/Intro/struc16.html
CSCI 324: Assembly Language
Programming
General Structure of an Assembly Program
Generally, a program should consist of three segments:



stack segment
data segment
code segment.
The segment description must start with the directive SEGMENT and end up with the directive
ENDS. The segments can be defined in arbitrary order.
The stack segment is reserved for the system stack. The data segment usually contains the data (all
single variables, strings, arrays, constants. The code segment contains the main program and the
procedures used there.
The stack segment
A stack of size 100 should be sufficient for our purposes. The stack segment can be defined in your
program as follows:
Prog_Stack
Prog_Stack
SEGMENT STACK 'STACK'
DB 100 DUP(0)
ENDS
Here Prog_Stack is the name of your stack segment. The directive DB (Define Byte) reserves a byte
in the memory. This directive used in a combination with 100 DUP(0) (Duplication) directive reserves
100 bytes in the memory, which will be filled with 0 before running the program.
The data segment
The data segment can be organized as shown below:
Prog_Data
SEGMENT 'DATA'
Prompt
Str
VideoSeg
Prog_Data
DB
DB
EQU
ENDS
16,"Input string -->"
80 DUP(32)
0B800H
Here the variable Prompt is associated with a sequence of 16 bytes which form a string-like
structure. This variable contains the address of the first byte of this sequence.
The variable Str points to the first byte of 80 bytes reserved for this array, which are preliminary fills
up with the value 32 (the ASCII code for the blank character ' ').
The variable VideoSeg is a constant. The directive EQU assigns the value 0B800h with this constant.
Note that 0B800h is the beginning of the VRAM segment.
The code segment
The code segment consists of the procedures and has a similar structure as in the C language. This
segment can start with the main procedure and contain more procedures used in the main one. The
procedures must not overlap. The segment should start with the SEGMENT declaration following by
the ASSUME directive as shown below. This directive defines which of the segment registers (CS,
DS, and SS) mush be assigned with the corresponding segments of your program.
Any procedure should have a name and start with the PROC declaration, and end up with the ENDP
declaration.
Prog_Code
;
main
[label:]
main
;
Prog_Code
SEGMENT 'CODE'
ASSUME CS:Prog_Code, DS:Prog_Data, SS:Prog_Stack
PROC
MOV AX, Prog_Data
MOV DS, AX
; load the DS register
........
........
Instruction [operand],[operand] ; [comment]
........
........
ENDP
ENDS
The main procedure
The main procedure of your program (the one that will be called first) can start with the loading of
the DS register. Note that this register can be loaded from a register only. Furthermore, the main
procedure should provide a correct return to DOS after completing of your program. We use the
DOS interrupt INT 21h for this purpose. Thus, the main procedure can have the following structure.
main
PROC
MOV AX, Prog_Data
MOV DS, AX
; load the DS register
main
......
......
......
MOV AX, 4C00H
INT 21H
ENDP
; return to DOS
; return to DOS
The entire program must end up with a directive END.
Template for a real mode Assembly program
Summarizing, the general structure of an Assembly program is as follows:
Prog_Data
Prog_Data
;
;
Prog_Code
;
main
SEGMENT 'DATA'
......
......
ENDS
; beginning of the data segment
; end of the data segment
SEGMENT 'CODE'
; beginning of the code segment
ASSUME CS:Prog_Code, DS:Prog_Data, SS:Prog_Stack
PROC
MOV AX, Prog_Data
MOV DS, AX
; load the DS register
......
......
......
MOV AX, 4C00H
INT 21H
ENDP
; return to DOS
main
; end of the code segment
;
Subroutine1 PROC
; beginning of subroutine 1
......
......
Subroutine1 ENDP
; end of subroutine 1
;
Subroutine2 PROC
; beginning of subroutine 2
......
......
Subroutine2 ENDP
; end of subroutine 2
;
Prog_Code
ENDS
; end of the code segment
;
;
Prog_Stack SEGMENT STACK 'STACK' ; beginning of the stack segment
DB
100 DUP(0)
; organization of the stack area
Prog_Stack ENDS
; end of the stack segment
;
;
END main
; end of the whole code
In order to make an .exe file apply MASM to your source assembly file. This results in an .obj file.
Then apply LINK to this file. This produces an executable file with extension .exe.
Alternatively, you can use make16.bat file.
<== Back to Course
Download