Wednesday, June 13, 2012

Memory Mapping Fully Explained


For Complete Java Guidance(J2SE,J2EE) you can switch over to myjavaquestionbank.blogspot.in

This tutorial explains  
                                     1. C compiler.
                                     2. Turbo C compiler.
                                     3. Hexadecimal Number System.
                                     4. Address Range represented in 20 bit.
                                     5. Difference Between TSO and TSR program.
                                     6. Difference between .com program and .exe program.
                                     
                                     7. Memory cell.
                                     8. Residence memory.
                                     9. Physical Address.
                                     10. Segmentation.
                                     11. Offset Address.
                                     12. Data segement.



C compiler
There are various c compilers are variables. Some of these are:
S.N.
Name
Microprocessor
OS
1
Turbo c 3.0
8086
MS DOS
2
ANSIC C
80386
LINUX
3
Borland C 4.0
80386
WINDOW
4
Microsoft C
8086
MS DOS
5
Visual C++
80386
WINDOW

Note: 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.


Note: Different versions of compilers are based on the different microprocessors and support many OS. It is always changing. As a programmer you should know the microprocessor name, its world length etc. which your compiler is based on. Since c language is platform dependent. In preprocessor section you will know how to make a program as much as platform independent.


Turbo c compiler

Turbo c is an IDE of c programming language created by Borland. Turbo C 3.0  is based on MS DOS operation  system. It is one of the most popular c compilers. It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses and 16 data bus. Its word length is two byte. 


Size of data types in Turbo C 3.0:


Data type
Size
short int
2
int
2
long int
4
char
1
float
4
double
8
long double
10

Byte ordering : Little Endianness
Default pointer : Near
Default memory model : Small


To compile a c program: Alt + F9
To run a c program: Ctrl + F9

Turbo C 4.5 is based on Microsoft window operating system. It is 32 bit compilers.
Size of data type in Turbo C 4.5:

Data type
Size (Byte)
short int
2
int
4
long int
4
char
1
float
4
double
10
long double
12

Default pointer: Far
Default memory model: Compact

Hexadecimal representation in c
In hexadecimal number system we use 16 different digits so its base is 16. List of all hexadecimal digits:

Hexadecimal digit
Decimal equivalent
Binary equivalent
0
0
0000
1
1
0001
2
2
0010
3
3
0011
4
4
0100
5
5
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
A
10
1010
B
11
1011
C
12
1100
D
13
1101
E
14
1110
F
15
1111



To convert the binary number into hexadecimal number:

Make the group of four binary digits from right to left and replace the four binary digits with the equivalent hexadecimal digit using above table.
For example:

Binary number = 11000111110101
Group of four digits from right side:


to make group of four digit of left most  digit 11 , add two zero at the left side i.e. 0011

Now put or replace with it the equivalent hexadecimal digit using above table


So, equivalent hexadecimal number will be 31F5.


What will be address range which can be represented in 20 bit?

Binary
Hexadecimal
Min possible
0000 0000 0000 0000 0000                            
00000
Max possible
1111 1111 1111 1111 1111    
FFFFF


In c any hexadecimal number start with 0x 0r 0X So, address range   will be 0x00000 to 0xFFFFF. So in turbo C 3.0 memory address of all variables must be within 0x00000 to oxFFFFF.

It is 1MB memory range.

Note.
2^10 = 1KB
2^20 = 1MB
2^30 = 1GB  

Where 10, 20, 30 are number of bit.

Difference between TSR and TSO program

Difference between TSR and TSO program


TSO means terminate but stay outside. It is that program, which release the main memory after the execution of the program. Example ms paint, notepad, turbo c compilers etc.

TSR means terminate but stay residence .It is those program, which after the execution of the program does not release the RAM (main memory).e.g. antivirus.
Difference between .com program and .exe program in c programming language

Both .com and .exe program are executable program but .com program execute faster than .exe program. All drivers are .com program. .com file has higher preference than .exe 
For example:
Open the command prompt in window OS. (Type CMD in Run)
In the command prompt type notepad and press enter key you will get the notepad. Since it executes notepad.exe 


Repeat the same task but now first create any .com file in the same working directory. We can create it as open the notepad save it as notepad.com, set save as type is All files or we can create the .com file from command prompt.


Then type notepad in command prompt and press the enter key you will get error message like:   
C:\notepad.com is not a valid Win32 application.


It proves that .com has higher precedence than .exe program.

Com file is binary execute used in MS DOS. Com program keeps only code and data. It stores code and data in one segment. In Turbo C memory model should tiny to create .com program. We will discuss later how to create .com file in in turbo c.



Memory cell in computer


Entire RAM has divided in numbers of equal parts, which are
known as memory cells. Following diagram represents the 256
MB RAM.


Each cell can store one-byte data. Data are stored in the
binary number system. That is a character data reserves one
memory cell while floating data reserves four memory cells.

Each memory cell has unique address. Address is always in
whole number and must be in increasing order. We will discuss
how a characters, integers etc. data are in stored in the
data type chapter. Just for now assume

int a = 4;

Here variables a stores in the memory in the flowing way:


If you know memory address of first cell is 0x5000 then
what would be the memory address of next memory cell?

It will 5001 since integer data always stores at
continuous memory location and as we know memory address
always in increasing order.


What is residence memory in c programming language?
RAM has divided into two parts:
(1)        Extended memory (useless)
(2)        Residence memory 






















































In Turbo C 3.0 compiler size of residence memory is 1MB.
Residence memory:
 
When any program is executed it is stored in the
residence memory. For turbo c 3.0, it has 1MB residence
memory i.e. when we open turbo c 3.0 it stores 1MB in the
RAM.

Segmentation in operating system


Segmentation in c programming language

Residential memory of RAM of size 1MB has divided into 16 
equal parts. These parts is called segment. Each segment 
has size is 64 KB.

16 * 64 KB = 1 MB






This process of division is known as segmentation.



Note: In turbo c 3.0 physical addresses of any variables

are stored in the 20 bits. But we have not any pointers 

(We will discuss later what is pointer?)of size 20 bits.
So pointers cannot access whole residence memory 
address.To solve this problem we there are three types
pointers in c language. They are

1. Near pointer
2. Far pointer
3. Huge pointer

Data segment in c

All the segments are used for specific purpose. Like segment number 15 is used for ROM, segment number 14 is used for BIOS etc.  



We will discuss about how to access text video memory, graphics video memory in the pointer and union chapters of 255 bits color graphics programming.
Segment number eight has special name which is known as data segment. This segment has been divided into four parts. This is very important for c programming




1. Stack area
All automatic variables and constants are stored into stack area. Automatic variables and constants in c:
1.       All the local variables of default storage class.
2.       Variables of storage calls auto.
3.       Integer constants, character constants, string constants, float constants etc in any expression.
4.       Function parameters and function return value.
Variables in the stack area are always deleted when program control reaches it out of scope. Due to this stack area is also called temporary memory area. For example:
What will be output of following c code?
#include
int main(){
    int i;
    for(i=0;i<3;i++){
         int a=5;
         printf("%d",a);
    }
    return 0;
}
Output: 5 5 5
Explanation: Since variable a is automatic variable, it will store in the stack area. Scope of variable a is within for loop. So after each iteration variable a will be deleted from stack and in each iteration variable a will initialize.  
This two concepts are only for Turbo C 3.0
It follows LIFO data structure. That in the stack area of memory data is stored in last in first out. For example:
What will be output of flowing c code. (Turbo c 3.0)?
#include
int main(){
    int a =5, b = 6, c = 7,d =8;
    printf("%d %d %d");
    return 0;
}
Output: 8 7 6
Explanation:
Default storage class of variables a, b, c and d is auto .Since it automatic variable it will be sorted in the stack area of memory. It will store in the stack as


Stack always follows LIFO data structure. In the printf function, name of variables is not written explicitly. So default output will content of stack which will be in the LIFO order i.e. 8 7 6.
It has two part one for initialize variable another for non-initialize variable. All initialize variables are more nearer than not initialized variable and vice versa. For example:
What will be output of following program (Turbo c 3.0)?
#include
int main(){
    int a =5, b, c =7;
    printf("%d %d %d");
    return 0;
}
Output: 7 5 garbage value
Explanation:
Automatic variable a and c has initialized while b has not initialized. Initialize variables are more nearer than uninitialized variable .They will be stored in the stack. So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is present in the stack.
Note: Default storage class of any local variable is auto.
2. Data area:
All static and extern variable are stored in the data area. It is permanent memory space and variable will store in the memory unless and until program end. For example:
What will be output of following c code?
#include
int main(){
    int i;
    for(i=0;i<3;i++){
         static int a=5;
         printf("%d",a);
    }
    return 0;
}
 Output: 5 6 7
3. Heap area:
This memory area is use to allocate memory dynamically. In c we can allocate the memory space dynamically by using function malloc and calloc. It always allocates memory in the heap area. Its size is variable and depends upon free space in the memory.
4. Code area:
Function pointer can only access code area. Size of this area is always fixed and it is read only memory area.




1 comment:

Thank for visting this blog . Please Pass on this information to all of Our friends to utilize this oppurtunity.