Upcoming Posts

Upcoming Posts....

Make your own partition magic software.
How to make an assembler.
What is booting?
Write a simple OS!

‎"I have no special talents. I am only passionately curious." - Albert Einstein

Tuesday, February 28, 2012

Virtual Machine Implementation

Can application software run an operating system? Let me put it in other way, how can ‘VMware’ application executes/loads operating system like Linux? Curious to know, be patient and read this article:

Let me start with the computer fundamental to get you in the context of virtual machine:

Hardware – Computer consists of hardware's like processor, RAM, Hard disk, monitor, keyboard and motherboard which contains chips like BIOS, System timer chip 8054, Interrupt controller 8259 etc.

Software – Operating system (OS) and other software’s like compiler, MS office etc are nothing but a binary file or a set of binary files which contain machine level instructions or opcode (Operation code).

When OS gets loaded into the memory, processor reads instructions and performs operations accordingly. Anything (any machine/person/software) which can follow the instruction mention in binary file(s) can load/execute OS. Isn’t it? Let’s take an example of processor:

Processor: It has several registers used to hold values. Few registers tell processor where the code segment and where the data is in the memory. Using these special registers, processor executes instructions and completes its job. Suppose, the processor reads ‘89D8’ instruction (equivalent to MOV AX,BX assembly code), it will copy the content of BX register into AX register. Similarly, the opcode ‘A0 F4 C4’ (equivalent to MOV AL, [34F4] assembly code) will copy content stored at “34F4” memory address in data segment into AL register.

This is how the processor makes machine and OS usable. Here, you can think that processor is doing nothing but just following the instruction written in OS binaries files. Right?

Now think, can’t we write a program which can behave exactly like a processor? We can write a class to represent a processor and write methods which will perform operations as processor does. The program will work like a processor. Isn’t it? Note that the physical processor is not executing these instructions, a program is doing that. So we can call this program, a ‘virtual’ processor. Can’t we?

Similarly, we can write programs for implementing virtual RAM, Hard disk, monitor, keyboard and motherboard which contains chips like BIOS, System timer chip 8054, Interrupt controller 8259 etc. Combining all these virtual devices will make a virtual machine software like VMWare. Stay tuned for more information on implementation!!!

I hope you’d have enjoyed reading this article. Post your feedback and help me in improving my writing skills/style!

Wednesday, February 15, 2012

How is 'this' pointer passed to the member method?

We know the 'this' pointer points to the object being worked on. All the member methods access object's data using 'this' pointer. C++ Standards doesn't say anything about how to pass 'this' pointer to the Member function. It depends on compiler's calling convention implementation. There are two ways to pass 'this' pointer to the member methods:

1. Pass 'this' pointer to method as argument by pushing it on stack. GCC/CC uses this mechanism.

For example, you can consider that compiler will teat 'void test::display()' method as 'void test::display(test *this)'.

2. Copy 'this' pointer on a register (ECX register is used by VC++). Microsoft VC++ uses this mechanism to provide 'this' pointer to the member methods.

test a;
a.compile_time_binding_method();
     lea ecx,[a] // Address of object 'a' is getting copied in ECX register
     call test::compile_time_binding_method (41118Bh)

You can refer 'Calling Convention' for more information.

Thursday, January 12, 2012

Why does C++ not allow overloading ‘.’ , ‘:*’ , ‘::’ and ‘?:’ operators?

Stroustrup’s wanted to allow programmers to use all the operators with user defined data types as well. Thus he added operator overloading as feature in C++ so that programmers can define functionality of operators for user defined data types. A restriction is that the operators like ‘.’ , ‘:*’ , ‘::’ and ‘?:’ are not allowed for overloading. These operators are not meant to use with any data types. These are introduced to use language’s features like:

“.” Direct member access operator is used to access member variable/function.

“:*” De-reference pointer to class member operator is used to De-reference pointer to class member.

“::” Scope resolution operator is used to access global variable and define method outside class.

“?:” Conditional operator is like if-else condition. Why do we need to overload it.

“Sizeof” operator is used get size of an object. It can’t be overloaded because built-in operations such as incrementing a pointer into an array implicitly depend on it. Consider:

                X a[10];
                X* p = &a[3];
                X* q = &a[3];
                p++;   // p points to a[4]
                          // thus the integer value of p must be
                          // sizeof(X) larger than the value of q

Thus, sizeof(X) could not be given a new and different meaning by the programmer without violating basic language rules.

As above operators are close to the core of the language, allowing overloading of these operators can cause many problems/confusions without any benefits (“->” operator is allowed to overload to make a smart pointer class).

Reference: The Design and Evolution of C++ by Bjarn Stroustrup 

Thursday, January 5, 2012

How is default argument to a method implemented in C++?


C++ allows a function to assign an argument a default value when no argument is specified in a call to that function. The third argument of following function will have “0” when it is not passed by the caller.

int sum(int num1, int num2, int num3 = 0)
{
       return num1 + num2 + num3;
}

When a method is called, all the arguments are pushed on stack and method pops them from stack and copy values in the formal arguments.

Let’s see the dis-assembly code generated for a method call with all the arguments:

sum (1,2,3);
       // Here is the dis-assembly code for passing arguments to sum method
push        3 //push value 3 on stack  
push        2 //push value 2 on stack      
push        1 //push value 1 on stack      
call        sum (41123Fh) // call sum method

Here, we can see that all the three values are pushed on the stack.

Now see the dis-assembly code generated for a method call when no value to supplied for last argument:

sum(1,2);
       // Here is the dis-assembly code for passing arguments to sum method
push        0 //Here 0 as default value for last argument
push        2 //push value 2 on stack      
push        1 //push value 1 on stack      
call        sum (41123Fh) // call sum method

We can see that “push 0” assembly  code is pushing “0” (specified in method’s definition) as default value for last argument “num3”.

Wednesday, January 4, 2012

'this' pointer implementation

In C++, the ‘this’ keyword is a constant pointer to object. It is actually a local variable defined in each member function, including constructors and destructors, which get initialized with object’s address passed by the caller.

Whenever, a public method called using a object, object’s address is passed to the member function and then the member function copies this supplied address to ‘this’ variable. Since ‘this’ variable is created on stack, each member method call will have separate ‘this’ variable on stack. Now using ‘this’ pointer, each of the data member is accessed in the method.

There can be two ways to pass object's address to the member function. 1) By pushing address on stack 2) by copied address in a register. Compiler can use any of the above method or can use any other method too.

Let’s take an example:

class test {

private:

int data;

public:

int public_data;

test() { data = public_data = 0; }

void display()

{ printf("\ndata = %d, public_data = %d", data, public_data); }

};

Let’s see the dis-assembly code generated for public method call on object:

test obj;

// Object’s address is getting copied in ECX register to supply it

// as input to constructor.

lea ecx,[obj]

call test::test (411195h) // constructor is getting called

obj.display();

// Object’s address is getting copied in ECX register to supply it

// as input to display method

lea ecx,[obj]

call test::display (411235h)


Now let's see the dis-assembly code of display method:

void display()

{

........

// Here ECX register contain object's address. Its value is getting copied to

// 'this' variable

mov dword ptr [ebp-8],ecx

printf("\ndata = %d, public_data = %d", data, public_data);

mov esi,esp

mov eax,dword ptr [this] // getting object's address

mov ecx,dword ptr [eax+8] //accessing 'public_data' value using 'this' pointer

push ecx

mov edx,dword ptr [this]

mov eax,dword ptr [edx+4] //accessing 'data' value using 'this' pointer

push eax

push offset string "\ndata = %d, public_data = %d" (415B10h)

call dword ptr [__imp__printf (4192D4h)]

......

}

In above dis-assembly code, we can see that the 'this' pointer is getting initialized with the object's address supplied by the caller. And the data members are getting accessed via 'this' pointer.

Tuesday, January 3, 2012

How does compiler achieve runtime binding/polymorphism?

Compiler determines address of every variables and methods while compiling source code not while executing the binary (it is applicable to virtual methods and dynamic link libs as well). Now the question is when address is determined at compile time, how run time binding works?

Here is the answer:

Whenever a method is called, compiler puts machine level code (‘call’ instruction in assembly level language) and supplies method’s address to call that method. Let’s take an simple example to understand this:

class test {
public:

void compile_time_binding_method()
{ printf("\nIn compile_time_binding_method() method"); }

virtual void run_time_binding_method()
{ printf("\nIn compile_time_binding_method() method"); }

};

Here, the ‘test’ class contain a virtual method ‘run_time_binding_method()’ and a non-virtual method ‘compile_time_binding_method()’.

Let’s create an object, its reference and a pointer to point to the created object:

test a; // Created a object of test class
test &refA = a; // Reference of object ‘a’
test *ptrA = &a; // Pointer of object ‘a’

Let’s call methods using object:

What do think calling a virtual method via its object will be a run time binding? If your answer is no, you are correct. When any method is called using its object, compiler is sure about the method to call. So dynamic binding/call is not at all required here even if a virtual method is being called. You can verify this by reviewing generated dis-assembly code.

a.compile_time_binding_method();
lea ecx,[a] // Dis-assembly code
call test::compile_time_binding_method (41118Bh)

a.run_time_binding_method();
lea ecx,[a] // Dis-assembly code
call test::run_time_binding_method (4110EBh)

In above generated dis-assembly code, we can see that the address of both the methods are hard coded (address determined by compiler while compiling the source code) to resolve the call. This hard coded address will never change (unless you modify and re-compile the source code) in the binary. Such binding/linking is known as static binding/linking (or compile time binding).

These calls are static calls because address is hard coded with machine code generated by compiler (same as ‘call ’ in assembly level language). Instead of using hard coded address, if compiler puts machine code for ‘call EAX’ where EAX register will hold the address of method to call. This way, the value of AX register can be changed any time and can call any method. I.e. Any method can be called at run-time by putting its address in AX register. This is how run-time binding is implemented. See the dis-assembly code generated for following call:

Let’s call methods using its pointer:

For non-virtual methods:

ptrA->compile_time_binding_method();
mov ecx,dword ptr [ptrA]
call test::compile_time_binding_method (41118Bh)

ptrA->run_time_binding_method();
mov eax,dword ptr [ptrA] //getting object's address
mov edx,dword ptr [eax] //getting VTABLE's address
mov esi,esp
mov ecx,dword ptr [ptrA]

//The following line will gets the address of
//run_time_binding_method() from VTABLE
mov eax,dword ptr [edx]
call eax // will call run_time_binding_method
cmp esi,esp
call @ILT+370(__RTC_CheckEsp) (411177h)

Let’s call methods using its reference:

As reference is nothing but an implicit pointer to the object, the method calls via reference is same as method calls via pointer:

refA.compile_time_binding_method();
mov ecx,dword ptr [refA]
call test::compile_time_binding_method (41118Bh)

refA.run_time_binding_method();
mov eax,dword ptr [refA]
mov edx,dword ptr [eax]
mov esi,esp
mov ecx,dword ptr [refA]
mov eax,dword ptr [edx]
call eax
cmp esi,esp
call @ILT+370(__RTC_CheckEsp) (411177h)

As I am not an author by profession, I might not have explained it in a best way J. Please help me make it best by raising your question/doubt.

Monday, January 2, 2012

Reference vs. Pointer

A reference is an alternate name for an object/variable. Reference is an implicit constant pointer to a variable. It can’t be used to point memory location say 0x1000. On the other hand, pointers can be used to point to any location in the memory. To access any address, we'd need to use pointers instead of references.

Here is a simple example of reference/pointer.

int i = 10;

mov dword ptr [i],0Ah // dis-assembly code

Reference:

int &ref = i;

lea eax,[i] // disassembly code

mov dword ptr [ref],eax // dis-assembly code

int j = ref;

mov eax,dword ptr [ref] // dis-assembly code

mov ecx,dword ptr [eax] // dis-assembly code

mov dword ptr [j],ecx // dis-assembly code

Pointer:

int *ptr = &i;

lea eax,[i] // disassembly code

mov dword ptr [ptr],eax // dis-assembly code

j = *ptr;

mov eax,dword ptr [ptr] // dis-assembly code

mov ecx,dword ptr [eax] // dis-assembly code

mov dword ptr [j],ecx // dis-assembly code

If you see their dis assembly code, you can see that the dis assembly code generated for reference and pointer is same. This means that implementation wise they are same.

Use reference as much as you can as you don’t need to use & and * confusing operators J.