'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Dec 28, 2023
Easy

Convert C/C++ Code to Assembly Language

Author Aayush Sharma
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

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;
}

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!

Previous article
Difference between float and double in C/C++
Next article
Managing Docker Swarm
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass