62Chapter3 • Compilers and Assemblers
3.3.3.1Creating Assembler Code
Using the –
S
(uppercase S) command line option, you can stop the GCC compiler just
before the assembler process. The output is an assembler file with a
.s
extension. The following
command creates an output file
hello.s
from source code file
hello.c
.
gcc –S hello.c
If you look at the output file, you can see the assembler code. Contents of the input file are
as follows:
#include
main()
{
printf ("Hello world\n");
}
The output assembler code is shown below:
[root@conformix chap-03]# cat hello.s
.file "hello.c"
.version "01.01"
gcc2_compiled.:
.section .rodata
.LC0:
.string "Hello world\n"
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.1
2.96-81)"
[root@conformix chap-03]#
This assembler code may be used with some assembler, like GNU as. Here
as
is not word
“as” but name of the GNU Assembler which is often written as
GNU as
as
, later on. It can also
be assembled and linked to create and execute. Please note that for the above command, the
compiler that is included in RedHat distribution was used.