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

Wednesday, March 21, 2012

Relationship between high level language data type with the processor's Data type

The size of any data type in any high level language like C/C++ has no direct relationship with the processor's data type. The size of any data type is defined by the language/compiler and it is implementation specific. Each data type is implemented in terms of data type supported by the processor.

Let's take Intel 8086 processor (16 bit) as an example, its instruction set supports following data types:

   Length | Size Name
  ---------|------------
   8-bit   | byte
   16-bit | word

It means, you can perform operations on 1/2 byte (8/16 bit) data. The Intel 8086 processor has no support for 4 byte (32 bit) data. You won't find any 8086 instruction which can perform operation on 4 byte (32 bit) data.

Now let's see the C data types and their sizes:

   Type              | Length | Range
  ------------------|----------|-----------------------------------------
   unsigned char |  8 bits  |                 0 to 255
   char              |  8 bits  |              -128 to 127
   enum            | 16 bits |           -32,768 to 32,767
   unsigned int   | 16 bits |                 0 to 65,535
   short int        | 16 bits |           -32,768 to 32,767
   int                | 16 bits |           -32,768 to 32,767
   unsigned long | 32 bits |                 0 to 4,294,967,295
   long              | 32 bits |    -2,147,483,648 to 2,147,483,647
   
Signed/unsigned Char data type of C/C++ is implemented as 'byte' data type of processor and other 16 bit data types like 'int', 'enum' etc are implemented as 'word' data type of processor. Now the question arises how can compiler support 32 bit data like 'long' on a processor (intel 8086) which doesn't support 32 bit arithmatics/operations ?

The answer is very simple :), the 32 bit data type like 'long' is implemted in terms of two 'word' data types of processor. Compilers implement 32 bit arithmetic operations by using 16 bit arithmetic operations provided by the processor.

Mapping each C/C++ data types with processor's data type doesn't mean that the size of any C/C++ data type will be determined by the processor. The data type of a machine dependent high level language is implemented in terms of processor's data type. It is the language and compiler which provides these data types. Thus the size of these data types will oviously be specified by the language/compiler.

Read Data models for more information on this.

No comments:

Post a Comment