Memory Cell in Computer

download Memory Cell in Computer

of 26

Transcript of Memory Cell in Computer

  • 7/31/2019 Memory Cell in Computer

    1/26

    Memory cell in computer

    Entire RAM has divided in numbers of equal parts, which

    areknown as memory cells. Following diagram represents the256MB RAM.

  • 7/31/2019 Memory Cell in Computer

    2/26

    http://4.bp.blogspot.com/_uIwyaTjqYYw/TUmu1VBmgtI/AAAAAAAABRs/JsqH_qUkRZI/s1600/memory1.jpeg
  • 7/31/2019 Memory Cell in Computer

    3/26

    Each cell can store one-byte data. Data are stored inthebinary number system. That is a character data reservesone

    memory cell while floating data reserves four memorycells.

    Each memory cell has unique address. Address is alwaysinwhole number and must be in increasing order. We willdiscusshow a characters, integers etc. data are in stored inthedata type chapter. Just for now assume

    int a = 4;

    Here variables a stores in the memory in the flowingway:

    If you know memory address of first cell is 0x5000thenwhat would be the memory address of next memory cell?

    It will 5001 since integer data always stores atcontinuous memory location and as we know memoryaddress

    always in increasing order.

    physical address of computer

    All the c variables are stored in the residence memory.In turbo C 3.0, 20 bits address of the memory cell is known as physical address or real address. In 20 bits,

    http://2.bp.blogspot.com/_uIwyaTjqYYw/TUmu6wzR1jI/AAAAAAAABRw/JRrpCzKRXss/s1600/mpq.png
  • 7/31/2019 Memory Cell in Computer

    4/26

    we can represent address from 0x00000 to 0xFFFFF.Thatis all c variables must have memory address withinthisrange.

    http://3.bp.blogspot.com/_uIwyaTjqYYw/TUm2Xyn1zlI/AAAAAAAABR8/kT2ix7-JXj0/s1600/memory3.jpeg
  • 7/31/2019 Memory Cell in Computer

    5/26

    A C programmer cannot not decides what will be thememoryaddress of any variables. It is decided by compiler.For Example:

    What will be output of following c code?

    #includeintmain(){

    int a;

  • 7/31/2019 Memory Cell in Computer

    6/26

    printf("%x",&x);return 0;

    }

    Output: We cannot predict.

    But we can say in 16 bits compilers address must bewithin 0x0000 to 0xFFFF and in 32 bits compilers memoryaddress must be within 0x00000000 to 0xFFFFFFFF.

    Note: Suppose your c compiler is based on themicroprocessor which total address buses are 32 thenitstotal size of addressable memory will be:

    = 2 ^ 32 bytes

    = 4 GBSegmentation in c programming languageResidential memory of RAM of size 1MB has divided into16equal parts. These parts is called segment. Eachsegmenthas size is 64 KB.16 * 64 KB = 1 MB

    http://4.bp.blogspot.com/_uIwyaTjqYYw/TUm2YyYAc3I/AAAAAAAABSA/NQT_9MsA6xY/s1600/addressable.png
  • 7/31/2019 Memory Cell in Computer

    7/26

    This process of division is known as segmentation.

    Note: In turbo c 3.0 physical addresses of anyvariables

    http://1.bp.blogspot.com/_uIwyaTjqYYw/TU1lDpNq97I/AAAAAAAABSE/vdOAH5egX3w/s1600/Concept1.png
  • 7/31/2019 Memory Cell in Computer

    8/26

    are stored in the 20 bits. But we have not anypointers

    (We will discuss later what is pointer?)of size 20bits.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 pointer2.Far pointer3.Huge pointer

    Offset address and segment number in c programminglanguage

    Each segment has divided into two parts.

    1. Segment no (4 bit)2. Offset address (16 bit)

    http://4.bp.blogspot.com/_uIwyaTjqYYw/TU2VU3WKBMI/AAAAAAAABSI/99bSIzXJfX8/s1600/memory2.png
  • 7/31/2019 Memory Cell in Computer

    9/26

    So, in the other words we can say memory address of anyvariable in c has two parts segment number and offsetaddress.

    In turbo c 3.0 a particular segment number offsetaddress varies from 0x0000 to 0xFFFF

    Suppose physical address of any variable in c is0x500F1.Then its segment number is 5 and offset address is00F1.

    Write a program to find the offset address of anyvariable?

    #includeint main(){

    int x;printf("%u ",&x); //To print offset addressprintf("%p ",x); //To print segment addressprintf("%p ",&x); //To print offset address

    printf("%fp ",&x); //To print segment address : offsetaddress

    return 0;

    }

    Data segment in c

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

  • 7/31/2019 Memory Cell in Computer

    10/26

    http://1.bp.blogspot.com/_uIwyaTjqYYw/TU2XAMKJnXI/AAAAAAAABSM/5EOm5g5pAjQ/s1600/memory.png
  • 7/31/2019 Memory Cell in Computer

    11/26

    We will discuss about how to access text video memory,graphics video memory in the pointer and union chaptersof 255 bits color graphics programming.

    Segment number eight has special name which is known as

    data segment. This segment has been divided into fourparts. 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, stringconstants, float constants etc in any expression.4. Function parameters and function return value.

    Variables in the stack area are always deleted whenprogram control reaches it out of scope. Due to this

    stack area is also called temporary memory area. Forexample:

    What will be output of following c code?

    #includeintmain(){

    http://1.bp.blogspot.com/_uIwyaTjqYYw/TU2XEGhdEKI/AAAAAAAABSQ/47Sj7lyNK4c/s1600/Concept25.png
  • 7/31/2019 Memory Cell in Computer

    12/26

    int i;for(i=0;i

  • 7/31/2019 Memory Cell in Computer

    13/26

    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 bein the LIFO order i.e. 8 7 6.

    It has two part one for initialize variable another fornon-initialize variable. All initialize variables aremore nearer than not initialized variable and viceversa. For example:

    What will be output of following program (Turbo c 3.0)?

    #includeintmain(){

    int a =5, b, c =7;printf("%d %d %d");return 0;

    }

    Output: 7 5 garbage valueExplanation:

    Automatic variable a and c has initialized while b hasnot initialized. Initialize variables are more nearerthan uninitialized variable .They will be stored in thestack. So due to LIFO first output will be 7 then 6(since a is more nearer than b with respect to c) then

    http://1.bp.blogspot.com/_uIwyaTjqYYw/TU2XE3mwetI/AAAAAAAABSU/-N7IGge2xpo/s1600/Concept1h.png
  • 7/31/2019 Memory Cell in Computer

    14/26

    any garbage value will be output which is present inthe stack.

    Note: Default storage class of any local variable isauto.

    2. Data area:

    All static and extern variable are stored in the dataarea. It is permanent memory space and variable willstore in the memory unless and until program end. Forexample:

    What will be output of following c code?

    #includeintmain(){

    int i;for(i=0;i

  • 7/31/2019 Memory Cell in Computer

    15/26

    Memory representation of:signedchar a=7;

    Binary equivalent of data 7 in eight bit: 00000111Data bit: 0000111 (Take first seven bit form rightside)Sign bit: 0 (Take leftmost one bit)In memory:

    Memory representation of:signedchar a=-7;

    Binary equivalent of data 7 in eight bit: 00000111Binary equivalent of data -7 will be its 2s

    complement:1s complements of 0000 0111 is 1111 1000

    2s complement will be:

    http://2.bp.blogspot.com/_uIwyaTjqYYw/TU5q5s1omjI/AAAAAAAABTg/pfUH85kQ5UM/s1600/17.jpeg
  • 7/31/2019 Memory Cell in Computer

    16/26

    Binary equivalent of data -7 in eight bit: 1111 1001Data bit: 1111001 (Take first seven bit form rightside)Sign bit: 1 (Take leftmost one bit)

    In memory

    Memory representation of int in cMemory representation of unsigned int:Total size of unsigned int: 16 bitThose eight bits are use as:Data bit: 16

    http://4.bp.blogspot.com/_uIwyaTjqYYw/TU5rdHQLdzI/AAAAAAAABTo/NfCnaAy1sz8/s1600/18.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5rbo3oKqI/AAAAAAAABTk/J55hpmLYz30/s1600/16.jpeghttp://4.bp.blogspot.com/_uIwyaTjqYYw/TU5rdHQLdzI/AAAAAAAABTo/NfCnaAy1sz8/s1600/18.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5rbo3oKqI/AAAAAAAABTk/J55hpmLYz30/s1600/16.jpeg
  • 7/31/2019 Memory Cell in Computer

    17/26

    Example:(1)Memory representation of:

    unsigned int a=7; (In Turbo c compiler)unsigned short int a=7 (Both turbo c and Linux gcccompiler)

    Binary equivalent of data 7 in 16 bit: 0000000000000111Data bit: 00000000 00000111First eight bit of data bit from right side i.e.00000111 will store in the leftmost byte from right toleft side and rest eight bit of data bit i.e. 00000000will store in rightmost byte from right to left side asshown in the following figure:

    Memory representation of signed int

    Total size of unsigned int: 16 bit

    Those eight bits are use as:Data bit: 15Signe bit: 1

    http://1.bp.blogspot.com/_uIwyaTjqYYw/TU5tdSyliqI/AAAAAAAABT4/ywduBqNPWpw/s1600/23.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5tcFFR-EI/AAAAAAAABT0/AxY_yPDzpnM/s1600/21.jpeghttp://1.bp.blogspot.com/_uIwyaTjqYYw/TU5tdSyliqI/AAAAAAAABT4/ywduBqNPWpw/s1600/23.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5tcFFR-EI/AAAAAAAABT0/AxY_yPDzpnM/s1600/21.jpeg
  • 7/31/2019 Memory Cell in Computer

    18/26

    Note:In c negative number is stored in 2s complementformat.

    Example:(1)Memory representation of:

    signed int a=7; (In Turbo c compiler)signed short int a=7 (Both turbo c and Linux gcccompiler)Binary equivalent of data 7 in 16 bit: 0000000000000111Data bit: 0000000 00000111 (Take first 15 bit formright side)

    Sign bit: 0 (Take leftmost one bit)

    First eight bit of data bit from right side i.e.00000111 will store in the leftmost byte from right toleft side and rest seven bit of data bit i.e. 0000000will store in rightmost byte from right to left side asshown in the following figure:

    http://3.bp.blogspot.com/_uIwyaTjqYYw/TU5uNwnFaXI/AAAAAAAABUA/2UTOyW5NvHI/s1600/24.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5uMC9dYPI/AAAAAAAABT8/LvVvh1w34tc/s1600/22.jpeghttp://3.bp.blogspot.com/_uIwyaTjqYYw/TU5uNwnFaXI/AAAAAAAABUA/2UTOyW5NvHI/s1600/24.jpeghttp://2.bp.blogspot.com/_uIwyaTjqYYw/TU5uMC9dYPI/AAAAAAAABT8/LvVvh1w34tc/s1600/22.jpeg
  • 7/31/2019 Memory Cell in Computer

    19/26

    (2)Memory representation of:

    signed int a=-7; (In Turbo c compiler)signed short int a=-7 (Both turbo c and Linux gcccompiler)Binary equivalent of data 7 in 16 bit: 0000000000000111Binary equivalent of data -7 will be its 2scomplement:1s complements of 00000000 00000111 is 11111111

    111110002s complement will be:

    Binary equivalent of data -7 in 16 bit: 1111111111111001Data bit: 1111111 11111001 (Take first 15 bit formright side)

    Sign bit: 1 (Take leftmost one bit)First eight bit of data bit from right side i.e.00000111 will store in the leftmost byte from right toleft side and rest seven bit of data bit i.e. 1111111will store in rightmost byte from right to left side asshown in the following figure:

    http://3.bp.blogspot.com/_uIwyaTjqYYw/TU5uPeFsRiI/AAAAAAAABUE/6Ja_rQAPmKk/s1600/25.jpeg
  • 7/31/2019 Memory Cell in Computer

    20/26

    Memory representation of float data type in c(Both in Turbo c compiler and Linux gcc compiler)Float numbers are stored in exponential form i.e.

    (Mantissa)*10^ (Exponent)

    Here * indicates multiplication and ^ indicates power.In memory only Mantissa and Exponent is stored not *,10 and ^.Total size of float data type: 32 bitThose bits are used in following manner:Exponent bit: 8Mantissa bit: 24Mantissa is signed number, so 24 bit are used as:Mantissa_sign bit: 1Mantisaa_data bit: 23For only mantissa:Mantissa_sign bit will zero if number is positive andMantissa_sign bit will one if number is negative.Exponent is also signed number, So 8 bit are used as:Exponent_sign bit: 1Exponent_data bit: 7

    Following figure illustrate how floating point numberis stored in memory.

    http://2.bp.blogspot.com/_uIwyaTjqYYw/TU5uRPzLTzI/AAAAAAAABUI/3QNac-2_r_E/s1600/26.jpeg
  • 7/31/2019 Memory Cell in Computer

    21/26

    Five important rules:

    Rule 1: To find the mantissa and exponent, we convertdata into scientific form.Rule 2: Before the storing of exponent, 127 is added toexponent.Rule 3: Exponent is stored in memory in first byte from

    right to left side.

    Rule 4: If exponent will negative number it will bestored in 2s complement form.Rule 5: Mantissa is stored in the memory in second byteonward from right to left side.Example:

    Memory representation of:

    float a = -10.3f;

    For this you have to follow following steps:

    step1: convert the number (10.3) into binary formBinary value of 10.3 is:1010.0100110011001100110011001100110011

    http://4.bp.blogspot.com/_uIwyaTjqYYw/TU5vgZrpUzI/AAAAAAAABUM/VTkeLcylqqE/s1600/27.jpeg
  • 7/31/2019 Memory Cell in Computer

    22/26

    step2: convert the above binary number in thescientific form. Scientific form of1010.0100110011001100110011001100110011=

    1.0100100110011001100110011001100110011*10^3Note: First digit i.e. 1, decimal point symbol, base ofpower i.e. 10, power symbol ^ and multiplication symbol* are not stored in the memory.

    Step3: find exponent and mantissa and signed bitMantissa_data bit in binary = 0100100 1100110011001101

    (Only first 23 bit from leftside)

    Mantissa_sign bit: 1 (Since it is a negative number)Exponent in decimal: 3Question:Why we have taken right most bit of mantissa_data bitone instead of zero?

    Step 5: Add 127 in the exponent and convert in thebinary number form.(Why 127? since size of exponent_data bit is 7 andmaximum possible number in seven bit will 1111111 in

    binary or 127 in decimal)

    Exponent= 127+3=130Binary value of 130 in eight bit: 1000001 0Exponent_data bit: 1000001

    (Take first seven bit from left side)Exponent_sign bit: 0 (Take rightmost bit)

    Step 6: Now store the Mantissa_data bit, Mantissa_signbit, Exponent_data bit and Exponent_sign bit atappropriate location as shown in the following figure.

  • 7/31/2019 Memory Cell in Computer

    23/26

    Note: Mantissa_data bits are stored from left to rightwhile Exponent_data bits are stored from right to left.

    How to check above memory representation is correct?

    Answer:

    We will take one char pointer and visit each byte of afloat number and observe the output.C program:#includeintmain(){

    int i;float f=-10.3f;char *p=(char *)&f;for(i=0;i

  • 7/31/2019 Memory Cell in Computer

    24/26

    Binary value of -63 in eight bit: 11000001This is exactly same as which we have represented inmemory in the above figure.

    const modifier in c

    In c all variables are by default not constant.Hence, you can modify the value of variable by program.You can convert any variable as a constant variable byusing modifier const which is keyword of c language.Properties of constant variable:

    1. You can assign the value to the constant variablesonly at the time of declaration. For example:

    constint i=10;floatconst f=0.0f;unsignedconstlongdouble ld=3.14L;

    2. Uninitialized constant variable is not cause of anycompilation error. But you can not assign any valueafter the declaration. For example:

    constint i;

    If you have declared the uninitialized variableglobally then default initial value will be zero incase of integral data type and null in case of non-integral data type. If you have declared theuninitialized const variable locally then defaultinitial value will be garbage.

    3. Constant variables executes faster than not constantvariables.

    4. You can modify constant variable with the help ofpointers. For example:

    #includeintmain(){

    int i=10;

  • 7/31/2019 Memory Cell in Computer

    25/26

    int *ptr=&i;*ptr=(int *)20;printf("%d",i);return 0;

    }

    volatile modifier in cAll variable in c are by default not volatile. Withhelp of modifier volatile which is keyword of clanguage you can make any variable as volatilevariable.Properties of volatile variable:

    1. A volatile variable can be changed by the backgroundroutine of preprocessor. This background routine may beinterrupt signals by microprocessor, threads, realtimes clocks etc.

    2. In simple word we can say a value volatile variablewhich has stored in the memory can be by any externalsources.

    3. Whenever compiler encounter any reference ofvolatile variable is always load the value of variablefrom memory so that if any external source has modifiedthe value in the memory complier will get its updatedvalue.

    4. Working principle of volatile variable is oppositeto the register variable in c. Hence volatile variablestake more execution time than non-volatile variables.

    A volatile variable is declared with help of keywordvolatile:

    intvolatile i;

    A non-volatile variable is declared without usingkeyword volatile:

    int i;

  • 7/31/2019 Memory Cell in Computer

    26/26

    Question: What is meaning of following declaration inc?constvolatilefloat f;registervolatilechar c;