Convert C/C++ code to assembly language
Last Updated :
01 Aug, 2024
The C/C++ compiler turn the given C/C++ source code into the Assembly Code. Then it is converted to the object code. We can use this property to convert the C or C++ code to assembly code.
We use gcc compiler to turn provided C/C++ code into assembly language. To see the assembly code generated by the C/C++ compiler, we can use the “-S” option on the command line:
Syntax for C
gcc -S filename.c
Syntax for C++
g++ -S filename.cpp
This will cause gcc to run the compiler, generating an assembly file.
Example
Suppose we write a C code and store it in a file name “source.c” or “source.cpp”.
source.cpp
// C++ code stored in source2.cpp file
#include <iostream>
// Driver Code
int main()
{
// Declaring variables
int a = 2000, b =17;
// Printing statement
cout << a << " + " << b << ": " << a + b;
return 0;
}
source.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);
}

Directory Before Executing the Command
Running the command: gcc -S geeks.c

This will cause gcc to run the compiler, generating an assembly file source.s, and go no further. (Normally it would then invoke the assembler to generate an object- code file.)

Assembly code Fine Genereated
The assembly-code file contains various declarations including the set of lines:
Assembly
.file "source.c"
.text
.def printf; .scl 3; .type 32; .endef
.seh_proc printf
printf:
pushq %rbp
.seh_pushreg %rbp
pushq %rbx
.seh_pushreg %rbx
subq $56, %rsp
.seh_stackalloc 56
leaq 48(%rsp), %rbp
.seh_setframe %rbp, 48
.seh_endprologue
movq %rcx, 32(%rbp)
movq %rdx, 40(%rbp)
movq %r8, 48(%rbp)
movq %r9, 56(%rbp)
leaq 40(%rbp), %rax
movq %rax, -16(%rbp)
movq -16(%rbp), %rbx
movl $1, %ecx
movq __imp___acrt_iob_func(%rip), %rax
call *%rax
movq %rbx, %r8
movq 32(%rbp), %rdx
movq %rax, %rcx
call __mingw_vfprintf
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
addq $56, %rsp
popq %rbx
popq %rbp
ret
.seh_endproc
.globl s
.data
.align 8
s:
.ascii "GeeksforGeeks\0"
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "%s %d \12\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $48, %rsp
.seh_stackalloc 48
.seh_endprologue
call __main
movl $2000, -4(%rbp)
movl $17, -8(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
addl %edx, %eax
movl %eax, %r8d
leaq s(%rip), %rdx
leaq .LC0(%rip), %rcx
call printf
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (tdm64-1) 10.3.0"
.def __mingw_vfprintf; .scl 2; .type 32; .endef
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.
Same thing we can do with C++. We just have to use the g++ compiler.