Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Assembly Language
3.
Why do we Need to Convert C/C++ Code to Assembly Language?
4.
Steps to Convert C/C++ Code to Assembly Language
4.1.
Move to the File Location
4.2.
Type the g++ Command 
4.3.
Conversion of C++ Code to Assembly
4.3.1.
C++ Code
4.3.2.
Assembly Code
4.4.
Conversion of C Code to Assembly
4.4.1.
C Code
4.4.2.
Assembly Code
5.
Frequently Asked Questions
5.1.
How is Assembly language better than other high-level languages in optimizations?
5.2.
What are some other alternatives to convert C/C++ code to assembly language?
5.3.
Can we convert our Assembly code into C/C++ code?
5.4.
What are the disadvantages of using Assembly language?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Convert C/C++ Code to Assembly Language

Author Rinki Deka
0 upvote

Introduction

Assembly Language is a low-level programming language directly controlling the computer's hardware. Assembly language bridges high-level languages like C++, Java, Python, etc., and machine code understood by the computer's CPU. But sometimes, we need to convert our C/C++ code into Assembly Code.

Convert C/C++ code to assembly language

This blog will discuss the steps to convert C/C++ code to Assembly language. We will also discuss some code examples along with their outputs. But first, let us discuss a bit more about Assembly language in the next section.

Assembly Language

Assembly language is a low-level programming language that acts as a bridge between the high-level languages written by the programmers and the machine language understood by the computer.

Assembly language uses shortcodes to represent the machine instructions in the code. Each of these instructions is mapped to a specific function performed by the CPU. Some of the most common instructions are discussed below in brief.
 

  • MOV - This instruction moves data from one location to another.

 

  • LOAD/STORE - These instructions load data from the memory to a register and vice-versa.

 

  • ADD - This instruction is used to perform arithmetic addition operations.

 

  • SUB - This instruction is used to perform arithmetic subtraction operations.

 

  • JMP - This instruction is used to jump to a specified address in the program.

 

  • CMP - This instruction compares two values and updates the flag register based on the result.

 

  • AND/OR/XOR - These instructions perform the respective bitwise operation in Assembly.
     

Although there are many more commands in Assembly, these are some of the most commonly used instructions. The following section will discuss why we must convert C/C++ code to assembly language.

Also see, Ensemble Learning

Why do we Need to Convert C/C++ Code to Assembly Language?

Now that we know about Assembly language, we will discuss when to convert C/C++ code to assembly language. Some prominent reasons we need to convert our C/C++ code to assembly language are listed below.

Significance of Converting C/C++ Code to Assembly Language
  • Low-Level Operations - In some scenarios, we need to write the programs or convert programs in high-level languages to low-level languages due to software limitations.
     
  • Better Performance - As we know, low-level languages are much more efficient than high-level languages like C/C++. Thus we can optimize our code by converting it into low-level Assembly language.
     
  • Easier Conversion - One of the main downsides of Assembly language is that writing programs could be more convenient. An alternative way to write assembly programs is to write them in high-level languages like C/C++ and then convert them into assembly language.
     
  • Cross-Platform Development - By converting our C/C++ code to assembly language code, we can efficiently run our code on devices like the 8051 family of microprocessors, which can only interpret assembly language.
     
  • Operating System Development - Operating system development often needs Assembly language to write codes for essential tasks like context switchinginterrupts, etc.


These were some significant scenarios where we needed to convert C/C++ code to assembly language. In the next section, we will discuss the steps involved in transforming our C/C++ code into assembly language.

Steps to Convert C/C++ Code to Assembly Language

The steps involved in converting our C/C++ code to Assembly language are pretty simple. The G++ compiler provides a command in the console to convert the C/C++ file code into a new assembly file.

Move to the File Location

Firstly you will need to move to the folder in which you have the C/C++ file you want to convert into the assembly code.

console output

Type the g++ Command 

The g++ compiler allows us to convert the C/C++ code to an assembly file. 

gcc -S filename

When we run the command, a new assembly file with the same name is created in the exact location.

file location

Conversion of C++ Code to Assembly

The original code in C++ and the program in assembly after conversion is shown below.

C++ Code

#include <iostream>
using namespace std;

int main() {

	// Declare the variables
	int divisor, dividend, quotient, remainder;

	divisor =4, dividend = 13;

	// Calculate the remainder and quotient
	quotient = dividend / divisor;
	remainder = dividend % divisor;

	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Assembly Code

	.file	"cpp_code.cpp"
	.text
	.local	_ZStL8__ioinit
	.comm	_ZStL8__ioinit,1,1
	.globl	main
	.type	main, @function
main:
.LFB1761:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$65, -12(%rbp)
	movl	$22, -8(%rbp)
	movl	-12(%rbp), %eax
	subl	-8(%rbp), %eax
	movl	%eax, -4(%rbp)
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1761:
	.size	main, .-main
	.type	_Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB2285:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	cmpl	$1, -4(%rbp)
	jne	.L5
	cmpl	$65535, -8(%rbp)
	jne	.L5
	leaq	_ZStL8__ioinit(%rip), %rax
	movq	%rax, %rdi
	call	_ZNSt8ios_base4InitC1Ev@PLT
	leaq	__dso_handle(%rip), %rax
	movq	%rax, %rdx
	leaq	_ZStL8__ioinit(%rip), %rax
	movq	%rax, %rsi
	movq	_ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
	movq	%rax, %rdi
	call	__cxa_atexit@PLT
.L5:
	nop
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2285:
	.size	_Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
	.type	_GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB2286:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$65535, %esi
	movl	$1, %edi
	call	_Z41__static_initialization_and_destruction_0ii
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2286:
	.size	_GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
	.section	.init_array,"aw"
	.align 8
	.quad	_GLOBAL__sub_I_main
	.hidden	__dso_handle
	.ident	"GCC: (GNU) 12.2.0"
	.section	.note.GNU-stack,"",@progbits

Conversion of C Code to Assembly

The original code in C to and the program in assembly after conversion is shown below.

C Code

#include <stdio.h>

int main() {

	// Declare the variables
	int number1, number2, sum;

	number1=10, number2=13;

	// Calculate the sum
	sum = number1 + number2;     
	return 0;
}
You can also try this code with Online C Compiler
Run Code

Assembly Code

	.file	"c_code.c"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$10, -12(%rbp)
	movl	$13, -8(%rbp)
	movl	-12(%rbp), %edx
	movl	-8(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 12.2.0"
	.section	.note.GNU-stack,"",@progbits

Frequently Asked Questions

How is Assembly language better than other high-level languages in optimizations?

Assembly language gives the users more specific control over the computer's hardware resources. Hence the assembly language can perform optimizations which are not possible in other high level programming languages.

What are some other alternatives to convert C/C++ code to assembly language?

There are some tools other than the gcc compiler to convert our C/C++ code to assembly language. Some of them are the Low Level Virtual Machine (LLVM)Intel Compiler (ICC), and disassemblers like IDA PRO.

Can we convert our Assembly code into C/C++ code?

Yes, it is possible to convert our assembly code to C/C++ code. This process is known as decompilation. However, this process is not as easy as converting C/C++ code to assembly code, resulting in unreadable code.

What are the disadvantages of using Assembly language?

Although Assembly language is faster than C/C++, it also has some disadvantages. Assembly language is generally difficult to write and debug programs. The assembly code is platform dependent and requires additional knowledge about the hardware on which it is running.

Conclusion

In this article, we discussed the steps to convert C/C++ code to assembly language. We discussed in brief about Assembly language and the need to convert our high-level code in C/C++ to low-level machine code in assembly language. In the end, we concluded by discussing some C/C++ programs and their converted output in Assembly language.

So now that you have learned how to C/C++ code to assembly language, you can refer to similar articles.
 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSA, Competitive Programming, System Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning!

Live masterclass