Convert C/C++ Code To Assembly Language - GeeksforGeeks

Skip to content geeksforgeeks
  • Courses
    • DSA Courses
    • Programming Languages
  • Tutorials
    • Python Tutorial
      • Python Loops and Control Flow
    • Java
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
    • Algorithms
      • Analysis of Algorithms
      • Searching Algorithms
      • Sorting Algorithms
    • Practice
      • Company Wise Coding Practice
      • Practice Problems Difficulty Wise
      • Language Wise Coding Practice
      • Curated DSA Lists
    • Company Wise SDE Sheets
    • DSA Cheat Sheets
    • Puzzles
  • Data Science
    • Data Science Packages
    • Data Visualization
    • Data Analysis
  • Web Tech
    • Web Development Using Python
      • Django
      • Flask
    • Cheat Sheets
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App Convert C/C++ code to assembly language Last Updated : 01 Aug, 2024 Summarize Comments Improve Suggest changes Like Article Like Save Share Report Follow

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 intmain() { // Declaring variables inta=2000,b=17; // Printing statement cout<<a<<" + "<<b<<": "<<a+b; return0; } source.c // C code stored in geeks.c file #include<stdio.h> // global string chars[]="GeeksforGeeks"; // Driver Code intmain() { // Declaring variables inta=2000,b=17; // Printing statement printf("%s %d \n",s,a+b); } source-file-only

Directory Before Executing the Command

Running the command: gcc -S geeks.c

cmd-screen

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.)

source-and-assembly-code-generated

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.

Comment More info Next Article 8085 code to convert binary number to ASCII code

S

Sahil Rajput Improve

Similar Reads

  • Convert C/C++ code to assembly language 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 t 3 min read
  • 8085 code to convert binary number to ASCII code Problem - Assembly level program in 8085 which converts a binary number into ASCII number. Example - Assumptions - Binary number which have to convert in ASCII value is stored at memory location 2050 and output will be displayed at memory location 3050 and 3051. Algorithm - Load the content of 2050. 5 min read
  • 8085 program to convert ASCII code into HEX code Problem - Write an assembly level language program to convert ASCII code to its respective HEX Code. Examples: Input: DATA: 31H in memory location 2050Output:DATA: 0BH in memory location 3050 Assume that starting address of program, input memory location, and output memory locations are 2000, 2050, 1 min read
  • Convert C/C++ program to Preprocessor code We use g++ compiler to turn provided Cpp code into preprocessor code. To see the preprocessor code generated by the CPP compiler, we can use the “-E” option on the command line: Preprocessor include all the # directives in the code. and also expands MACRO function. Syntax: g++ -E filename.cpp // MAC 1 min read
  • 8086 program to convert 8 bit BCD number into ASCII Code Problem – Write an assembly language program in 8086 microprocessor to convert 8-bit BCD number to its respective ASCII Code. Assumption – Starting address of the program: 400 Input memory location: 2000 Output memory location: 3000 Example : Input: DATA: 98H in memory location 2000Output:DATA: 38H 2 min read
  • Assembly language program to find the range of bytes Problem - Write an assembly language program that if an input number BYTE1 lies b/w 50H to 80H display it on output PORT2. If BYTE1 is less than 50H then simply print 00H at the output PORT1. Examples: Input: 64H Output: output at PORT2 -->64H Input: 40H Output: output at PORT1 -->00H Algorith 2 min read
  • How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe 3 min read
  • C++ | asm declaration As we know C++ is a comprehensive and powerful programming language but there are few highly specialized situations that it cannot handle. For those situations, C++ provides an option using which one can drop an assembly code at any time. This option is the use of the 'asm' statement. Using asm stat 4 min read
  • 8086 program to convert binary to Grey code Prerequisite - Binary to/from Gray Code Problem - Write a program to convert Binary number to Grey code 8-bit number where starting address is 2000 and the number is stored at 2500 memory address and store result into 2600 memory address. Example – Algorithm – Move value at [2500] into AL Move AL in 1 min read
  • 8085 program to convert a hexadecimal number into ASCII code Problem: Write an assembly-level language program to convert the HEX code to its respective ASCII code. Assume that the starting address of the program and input memory location are 2000 and 2050 respectively. Example: Input: 2050 E4 (Hex data)Output:2051 34 (ASCII code for 4)2052 45 (ASCII code for 2 min read
  • 8086 program to convert 8 bit ASCII to BCD number Problem - Write a program to convert ASCII to BCD 8-bit number where the starting address is 2000 and the number is stored at 2050 memory address and store result in 3050 memory address. Example-Input : location: 2050 Data : 37Output : location: 3050 Data : 07 Algorithm – Move value at [2050] into A 1 min read
  • VS Code | Compile and Run in C++ In this article, we will learn how to compile and run C++ program in VS Code. There are two ways of doing that you can use any one of them as per your convenience. It is to be noted that a majority of competitive programmers use C++, therefore the compilation and execution of the program needs to be 3 min read
  • Assembly language program to find largest number in an array Problem - Determine largest number in an array of n elements. Value of n is stored at address 2050 and array starts from address 2051. Result is stored at address 3050. Starting address of program is taken as 2000. Example: Algorithm: We are taking first element of array in AComparing A with other e 3 min read
  • 8085 program to convert a BCD number to binary Problem – Write an assembly language program for converting a 2 digit BCD number to its binary equivalent using 8085 microprocessor. Examples: Input : 72H (0111 0010)2Output : 48H (in hexadecimal) (0011 0000)2((4x16)+(8x1))=72Algorithm: Load the BCD number in the accumulatorUnpack the 2 digit BCD nu 2 min read
  • Assembly program to transfer the status of switches Problem - Write an assembly language program in 8085 of interfacing between 8085 and 8255. 8 switches are connected at port A. Transfer the status of these switches into port B where LEDs are connected. Example - Input port is A and output port is B. Algorithm - Construct the control word registerIn 2 min read
  • 8085 program to convert gray to binary Problem – Write an assembly language program in 8085 microprocessor to convert gray numbers to binary. Example - Algorithm – Load the data from address 2050 in AMove the data 07 in CMove the data of A to BExtract the MSB (Most Significant Bit) of data available in ARotate the bits of A to rightTake 3 min read
  • Convert std::string to LPCWSTR in C++ C++ provides us a facility using which one can represent a sequence of characters as an object of a class. This class is std::string. Internally, std::string class represents a string as a sequence of characters (bytes) and each character can be accessed using the [] operator. The difference between 2 min read
  • How to Create a Dynamic Library in C++? In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the 4 min read
  • Build a C++ Program that Have Multiple Source Code Files In C++ generally, we saw only a single file program but what if we have multiple source code files and we have to build a program by using all these files? Let's see how we can build a C++ program with multiple source files without including them as header files. Use two or more source code files in 3 min read
Article Tags :
  • C Language
  • C++
  • system-programming
Practice Tags :
  • CPP
Like three90RightbarBannerImg Explore More We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Lightbox Improvement Suggest changes Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. geeksforgeeks-suggest-icon Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. geeksforgeeks-improvement-icon Suggest Changes min 4 words, max CharLimit:2000

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences

Từ khóa » Chuyển Code C Sang Assembly