Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

- An assembler convert assembly language code to object file

- GCC can create object file from either .c or .s code


```
gcc -c prog.s -o prog.o # From assembly
gcc -O1 -g -c prog.c -o prog.o # From C
```
- `-g` says to include debug symbols
- Assembler dose 2 passes over the assembly code
- During 1st pass, its assigns address to all the instructions and finds all the symbols and
label, keeping them in a symbol table.
- On 2nd pass, assembler produces machine code, Address for label and taken from symbol
tables
- The machine language code and symbol table is stored in object file
- We can disassemble object file using `objdump` command to see assembly code beside machine
code
```
objdump -S prog.o
```
* If the code was compiled with `-g` option (debug symbols enabled), the dump will have original C
code interspersed between assembly code
* We can also get symbol table, as
```
objdump -t prog.o
```
- In above dumps, as the program is not yet linked, some address would be invalid and would be
updated once object code go through linker.
### Linking
- Linker combines all the object file and startup code (used to setup stack/heap/etc) into one
machine language executable and assign address to global variables.
- The linker relocates the data and instructions in object files so that they are not all on top of each
other.
- Invoke GCC to link object file -
```
gcc prog.o -o prog
```
- We can disassemble the executable with -
```
objdump -S -t prog
```
### Loading
- The OS loads a program by reading the text segment of the executable file from a storage device
into the text segment of memory. The operating system jumps to the beginning of the program to
begin executing.

You might also like