Thread: How do we know the memory arrangement using in microprocessors? Top-Bottom,Bottom-Top

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    How do we know the memory arrangement using in microprocessors? Top-Bottom,Bottom-Top

    Hi folks,

    This question is a little deep into memory management of microprocessor.

    How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top?
    For example, the "Top-Bottom" is arranging memory resource from higher address to lower address.

    If we have no hardware knowledge of microprocessors, how can we know the arrangement of microprocessors?

    I have idea which is declearing an array and check the addresses of array elements because the allocation of array elements is sequential. And, then, we know how memory resource is arranged.

    BUT, is there any other way to find out?

    Code:
    // My complier is gcc v4.1.1
    
    #include <stdio.h>
    
    int main (void)
    {
        char var[10];
        int i;
        for ( i = 0; i < 10; i++)
            printf("addr = %X\n", &var[i]);
        
        return 0;
    }
    
    // Bottom-Top arrangement:
    // Result:
    $ ./addr
    addr = BF9CEF66
    addr = BF9CEF67
    addr = BF9CEF68
    addr = BF9CEF69
    addr = BF9CEF6A
    addr = BF9CEF6B
    addr = BF9CEF6C
    addr = BF9CEF6D
    addr = BF9CEF6E
    addr = BF9CEF6F
    $

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("addr = %X\n", &var[i]);
    You can also do:
    printf("addr = %p\n", &var[i]);

    I'm not sure about your question though.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Sorry, it's not about how to print the addresses. Thank you.

    How to find out the memory arrangement in microprocessors or in its MMU using only software techniques?

    Cuthbert

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not sure either. It's also odd to me a... top-bottom(?) arrangement. I'm thinking pointer arithmetic being rather strange. What happens on this case? Adding to the pointer moves up in the array, but down in memory address?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Sorry, it's not about how to print the addresses. Thank you.
    Which is why I said: I'm not sure about your question though.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Sorry, my way is meaningless. The address of array elements is always increased along indexes.
    Maybe different dynamic objects can reflect the real order of memory allocation. I guass.

    Cuthbert

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by cuthbert
    Hi folks,

    This question is a little deep into memory management of microprocessor.

    How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top?
    For example, the "Top-Bottom" is arranging memory resource from higher address to lower address.

    If we have no hardware knowledge of microprocessors, how can we know the arrangement of microprocessors?

    I have idea which is declearing an array and check the addresses of array elements because the allocation of array elements is sequential. And, then, we know how memory resource is arranged.

    BUT, is there any other way to find out?

    Code:
    // My complier is gcc v4.1.1
    
    #include <stdio.h>
    
    int main (void)
    {
        char var[10];
        int i;
        for ( i = 0; i < 10; i++)
            printf("addr = %X\n", &var[i]);
        
        return 0;
    }
    
    // Bottom-Top arrangement:
    // Result:
    $ ./addr
    addr = BF9CEF66
    addr = BF9CEF67
    addr = BF9CEF68
    addr = BF9CEF69
    addr = BF9CEF6A
    addr = BF9CEF6B
    addr = BF9CEF6C
    addr = BF9CEF6D
    addr = BF9CEF6E
    addr = BF9CEF6F
    $
    If you know the processor you can get the manual which describes the instruction set and how the processor works, so you can see what a 'push' statement does, for example for an intel chip:-
    ftp://download.intel.com/design/Pent...s/25366520.pdf
    "When an item is pushed onto the stack, the processor decrements the ESP register, then writes the item at the new top of stack."
    However what happens depends on how the compiler is actually written, although there may be 'standard' ways of writing compilers, not everyone sticks to the standards!!
    Anyway it seems like your data was created on the heap, which I think is normal a bottom to top 'thing'. (By the way if you are in a chat room and if someone asks you if you are a top or bottom it's probably a good time to leave )
    Anyway, it depends upon what you mean to a certain extent, for example, you might think
    from your experiment that in the following code that the address of var2[0] would be higher
    than var[9]

    Code:
    char var[10];
    char var2[10];
    However in the following code:-

    Code:
    
    int main (void)
    {
        char var[10];
        char var2[10];
        int i;
        for ( i = 0; i < 10; i++)
            printf("addr = %X\n", &var[i]);
    		
         for ( i = 0; i < 10; i++)
            printf("addr = %X\n", &var2[i]);
        
        return 0;
    }
    However it is not (well not on my setup anyway) as the results show:-
    ===========
    addr = 8FF42
    addr = 8FF43
    addr = 8FF44
    addr = 8FF45
    addr = 8FF46
    addr = 8FF47
    addr = 8FF48
    addr = 8FF49
    addr = 8FF4A
    addr = 8FF4B
    addr = 8FF38
    addr = 8FF39
    addr = 8FF3A
    addr = 8FF3B
    addr = 8FF3C
    addr = 8FF3D
    addr = 8FF3E
    addr = 8FF3F
    addr = 8FF40
    addr = 8FF41
    ===========

    So it seems they are created on the stack, but prehaps not as I expected!!

    I remember at one time in work we had a program which was 'ported' to several different machines (processors), it failed to run on one (a RISC machine, I believe) because it managed it's memory 'upside down' which cause a minor bug to overwrite a critical piece of data, rather
    then the non-critical data it overwrote on the other machines. However if you code is bug free you don't have to worry to much about such things!!
    So...you probably need to know how the processor and the compiler works to be sure!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by esbo
    I remember at one time in work we had a program which was 'ported' to several different machines (processors), it failed to run on one (a RISC machine, I believe) because it managed it's memory 'upside down' which cause a minor bug to overwrite a critical piece of data, rather
    then the non-critical data it overwrote on the other machines. However if you code is bug free you don't have to worry to much about such things!!
    So...you probably need to know how the processor and the compiler works to be sure!
    Probably because you wrote it and figured "works for me!" instead of writing it correctly in the first place. You'd think you'd learn.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by esbo
    So it seems they are created on the stack, but prehaps not as I expected!!
    Of course it's located on the stack. Automatic local always variables are.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cuthbert
    If we have no hardware knowledge of microprocessors, how can we know the arrangement of microprocessors?
    Generally by looking at the manual.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might be able to malloc an array, then malloc another array, and compare the addresses of their first elements. At least on a microprocessor, it might tell you which way the heap is growing.
    Code:
       int *arraya, int *arrayb;
       arraya = malloc(BUFSIZ * sizeof(*arraya));
       arrayb = malloc(BUFSIZ * sizeof(*arrayb));
       printf("%p\n", arraya);
       printf("%p\n", arrayb);
    It seems like I've read that the stack may grow in the opposite direction, depending on the architecture.
    Last edited by swoopy; 09-12-2006 at 08:32 PM.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Unless you are coding in asm or directly using EBP and ESP in C code, which you shouldn't be, I don't see why this entire thread has any relevance.

    Your compiler will handle the stack and I wouldn't recommend fiddling with it unless you have good reason to - which most people don't when they do.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I don't see why this entire thread has any relevance.
    I think it's like owning a car. Some people just want to drive it. Some people want to know what's under the hood. To be honest, that's a very peculiar quote coming from you Bubba.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > using only software techniques?
    You can't, and in well-written programs there is never any need to know.

    Code:
    void foo ( ) {
      int i, j;
      printf( "%p %p\n", (void*)&i, (void*)&j );
    }
    There is NOTHING you can infer from looking at the two addresses which get printed out.
    - you can't tell which direction the stack grows
    - you can't tell whether you even have a stack (the C standard doesn't require one, it's just a common implementation choice)
    - you can't tell how the compiler chooses to lay out variables in memory
    - you can't tell the size of the variables from the difference between the pointers.

    Only arrays within their bounds have consecutive memory addresses from one element to the next.
    struct members are arranged in the order you declare them, but you can't tell in advance what any padding will be between members.
    Anything else is off in fantasy land.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think it's like owning a car. Some people just want to drive it. Some people want to know what's under the hood. To be honest, that's a very peculiar quote coming from you Bubba
    This thread makes no sense and at the very least does not belong on this particular board b/c it has nothing to do with C and everything to do with CPU and architecture. Maybe that's why it struck me as irrelevant barring the fact that there is no way to tell unless you read the processor's manual.

    But for the heck of it the Intel tech ref will tell you all you need to know about AMD/Intel CPUs. AFAI remember they can grow either way. I do believe the FPU stack grows down. But I could be wrong since it's been ages since I've messed with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 05:16 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. memory checking a daemon using top
    By rotis23 in forum Linux Programming
    Replies: 3
    Last Post: 11-04-2002, 09:48 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM