Learn how programs run, modes of operation, and basic execution environments on the x86 processor.

This article is a continuation of a series on x86 architecture

Loading and Execution Processes

Below is a list of steps, described in order of operation, of when a user runs a program through the command prompt:

  1. The operating system, or OS, explores the computer for the given program's filename in the disk directory. If the OS cannot find the name in this directory, it will then explore a list of directories, which are predetermined (known as paths) for the program's filename
  2. If the OS finds the program file, it will retrieve any basic information about the file from the directory; this basic information includes the size of the program's file as well as its physical exact location on the disk drive.
  3. The OS will determine the next available location in the memory and will load the program file into the memory. From here, it will allocate a block of the memory for the file and registers the file's information into a table. Also, the OS can adjust values of pointers within the program, only if addresses of the program's data are needed to be known.
  4. Next, the OS will begin executing the program's very first instruction. Once the program begins to run, it is now called a process. The OS allocates this process an generated number, or process ID, to keep track of its progress whilst the process is being executed.
  5. This process will run by itself. However, it is the OS's responsibility to monitor how the process is executed as well as responding to system resource requests. Some common system resources are memory, disk files, as well as I/O devices. 
  6. When the program's process is finished executing, it is removed from its allocated space in the memory.

Multitasking Processes in x86

A system is known to be a multitasking OS if it is able to run multiple tasks at the same time. These tasks can be defined as either a program or thread of execution. A process is given its own memory area and can contain a various amount of execution threads. These individual threads can share its memory with other threads, but only if they are contained within that same process. Looking at a gaming program, individual threads are often used to control multiple graphic objects simultaneously. Another example is a web browser, where separate threads are used to load graphic images and respond to a user's input simultaneously. 

Nowadays, most modern operating systems that communicate with hardware, perform background processes, and display user interfaces will execute those tasks simultaneously. A central processing unit (CPU) really can only execute one machine instruction at a time. This means that a component of the OS given the name of the scheduler will allocate a sliver of CPU time, or time slice, to each separate task. During this time slice, the CPU will execute a set of machine instructions, stopping after each time slice has finished.

When tasks are switched rapidly, the CPU creates the illusion that they are running simultaneously. A type of scheduling used by the OS we'll discuss is known as round-robin scheduling. Figure 1.1 illustrates a scheduler running nine active tasks. Say the scheduler authorized 150 milliseconds to each task and allowed for 8 milliseconds for switching between tasks; one full circuit of the complete task list would need 1422 milliseconds (9 * 150) + (9 * 8) to be fully complete. 


Figure 1.1 Round-Robin Scheduler


Processors (such as the x86 processor) that support switching of tasks allow a multitasking OS to be run on it. This processor will, in turn, save the state of each task prior to switching to a new task. This state can consist of the various contents of the processor registers, status flags, as well as the references to each task's memory segments. This OS will more than likely assign varying priorities to a task; this allows for a smaller or larger time slice for the task. An OS that permits a higher priority task to interrupt a lower priority one is known as a preemptive multitasking OS, and allows for better system stability. A couple operating systems that use this are Windows XP and Linux. 


Different Modes of Operation

Some basic architectural features that the x86 processor includes various modes of operation. These processors have three modes of operation that are primarily used: protected mode, real-address mode, as well as a system management mode. Addition to these three, there exists a sub-mode, virtual-8086, which is a variant of the protected mode. Below is a list of each mode: 

  • Protected Mode: this mode is the inherent state of which the processor is in, where all machine instructions are available. In this mode, each program are given an allocated memory area which are named segments, and the processor will not allow programs from referencing memory that is outside of their segment.
  • Real-Address Mode: This mode allows for the program environment of the processor to switch into other modes. This mode may be used to run MS-DOS programs (Microsoft Disk Operating System) which require direct access to the system's memory and hardware. If a program is run in this mode, there is a chance for the operating system to crash, or stop responding to instructions.
  • System Management Mode: Shortened to SSM, this mode provides the OS with a method for implementing various functions such as power management or security of the system. These are usually implemented by manufacturers who tweak the processor for a certain system setup.
  • Virtual-8086 Mode: While in this mode, under the protected mode, the CPU is able to execute real-address programs such as MS-DOS programs in a safe environment. I.e. if the program crashes or tries to write data into the system memory area, it will not affect other running programs. 


Coming Up

As of now, you should have a good understanding of how a program runs in an x86 processor, as well as how a processor can multitask. The modes of operation should be clear as to why each mode is chosen for a program to run in. Topics to be covered next will be address space in a basic run-time environment, execution registers, and x86 memory management. Thank you for reading and if you have any questions or comments, please leave them below!



1 Comment