Assembly

The assembler then converts the assembly code to object code. The object file contains machine code. Also the object file contains re-location information for the undefined symbols. A typical C program contains reference to symbols such as printf, scanf which are defined in some other libraries. The address offset of these symbols in memory cannot be determined at this stage, therefore incomplete references to these symbols are made in the object code. The references to these symbols are replaced with their actual addresses by the linker in the next phase. The object code for for an assembly program can be obtained by using as in the following way:

$ as -o area.s

This would generate an object file area.o. The object files have .o extension.

The object code can be disassembled back into assembly code and what changes the assembler has done can be seen using the objdump command as follows:

$ objdump -d area.o

.

.

.

Disassembly of section .text:

00000000 <main>:

0: 55 push %ebp

1: 89 e5 mov %esp,%ebp

3: 83 ec 08 sub $0x8,%esp

6: c7 45 fc 03 00 00 00 movl $0x3,0xfffffffc(%ebp)

d: 83 ec 04 sub $0x4,%esp

10: 8b 45 fc mov 0xfffffffc(%ebp),%eax

13: 0f af 45 fc imul 0xfffffffc(%ebp),%eax

17: 50 push %eax

18: ff 75 fc pushl 0xfffffffc(%ebp)

1b: 68 00 00 00 00 push $0x0

20: e8 fc ff ff ff call 21 <main+0x21>

25: 83 c4 10 add $0x10,%esp

28: b8 00 00 00 00 mov $0x0,%eax

2d: c9 leave

2e: c3 ret

2f: 90 nop

Here the call to the function printf (call printf in the assembly file) has been replaced with a relative address (call 21 < main+0x21 >) in the object file. This relative address would then be replaced by the actual address by the linker in the later phase.