17.6 Register and Memory Initialization

When you execute an OS/2 program, OS/2 stores information about the program directly in registers. With DOS programs, the information is kept in a separate program segment prefix (PSP). The registers hold these values when an OS/2 program begins:

Register Contents at Program Start

AX Segment address of program's environment
BX Offset of command-line arguments within the environment
CX Length of near data area (DGROUP)
SP Offset of the top of the stack within the stack segment
CS:IP Program's entry point
DS Segment address of near data area (DGROUP)
SS Segment address of stack

Note that OS/2 automatically initializes SS:SP correctly. If the .MODEL directive specifies FARSTACK, SS is initialized to its own segment address. If the model is NEARSTACK, OS/2 sets SS to DGROUP and SP to the top of the stack within DGROUP.

Summary: You may want to save the AX, BX, and CX registers at startup.

Upon start-up, AX, BX, and CX all contain information highly useful to some programs. If you want to access the program's command-line arguments or know the size of DGROUP, you must save the contents of these registers immediately:

FPBYTE TYPEDEF FAR PTR BYTE

.DATA

args FPBYTE 0

cmds FPBYTE 0

.CODE

mov WORD PTR args[0], ax ; Save segment of args

mov WORD PTR args[2], 0 ; Offset is 0

mov WORD PTR cmds[0], ax ; Save segment of cmds

mov WORD PTR cmds[2], bx ; Save offset of cmds

The AX register points to the segment value of the start of the program's environment. AX:BX points to the starting address of arguments within the environment, the first of which is the program name. This name is followed by a null (zero) byte and the command-line arguments exactly as typed at the command prompt. A second null marks the end of the arguments.

Summary: If you use simplified segments, .DATA is equivalent to DGROUP.

Under OS/2, the data segment register, DS, contains the segment of the near data area, DGROUP. If you use simplified segment directives, this is the .DATA segment. You must place one data segment in a group called DGROUP if you do not use the simplified directives:

_DATA SEGMENT WORD PUBLIC 'DATA'

.

.

.

_DATA ENDS

DGROUP GROUP _DATA

ASSUME DS:DGROUP

Calling the group anything other than DGROUP, or not having a DGROUP, causes an error. Only the memory required by the program is allocated by OS/2. This means that the system has space in reserve for later memory requests and for other programs.