x86
Why we code for DOS. Real mode vs. Protected Mode
To learn assembly language we will program for the dos platform.
Dos can only run while the processor is in realmode. All PCs start in realmode. Windows only runs in protected mode. Windows starts in realmode, then switches the processor into protected mode.These modes dictate the operating evironment, the way the processor functions. Real mode emulates the 8086, while protected mode is the true Pentium architecture. Protected mode offers access to a lot more RAM but restricts access to areas of ram which do not belong to the code currently executing. How does the processor know which ram areas belong to who? The processor has a ram map called a descriptor table. This table must be setup by the OS loading the program or by the bootloader/initialization-code of an OS.
An essential, yet hidden, aspect of computing is Getting code to execute (and at the right time). That is the key to virus development. However, notice that protected mode's ram restrictitons disallow application code from modifing OS code.
What is great about DOS and the Realmode environment is the freedom executing code possesses. In realmode, one can easily modify the Interupt Vector Table to point to a custom interupt handler. All hardware which is serviced by the processor must have some code somewhere (in rom or ram) to do the needed work.
Setting up code to execute in protected mode is alot more involved. To do so, the Global Descrictor Table must be updated to grant the newly loaded code execution rights. Code that accesses the GDT must execute while the processor runs in Priveledge Level 0.
Realmode is easy and simple- we can execute any instruction at any time. Another thing we can access is any addressable device on the motherboard. The opcodes IN and OUT allow this. Of What devices do I speak? The keyboard controller is an addressable device. Its address is called a port.
Learn about Interupts, hardware and software. All devices attached to the motherboard can put voltage on their interupt line, causing the processor to run that device's handler code (cpu takes the interupt # in question and multipies it by 4, this number is now an offset into the interupt vector table, where the segment:offset address of handler code are stored). [by the way The cpu can cause a Software Interupt... it is a poular Instruction] Any device causing an INT will have a port address for communication.
So DOS is a great platform.
A platform? It's a starting point, a diving board. Dos is a program lauacher. DOS: a universal pc OS, or as Robinson put it, 'a glorified API'. Dos won't interfere with your program's architeture. It's API will aid with standard I\O ana file management.
Protected mode allows INT's IN's OUT's and all that other good stuff, but only if the os [ the os must set the application's code segment with the corect permissions ] allows it. It's all in the descritor table ( and there a few kinds of discriptors). INTs, OUTs, or INs could cause a TRAP to occur if the executing code segment them doesn't have permission to use those instructions. A TRAP is like an INT (aking to a JUMP), but is used to handle processor-intracacies. The cose a TRAP goes to could allow, or disallow, the previously mentioned INT, OUT, or IN to execute. Or that code could message to OS to termminate the program.
OS code, running at the highest proviledge level (and with favorable descritor settings) will have no interferrence doing anything at all. The reseason for this restriction is found in the purpose of protected mode itself. It is protected! In a multitasking environment, it is possible to have various threads accessing the same resources (like the Hard Drive) at once. This could cause corruption of I/O and data in RAM.
I propose making a Protected mode Dos. No multi-tasker. It's program's run in Priviledge level 0. It's programs are conceptually like system modules. Especially if it the OS has good terminate-and-stay-resident functionality. Old dos programs can run too, using Virtual-86 mode. Did I not meantion V-86 mode? It's what windows uses to run their DOS-BOX commandline (win98 at least). Maybe this new Dos will run Linux programs too, but whatever.