Access to data, BSS segments will be through using a pointer or byintructions directly addressing ?

Discussion in 'C++' started by Good Guy, Oct 18, 2010.

  1. Good Guy

    Good Guy Guest

    I know when it's a matter of accessing memory of a stack frame it'll
    be through using stack frame pointer but I wonder about how the access
    to data, BSS segments containing global/static data will be, through
    using a pointer like stack frame pointer indicating starting point of
    those segments or instructions address pieces of those segments
    directly so that each time application starts the system will have to
    write address parts of instructions in text segment ?
     
    Good Guy, Oct 18, 2010
    #1
    1. Advertising

  2. Good Guy

    red floyd Guest

    Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    On Oct 18, 1:11 pm, Good Guy <> wrote:
    > I know when it's a matter of accessing memory of a stack frame it'll
    > be through using stack frame pointer but I wonder about how the access
    > to data, BSS segments containing global/static data will be, through
    > using a pointer like stack frame pointer indicating starting point of
    > those segments or instructions address pieces of those segments
    > directly so that each time application starts the system will have to
    > write address parts of instructions in text segment ?


    Depends on your implementation.
     
    red floyd, Oct 18, 2010
    #2
    1. Advertising

  3. Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    On Oct 19, 1:11 am, Good Guy <> wrote:
    > I know when it's a matter of accessing memory of a stack frame it'll
    > be through using stack frame pointer but I wonder about how the access
    > to data, BSS segments containing global/static data will be, through
    > using a pointer like stack frame pointer indicating starting point of
    > those segments or instructions address pieces of those segments
    > directly so that each time application starts the system will have to
    > write address parts of instructions in text segment ?


    I think the access to data segments for global variables or static
    vars should not be done through stack pointer ( which accesses the mem
    of stack segment). I think the data segment should have its own data
    structure...just guessing....I am also very excited to know the exact
    answer..
     
    Amarpreet Singh, Oct 19, 2010
    #3
  4. Good Guy

    Good Guy Guest

    Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    On Oct 19, 7:40 am, Amarpreet Singh <> wrote:
    > On Oct 19, 1:11 am, Good Guy <> wrote:
    >
    > > I know when it's a matter of accessing memory of a stack frame it'll
    > > be through using stack frame pointer but I wonder about how the access
    > > to data, BSS segments containing global/static data will be, through
    > > using a pointer like stack frame pointer indicating starting point of
    > > those segments or instructions address pieces of those segments
    > > directly so that each time application starts the system will have to
    > > write address parts of instructions in text segment ?

    >
    > I think the access to data segments for global variables or static
    > vars should not be done through stack pointer ( which accesses the mem
    > of stack segment). I think the data segment should have its own data
    > structure...just guessing....I am also very excited to know the exact
    > answer..


    I didn't mean it might be through using exactly stack/stack frame
    pointer, I meant a pointer with similar purpose.
     
    Good Guy, Oct 19, 2010
    #4
  5. Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    > how the access to data, BSS segments containing global/static data will be, through
    > using a pointer like stack frame pointer indicating starting point of
    > those segments or instructions address pieces of those segments
    > directly so that each time application starts the system will have to
    > write address parts of instructions in text segment ?


    The addresses of global/static variables are resolved during linkage.
    The calculated addresses are relative to the begging of BSS segment
    and each time the application starts may reside at different physical
    memory locations.

    For example,On x86 real-mode, when application starts, the BSS
    starting address will be stored in DS register.
    Then, if one wants to access a global variable at offset 4 from the
    beginning of BSS the compiler will generate something like
    MOV AX,[0x04] which is equal to MOV AX, [DS:0x04] (Actual address
    DS*16 + 4)
    The offset 0x04 will be calculated at link time
     
    Yakov Gerlovin, Oct 19, 2010
    #5
  6. Good Guy

    Jorgen Grahn Guest

    Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    On Tue, 2010-10-19, Yakov Gerlovin wrote:
    >> how the access to data, BSS segments containing global/static data
    >> will be, through
    >> using a pointer like stack frame pointer indicating starting point of
    >> those segments or instructions address pieces of those segments
    >> directly so that each time application starts the system will have to
    >> write address parts of instructions in text segment ?

    >
    > The addresses of global/static variables are resolved during linkage.


    During /linkage/ or during /loading/. E.g. on the Amiga, you had no
    idea which addresses your BSS would end up in until you actually
    loaded (tried to run) the program. Then you had at least two ways of
    loading:

    - keep a base address and do indirect addressing with offset to
    access the various variables, or

    - "write address parts of instructions" according to above.

    The latter was generally considered less efficient, and prevented you
    from sharing the same text segment between two running copies of the
    same program.

    > The calculated addresses are relative to the begging of BSS segment
    > and each time the application starts may reside at different physical
    > memory locations.


    Not on modern Unixes though, and I'm guessing not on Windows either.
    You have virtual memory, so an executable can (and will) contain the
    exact addresses which are later used during runtime.

    In this scenario the code /can/ contain hardcoded addresses ... but if
    that's the most efficient on a certain architecture is a different
    question.

    The simplest way to check is to compile an example and look at the
    generated code.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 19, 2010
    #6
  7. Good Guy

    James Kanze Guest

    Re: Access to data, BSS segments will be through using a pointer orby intructions directly addressing ?

    On Oct 18, 9:11 pm, Good Guy <> wrote:
    > I know when it's a matter of accessing memory of a stack frame it'll
    > be through using stack frame pointer but I wonder about how the access
    > to data, BSS segments containing global/static data will be, through
    > using a pointer like stack frame pointer indicating starting point of
    > those segments or instructions address pieces of those segments
    > directly so that each time application starts the system will have to
    > write address parts of instructions in text segment ?


    There are no real rules; it depends on the architecture. On
    machines with direct addressing, it will typically resolve in an
    instruction using the direct address. On other machines (many
    risc architectures, for example), the generated code will load
    the address into a register, and then access that. And it's
    quite conceivable that the ABI reserves a register for this.

    --
    James Kanze
     
    James Kanze, Oct 20, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. termiflyer
    Replies:
    4
    Views:
    644
    S.Tobias
    Oct 10, 2005
  2. onkar

    variable allocated from stack/bss ??

    onkar, Nov 29, 2006, in forum: C Programming
    Replies:
    148
    Views:
    2,341
    Dave Thompson
    Dec 18, 2006
  3. Replies:
    4
    Views:
    463
    Keith Thompson
    May 23, 2007
  4. karthikbalaguru

    Heap & BSS

    karthikbalaguru, Aug 21, 2007, in forum: C Programming
    Replies:
    13
    Views:
    932
    Ravishankar S
    Aug 23, 2007
  5. text,data and bss

    , Nov 16, 2008, in forum: C Programming
    Replies:
    27
    Views:
    7,212
Loading...

Share This Page