The Wayback Machine - https://web.archive.org/web/20241117084815/https://www.geeksforgeeks.org/convert-cc-code-to-assembly-language/
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
News 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
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);
}
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.



Similar Reads

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 elements of array, if A is smaller then store that
3 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 Algorithm - Load the BYTE1 in accumulator A.Copy the Data
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 registerInput the data from port ADisplay the result in port
2 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, and 3050 respectively. Algorithm - Input the conte
1 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.Then separate the LSB of the no. using ANI 0F inst
5 min read
Difference Between C Language and LISP Language
C Language: C is the procedural Programming language. It was designed to be compiled using a compiler. The Language has small and fixed number of keywords like if/else, for, while,.. etc. We can use more than one assignment that may be used in one statement in this language. Functions are also used here, it can return values that can be ignored, wh
2 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 into BL Logical shift right AL one time XOR BL with
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 // MACRO named MAX initialism to 10 #define MAX 10 int m
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 E) Algorithm: Load the given data in accumulator
2 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 in memory location 3000 and 39H in memory location
2 min read
8085 program to convert 8 bit BCD number into ASCII Code
Problem – Write an assembly-level language program to convert 8-bit BCD number to its respective ASCII Code. Assumptions - Starting address of program: 2000 Input memory location: 2050 Output memory location: 3050 and 3051 ASCII Code for Digits 0 – 9  Example -  Algorithm -   Input the content of 2050 in accumulator  Move content of Accumulator to
3 min read
isxdigit() function in C Language
isxdigit() function in C programming language checks that whether the given character is hexadecimal or not. isxdigit() function is defined in ctype.h header file. Hexadecimal equivalent of Decimal Numbers: Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Syntax: char isxdigit( char x); Examples: Input : A
2 min read
isupper() function in C Language
isupper() function in C programming checks whether the given character is upper case or not. isupper() function is defined in ctype.h header file. Syntax : int isupper ( int x ); Examples: Input: A Output: Entered character is uppercase character Input: a Output: Entered character is not uppercase character Input: 1 Output: Entered character is not
2 min read
isalnum() function in C Language
isalnum() function in C programming language checks whether the given character is alphanumeric or not. isalnum() function defined in ctype.h header file. Alphanumeric: A character that is either a letter or a number. Syntax: int isalnum(int x); Examples: Input : 1 Output : Entered character is alphanumeric Input : A Output : Entered character is a
2 min read
trunc() , truncf() , truncl() in C language
All three functions are used to remove digits after decimal point and return the modified decimal number. trunc() : Truncates a double value after the decimal point and gives the integer part as the result. The return value and the arguments are of the type double. Syntax : double trunc(double x); Parameters: x :It takes a double value as an input
4 min read
How to clear console in C language?
It is one of the basic need a program may required i.e clear the console during execution time. Need of a clear console screen : A console screen must be cleared once its stated objectives have been completed. A user also needs a clear screen for new data. Like any other language, C offers a variety of ways to clear the console screen. The clear sc
7 min read
Assigning multiple characters in an int in C language
Consider the following C program. #include <stdio.h> int main(void) { int a = 'd'; printf("%d\n", a); /*OUTPUT - 100 (ASCII Code for character d)*/ int b = 'dd'; printf("%d", b); /*OUTPUT - 25700 (Explanation in detail given below)*/ return 0; } Output : 100 25700 We can easily guess that the output for 'd' is 100 as 100 i
1 min read
Difference between Java and C language
Here are some of the differences between Java and C language. C is much faster than Java Java is slower than C due to overhead. C Java C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.C is a Procedural Programming Language.Java is Object-Oriented language.C is more procedure-oriented.Java is mor
3 min read
Similarities and Differences between Ruby and C language
Similarities between Ruby and C There are many similarities between C and Ruby, and some of them are: Like C, in Ruby also… A programmer is able to program procedurally if they like to do. But still, behind the scenes, it will be object-oriented.Both the languages have the same operators, for example, compound assignment and bitwise operators. But
3 min read
Introduction to the C99 Programming Language : Part I
Introduction: C99 is a standardized version of the C programming language that was published in 1999 by the International Organization for Standardization (ISO). It introduced a number of new features and improvements over the previous C89 standard, including support for variable-length arrays, flexible array members, complex numbers, and new keywo
8 min read
Introduction to the C99 Programming Language : Part II
In this article, we are going to discover some more interesting additions to C89 which gave us C99 standard: Variable Argument Lists: C99 brings out a small changes to the preprocessor. Macros can accept a variable number of arguments. These Macros are indicated by ellipsis (...) in their declaration. These Variable Arguments are represented by the
4 min read
Introduction to the C99 Programming Language : Part III
Kindly read Introduction to the C99 Programming Language (Part I) and Introduction to the C99 Programming Language (Part II) before reading this article. Addition of Library Functions: C99 provides some Additional Library functions listed below. Library Function Usage complex.h complex.h supports complex arithmetic fenv.h fenv.h gives access to flo
3 min read
Draw a smiley face using Graphics in C language
Prerequisite: graphics.h, How to include graphics.h in CodeBlocks? The task is to write a C program to draw a smiley face using graphics in C.To run the program we have the include the below header file: #include <graphic.h> Approach: We will create a Smiley Face with the help below functions: fillellipse(int x, int y, int x_radius, int y_rad
2 min read
Why Java Language is Slower Than CPP for Competitive Programming?
Choosing the appropriate language while starting competitive programming is the most important factor. Generally, we choose that language that has short syntax and executes very fast or which is familiar to us, and we know all the working patterns of that particular whether it is Java or C++. Most of the programmers use C++ for competitive programm
5 min read
Security issues in C language
C is a very powerful and popular programming language. It was first developed in the 1970s. C language is used in programming Network drivers, Interpreters, and Compilers, etc.Even though the C language is widely used in different systems still it has many security flaws associated with it. This article focuses on discussing security vulnerabilitie
12 min read
Shopping Cart Project Using C Language
Online Shopping applications are one of the favorite applications out of all online applications. It is a very much used application in everyone's life. So, Let's try to create a Shopping cart using C language which can perform basic tasks for us. The functionality of the Shopping Cart Although online Shopping has many functionalities we can't incl
11 min read
Language Evaluation Criteria
Language evaluation criteria in programming languages are sets of standards and principles used to assess the quality and effectiveness of programming languages. These criteria are used to evaluate the features, design, syntax, and semantics of programming languages in order to determine their suitability for various applications and contexts. Prog
7 min read
Test Cases For Signup Page Using C Language
Prerequisite: Structure in C In this article, we are going to check on how to check test cases for the signup page using C language. The signup page is where we enter data to create our account on any of the websites or applications. A test case is a document that contains various sets of data, conditions to be performed, and the expected result fo
6 min read
A C Programming Language Puzzle
Give a = 12 and b = 36 write a C function/macro that returns 3612 without using arithmetic, strings and predefined functions. We strongly recommend you to minimize your browser and try this yourself first. Below is one solution that uses String Token-Pasting Operator (##) of C macros. For example, the expression "a##b" prints concatenation of 'a' a
1 min read
Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
This is a question asked quite often as in which language should be preferred to be efficient in competitive programming. It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less same, but till now the most preferred language is C++, and here are the reasons. Python Simple and
5 min read
Practice Tags :
three90RightbarBannerImg