Let’s try to answer in order!
-
The data section contains anything that you want to be automatically initialized for you by the system before it calls the entry point of your program. You’re right, normally global variables end up here. Zero-initialized data is generally not included in the executable file, since there’s no reason to – a couple of directives to the program loader are all that’s needed to generate that space. Once your program starts running, the ZI and data regions are generally interchangeable. Wikipedia has a lot more information.
-
Variables don’t really exist when assembly programming, at least not in the sense they do when you’re writing C code. All you have is the decisions you’ve made about how to lay out your memory. Variables can be on the stack, somewhere in memory, or just live only in registers.
-
Registers are the internal data storage of the processor. You can, in general, only do operations on values in processor registers. You can load and store their contents to and from memory, which is the basic operation of how your computer works. Here’s a quick example. This C code:
int a = 5; int b = 6; int *d = (int *)0x12345678; // assume 0x12345678 is a valid memory pointer *d = a + b;
Might get translated to some (simplified) assembly along the lines of:
load r1, 5 load r2, 6 load r4, 0x1234568 add r3, r1, r2 store r4, r3
In this case, you can think of the registers as variables, but in general it’s not necessary that any one variable always stay in the same register; depending on how complicated your routine is, it may not even be possible. You’ll need to push some data onto the stack, pop other data off, and so on. A ‘variable’ is that logical piece of data, not where it lives in memory or registers, etc.
-
An array is just a contiguous block of memory – for a local array, you can just decrement the stack pointer appropriately. For a global array, you can declare that block in the data section.
-
There are a bunch of conventions about registers – check your platform’s ABI or calling convention document for details about how to use them correctly. Your assembler documentation might have information as well. Check the ABI article on wikipedia.
-
Your assembly program can make the same system calls any C program could, so you can just call
malloc()
to get memory from the heap.