Page 1 of 2
These days we are surrounded by different software frameworks. Java, and .NET to name but two, and, there are many more. Have you ever wondered how they work or have you ever wanted or needed to implement one?
This article explores a simple, even trivial, runtime framework.
Note: The source code given in this article is for example purposes only. I know that this framework is far from being perfect, therefore, this article is not a howto or tutorial - just an explanation of principle. Error checks are omitted on purpose. You want to implement a real framework - do it yourself, including error checks.
Now, to let's get to business.
Wikipedia gives the following identification for the term "Software Framework":
A software framework is a universal, reusable software platform used to develop applications, products and solutions. Software Frameworks include support programs, compilers, code libraries, an application programming interface (API) and tool sets that bring together all the different components to enable development of a project or solution.
As you can see, software framework is quite a complex thing. However, let's simplify it and see how it basically work.
Figure 1 may give you a good understanding of what a Software Framework is and what role it performs. Put simply it is a shim between the user application and the Operating System.
There are at least two types of Software Frameworks:
- Application Programming Interface (API) - if we take a look at Windows API, we may see that it is a framework as well. However, it may be bypassed or, at least, a programmer may choose to decrease the interaction with it by, for example, using functions from ntdll.dll instead of those provided by kernel32.dll or even "talk" to Windows kernel directly (highly not recommended, but may be unavoidable some times) through interrupts.
- .NET like framework - total isolation of user code from the operating system. Such frameworks are mostly virtual machines totally isolating user application from the operating system and hardware. However, such framework has to provide the application with all the services available in the Operating System. This is type of framework we are going to build in this article.
The basics of building a simple virtual machine is covered in this article, so I will only give a brief explanation here.
Our VM in this example will consist of the following components:
- Virtual CPU
A structure that represents a CPU - basically, has 6 registers and a pointer to the stack:
unsigned int regs;
unsigned int* stack;
The 6 registers are general purpose A, B, C and D, where A is also used to store system call return value and C is used as a counter for LOOP instruction, STACK POINTER (SP) and INSTRUCTION POINTER (IP).
- Instruction Interpreter
A function or a set of functions which responsible for interpretation of the pseudo assembly (or call it intermediate assembly language) designed for this virtual machine (in this case 14 instructions).
- System Call Handler
This component provides the means for the user application to interact with the Operating System (in this case 2 system calls: sys_write and sys_exit).
The name of the function speaks for itself. This is the first function of the framework implementation which gains control. In this particular case, it does not have too many things to do - initialization of the virtual CPU and execution of the command interpreter, until the user application exits (signals the framework to terminate the execution).
It is a common practice to implement a framework as a DLL (dynamic link library), for example, mscoree.dll - the core of the .Net framework. I do not see any reason to reinvent the wheel, therefore, this framework will be implemented as a DLL as well.
All is fine, you may say, but how should we pass the compiled pseudo assembly code to the framework?
Well, I bet, most of you know how to do that. In case you don't - no worries, just keep reading.