May. 31st, 2009

jack: (Default)
A friend said she was going to GM a one-off Dogs in the Vineyard (aka Mormons with Guns) evening sometime in the next couple of weeks. Would anyone else be interested if she had space?
jack: (Default)
I wrote a long essay for Liv trying to give a whirlwind tour through the different way programs have allocated memory between 1980s BASICs and today, but realised I didn't necessarily have wide enough knowledge to really do it justice.

It's obviously quite specific as it's aimed at people who've done some programming, but not recently.

If you've any suggestions for how it could be helpful, please shout. I was originally going to put it on toothywiki so someone could make sweeping changes if they happened to feel like it, but didn't feel like trying to format it.

1. STATIC MEMORY ALLOCATION

Suppose you know in advance how many memory locations your program will need to store things. Then you can just designate them in advance, when the program is written or compiled, and not need to do anything special while the program runs.

In C and other languages, this is represented by global variables. Global variables are declared somewhere near the start of the program, and can be accessed at any time in the duration of the program, so the compiler knows exactly how many global variables there are, so when it compiles the program, it can say "OK, there's 20 bytes of code, and a further 10 bytes to store variable 1, variable 2, etc."

A simple part of a program might be:
  unsigned int N; // global variable
  int sum_1_to_20() {
    N=20;
    return N * (N+1) / 2;
  }

And this is compiled into machine code:
  mov  dword ptr [N (405120h)],14h  // Store 20 (0x14) in memory location 0x405120.
  mov  eax,dword ptr [N (405120h)]  // Load memory location 0x405120 into register eax
  lea  ecx,[eax+1]                  // Put that value plus one into register ecx
  imul eax,ecx                      // Multiply register eax by register ecx
  shr  eax,1                        // Shift all the bits in eax right (ie. divides by two).
  ret                                // Return from function (answer in register eax)

FOOTNOTE

In actual fact, if you tried this example, you wouldn't get _exactly_ that unless you knew what to fiddle with, because the compiler should normally optimise the code. In this case, the compiler would probably do something like:

Read more... )

Active Recent Entries