next up previous
Next: About this document ... Up: Compilers Previous: Structure of a compiler

Additional components: preprocessors, assemblers, loaders/link-editors

Several other programs may be required to produce an EXECUTABLE target program.

We consider now an example. The above file addAndPrint.c is a C program. We compile it with the cc compiler using the -save-temps option which allows to save temporary files. In our example these files are

DIV ALIGN="CENTER">

[moreno@iguanodon tmp]$ more addAndPrint.c

#define ADD +
#define PRINT__floats__x__and__y printf("x = % d y = % d\n", x, y);

main()
{
  int x,y;
  x=20; y=3;
  x = x ADD y*10;
  PRINT__floats__x__and__y;
}

[moreno@iguanodon tmp]$ cc -save-temps addAndPrint.c 
[moreno@iguanodon tmp]$ ls
addAndPrint.c  addAndPrint.i  addAndPrint.o  addAndPrint.s  a.out
[moreno@iguanodon tmp]$ more addAndPrint.i

# 4 "addAndPrint.c"
main()
{
  int x,y;
  x=20; y=3;
  x = x + y*10;
  printf("x = % d y = % d\n", x, y);;
}

[moreno@iguanodon tmp]$ file addAndPrint.s 
addAndPrint.s: ASCII assembler program text
[moreno@iguanodon tmp]$ more addAndPrint.s

	.file	"addAndPrint.c"
	.version	"01.01"
gcc2_compiled.:
		.section	.rodata
.LC0:
	.string	"x = % d y = % d\n"
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	movl	$20, -4(%ebp)
	movl	$3, -8(%ebp)
	movl	-8(%ebp), %edx
	movl	%edx, %eax
	sall	$2, %eax
	addl	%edx, %eax
	leal	(%eax,%eax), %edx
	leal	-4(%ebp), %eax
	addl	%edx, (%eax)
	subl	$4, %esp
	pushl	-8(%ebp)
	pushl	-4(%ebp)
	pushl	$.LC0
	call	printf
	addl	$16, %esp
	leave
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.ident	"GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.3 2.96-110)"

[moreno@iguanodon tmp]$ file addAndPrint.o 
addAndPrint.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
[moreno@iguanodon tmp]$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped


next up previous
Next: About this document ... Up: Compilers Previous: Structure of a compiler
Marc Moreno Maza
2004-12-02