12.3 – Generating assembly code

by subbu on December 16, 2013

Generating assembly code

How to generate assembly code in Windows using gcc

Now we will see how we can generate assembly source using an option -S (capital) using MinGW gcc compiler in windows command prompt

1. Open the text editor to write the program

>edit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

#include<stdio.h>
int main()
{
 int a,b,c;
 a=45;
 b=25;
 c=a+b;
 printf("Sum %d",c);
 return 0;
}

3. Compile the program using “gcc”.

Note:
-S (capital) is a compiler option to generate assembly source as a file with .s extension
-o is a compiler option to generate assembly file with specified file name and extension Prog.asm otherwise Prog.s is created by default
-O (capital) is a compiler option to generate optimized code. There would be redundancy (repetition) in assembly code generated by gcc without optimization. O1,O2,O3,Og and Ofast are different options to select different levels of optimization

>gcc -S -o Prog.asm Prog.c

4. Now we can see the assembly source without optimization using text editor

>edit Prog.asm
_main:
pushl   %ebp
movl   %esp, %ebp
andl    $-16, %esp
subl    $32, %esp
call     ___main
movl   $45, 28(%esp)
movl   $25, 24(%esp)
movl   24(%esp), %eax
movl   28(%esp), %edx
addl    %edx, %eax
movl   %eax, 20(%esp)
movl   20(%esp), %eax
movl   %eax, 4(%esp)
movl   $LC0, (%esp)
call     _printf
movl   $0, %eax
leave
ret

There is some repetition of code in the assembly code as we have not given any optimization option to gcc.
Now we will see how optimization effects to the assembly code

>gcc -S -O -o Prog.asm Prog.c
_main:
pushl   %ebp
movl   %esp, %ebp
andl    $-16, %esp
subl    $16, %esp
call     ___main
movl   $70, 4(%esp)
movl   $LC0, (%esp)
call     _printf
movl   $0, %eax
leave
ret

building assembly code from C code

How to generate assembly code in Linux using gcc

Now we will see how we can generate assembly source using an option -S (capital) using gcc compiler in Linux

1. Open the text editor to write the program

$gedit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, q) from the editor

#include<stdio.h>
int main()
{
 int a,b,c;
 a=45;
 b=25;
 c=a+b;
 printf("Sum %d",c);
 return 0;
}

3. Compile the program using “gcc”.

Note:
-S (capital) is a compiler option to generate assembly source as a file with .s extension
-o is a compiler option to generate assembly file with specified file name Prog.asm otherwise Prog.s is created by default
-O (capital) is a compiler option to generate optimized code. There would be redundancy (repetition) in assembly code generated by gcc without optimization. O1,O2,O3,Og and Ofast are different options to select different levels of optimization

$gcc -S -o Prog.asm Prog.c

4. Now we can see the assembly source without optimization using text editor

$gedit Prog.asm
_main:
pushl   %ebp
movl   %esp, %ebp
andl    $-16, %esp
subl    $32, %esp
call     ___main
movl   $45, 28(%esp)
movl   $25, 24(%esp)
movl   24(%esp), %eax
movl   28(%esp), %edx
addl    %edx, %eax
movl   %eax, 20(%esp)
movl   20(%esp), %eax
movl   %eax, 4(%esp)
movl   $LC0, (%esp)
call     _printf
movl   $0, %eax
leave
ret

There is some repetition of code in the assembly code as we have not given any optimization option to gcc.
Now we will see how optimization effects to the assembly code

$gcc -S -O -o Prog.asm Prog.c
$gedit Prog.asm
_main:
pushl   %ebp
movl   %esp, %ebp
andl    $-16, %esp
subl    $16, %esp
call     ___main
movl   $70, 4(%esp)
movl   $LC0, (%esp)
call     _printf
movl   $0, %eax
leave
ret

generating Assembly code from C code

Previous post:

Next post: