Chapter 2

INSTRUCTION ADDRESSING

AND EXECUTION

 

Introduction: This chapter describes the PC software environment: the functions of DOS and its main components. We will examine the following:

1. The Boot process

2. How the system loads a program

3. System use of the stack

4. How instruction in the code segment addresses data in the data segment
 

Operation System Characteristics
 

DOS is an operating system that provides general, device-independent access to the resources of a computer. The devices supported include the keyboard, screens, and disk drives. Device independence means that you don't have to address devices specifically, since DOS and its device drivers can handle the operations at the device level.
 

DOS Functions:

1. File management. DOS maintains the directories and files on system disks. DOS bears the responsibility of managing their location on disk.
 

2. Input/Output. DOS maintains I/O interrupts generated by programs.
 

3. Program loading. DOS handles accessing a program from disk, loading it in memory, and initializing it form execution.
 

4. Memory management. DOS maintains the allocation and deallocation of memory used by programs.
 

5. Interrupt Handling. DOS allows users to install resident programs that attach themselves to the interrupts system to perform special functions.
 

Organization of DOS
 

The three major components of DOS are IO.SYS, MSDOS.SYS, and COMMAND.COM.
 

IO.SYS performs initialization functions at boot up time and also contains important input/output functions and device drivers that supplement the primitive I/O supports in ROM BIOS. This component is stored on disk as a hidden system file and is known under PCDOS as IBMBIO.COM.
 

MSDOS.SYS acts as the DOS kernel and is concerned with file management, memory management, and input/output. IMBDOS.COM
 

COMMAND.COM is a command processor or shell that acts as the interface between the user and the operating system. It displays the DOS prompt, monitors the keyboard, and processes user commands such as deleting a file or loading a program for execution.
 
 

THE BOOT PROCESS
 
 

DOS-BIOS Interface

BIOS contains a set of routines in ROM to provide device service support. BIOS tests and initializes attached devices and provides services that are used for reading to and for writing from devices.

One task of DOS is to interface with BIOS when it need to access its facilities.

A program can access BIOS directly or it can access the device directly.

System Program Loader

DOS supports two types of executable programs: .COM and .EXE.

A .COM program consists of one segment that contains code, data, and the stack.

You should write a .COM program if you need a utility or a resident program.

An .EXE program consists of separate code, data, and stack segments and is the method used for more serious programs.

When DOS loads an .EXE program from a disk into memory, the following tasks are performed:

1. Access the .EXE program from disk

2. Constructs a 256-byte (100H) program segment prefix (PSP) on a paragraph boundary in available internal memory.

3. Stores the program in memory immediately following the PSP.

4. Loads the address of the PSP in the DS and ES registers.

5. Loads the address of the code segment in the CS and sets the IP to the offset of the first instruction (usually zero) in the code segment.

6. Loads the address of the stack in the SS and set the SP to the size of the stack.

7. Transfers control to the program for execution, beginning (usually with the first instruction in the code segment.
 

The Stack
 

The stack provides a space for the temporary storage of addresses and data items.

DOS automatically defines the stack for a .COM program. For an .EXE program the programmer must explicitly define a stack.
 

Each data item in the stack in one word (two bytes). The SS register as initialized by DOS contains the address of the beginning of the stack. Initially the SP contains the size of the stack, a value that points to the byte past the end of the stack. The stack begins at the highest location and stores data downward through memory.
 

The PUSH decrements SP by 2 and stores a value there.

The POP instructions returns a value from the stack increments SP by 2 to the next higher storage word.
 

The statement PUSH AX decrements the SP by 2 and stores the contents of AX on the stack.

The statement POP AX stores the contents on the stack pointed to by SP into the AX register and increments the SP by 2.
 

Note: You should always ensure that your program coordinates pushing values onto the stack with popping them off of it. Note when designing a program that will be an .EXE, you have to define a stack that is large enough to contain all values that could be pushed onto it.
 

Other related instructions are PUSHF and POPF which save and restore the status of the flags and PUSHA and POPA (for 80286 and later) which save and restore the contents of all the general-purpose registers.
 

Program Addressing

Each instruction of your program is fetched during its execution by using the CS:IP pair.

The CS register points to the code segment and the IP is the offset into the segment.
 

Similarly data stored in the data segment or stack segment is referenced by the DS:SI or SS:SP

SS:BP or DS:BX.

 

Instruction Operands

An instruction may have no, one, two, or three operands.

No operands:	RET
One operand:	MUL 10
Two operands:	MOV CX,10
Three operands:	SHRD ECX,EBX,CL

Examples of types of operands:
WORDX 	DW 	0		;DEFINE WORDX AS WORD

	MOV	CX,WORDX	;MOVE CONTENTS OF WORDX TO CX

	MOV	CX,25		;MOVE VALUE 25 TO CX

	MOV	CX,BX		;MOVE CONTENTS OF BX TO CX

	MOV	CX,[BX]		;MOVE CONTENTS OF LOCATION ADRESSED BY BX

 

 

 

PROTECTED MODE

Under Window's protected mode, the processor can switch from one task to another.

In real mode, the segment registers contain the actual address of segments. The addressable memory is restricted to one megabyte.

Protected mode requires greater addressing capability. It uses various tables such as:

The actual address of segments are stored in the Descriptor tables; the segment registers contain pointers to the current Local Descriptor table. The table provides 32-bits for addressing up to 4 (2^32 =4294967296) gigabytes of memory.