Anatomy of Assembly
Example code:
.globl _start # directive, don't delete the following symbol
.section .text # directive, this section is text (code)
_start: # label the linker will pickup and place in the ELF header as the start address
movq $60, %rax
movq $3, %rdi
syscallTerse explainations detailed breakdown below.
Detailed Breakdown
.globl _startDefinition
directive: Anything that starts with a dot
.is a directive it doesn’t generate code, it just tells the assembler to do something.
Symbols like _start are thrown away by the assembler, the .globl (.global works too) directive marks it as global so it shouldn’t be discarded. Why do we do this? Because the linker needs _start to record the entry point address in the ELF header.
.section .text.section directive tells the assembler that the next part of the listing should be placed in the code section of the program, historically known as the text you can think of this as what the process will “read” thuse being the text of the program.
In assembly language, you can freely switch between .data and .text sections, and the assembler and linker will group all of the data and code sections together in the final executable.
_start:Definition
symbol: is a name bound to a value. A label creates a symbol whose value is an address.
_start: is a label, labels are examples of symbols.
_start is special to linker it will use this as the start location of the first text (code) that gets run. The linker writes the address into the ELF header’s entry field; the kernel reads that field at exec time and jumps there. _start the name is gone by then, only the address survives.