We use g++ compiler to turn provided C code into assembly language. To see the assembly code generated by the C compiler, we can use the “-S” option on the command line:
Syntax:
$ gcc -S filename.c
This will cause gcc to run the compiler, generating an assembly file. Suppose we write a C code and store it in a file name “geeks.c” .
// C code stored in geeks.c file #include <stdio.h> // global string char s[] = "GeeksforGeeks"; // Driver Code int main() { // Declaring variables int a = 2000, b =17; // Printing statement printf("%s %d \n", s, a+b); } |
Running the command:
$ gcc -S geeks.c
This will cause gcc to run the compiler, generating an assembly file geeks.s, and go no further. (Normally it would then invoke the assembler to generate an object- code file.)
The assembly-code file contains various declarations including the set of lines:
.section __TEXT, __text, regular, pure_instructions .macosx_version_min 10, 12 .globl _main .align 4, 0x90 _main: ## @main .cfi_startproc ## BB#0: pushq %rbp Ltmp0: .cfi_def_cfa_offset 16 Ltmp1: .cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp2: .cfi_def_cfa_register %rbp subq $16, %rsp leaq L_.str(%rip), %rdi leaq _s(%rip), %rsi movl $2000, -4(%rbp) ## imm = 0x7D0 movl $17, -8(%rbp) movl -4(%rbp), %eax addl -8(%rbp), %eax movl %eax, %edx movb $0, %al callq _printf xorl %edx, %edx movl %eax, -12(%rbp) ## 4-byte Spill movl %edx, %eax addq $16, %rsp popq %rbp retq .cfi_endproc .section __DATA, __data .globl _s ## @s _s: .asciz "GeeksforGeeks" .section __TEXT, __cstring, cstring_literals L_.str: ## @.str .asciz "%s %d \n" .subsections_via_symbols |
Each indented line in the above code corresponds to a single machine instruction. For example, the pushq instruction indicates that the contents of register %rbp should be pushed onto the program stack. All information about local variable names or data types has been stripped away. We still see a reference to the global
variable s[]= “GeeksforGeeks”, since the compiler has not yet determined where in memory this variable will be stored.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Assembly language program to find largest number in an array
- Assembly language program to find the range of bytes
- Assembly program to transfer the status of switches
- 8085 code to convert binary number to ASCII code
- 8085 program to convert ASCII code into HEX code
- Difference Between C Language and LISP Language
- 8085 program to convert 8 bit BCD number into ASCII Code
- 8086 program to convert binary to Grey code
- 8086 program to convert 8 bit BCD number into ASCII Code
- Convert C/C++ program to Preprocessor code
- 8085 program to convert a hexadecimal number into ASCII code
- C Programming Language Standard
- A C Programming Language Puzzle
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- C Language Introduction
- Arrays in C Language | Set 2 (Properties)
- Why C++ is partially Object Oriented Language?
- Signals in C language
- Constants vs Variables in C language
- Difference between %d and %i format specifier in C language

