I've compiled the following C function to see non-local variables are handled in asm:
int a=1;
int b=2;
int main() {
int c = 3;
return a+b+c;
}
Compiling it with $ gcc file.c -S
gives me:
.file "isec.c"
.globl a
.data
.align 4
.type a, @object
.size a, 4
a:
.long 1
.globl b
.align 4
.type b, @object
.size b, 4
b:
.long 2
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
I'm a bit confused about the ordering of the items above LFB0
. For example, why isn't it just:
a: .long 1
b: .long 2
What is everything else for? And, if it is going to list a bunch of globals, why doesn't it do something like:
.globl a
.globl b
.globl main
I guess I'm confused about the ordering and why the top sections are organized like they are.
I am incredibly new to asm but I guess how I would think it would be compiled would be along the lines of:
.globl a
.globl b
.globl main
a: .long 1
b: .long 2
main:
mov a(%rip), %eax
add b(%rip), %eax
add $3, %eax
ret
Additionally, is it required to make a
and b
global? (What's the point of doing global? All I can tell is it's required for the main function).