0% found this document useful (1 vote)
953 views

Convert C - C++ Code To Assembly Language

This document discusses how to convert C/C++ code to assembly language using the gcc compiler. It explains that using the "-S" option will cause gcc to run the compiler and generate an assembly file rather than object code. As an example, C code stored in a file called "geeks.c" is compiled to the assembly file "geeks.s". The generated assembly file contains machine instructions corresponding to the C code without local variable names or data types.

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
953 views

Convert C - C++ Code To Assembly Language

This document discusses how to convert C/C++ code to assembly language using the gcc compiler. It explains that using the "-S" option will cause gcc to run the compiler and generate an assembly file rather than object code. As an example, C code stored in a file called "geeks.c" is compiled to the assembly file "geeks.s". The generated assembly file contains machine instructions corresponding to the C code without local variable names or data types.

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Convert C/C++ code to assembly language

We use g++ compiler to turn provided C code into assembly language. To see the assembly code generated by the C compiler, we can use
the “-S” option on the command line:
Syntax:

$ gcc -S filename.c

This will cause gcc to run the compiler, generating an assembly le. Suppose we write a C code and store it in a le name “geeks.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);
}

Running the command:

$ gcc -S geeks.c

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

The assembly-code le contains various declarations including the set of lines:

    .section    __TEXT, __text, regular, pure_instructions


    .macosx_version_min 10, 12
    .globl    _main
    .align    4, 0x90
_main:                                  ## @main
    .cfi_startproc
## BB#0:
    pushq    %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    subq    $16, %rsp
    leaq    L_.str(%rip), %rdi
    leaq    _s(%rip), %rsi
    movl    $2000, -4(%rbp)         ## imm = 0x7D0
    movl    $17, -8(%rbp)
    movl    -4(%rbp), %eax
    addl    -8(%rbp), %eax
    movl    %eax, %edx
    movb    $0, %al
    callq    _printf
    xorl    %edx, %edx
    movl    %eax, -12(%rbp)         ## 4-byte Spill
    movl    %edx, %eax
    addq    $16, %rsp
    popq    %rbp
    retq
    .cfi_endproc
  
    .section    __DATA, __data
    .globl    _s                      ## @s
_s:
    .asciz    "GeeksforGeeks"
  
    .section    __TEXT, __cstring, cstring_literals
L_.str:                                 ## @.str
    .asciz    "%s %d \n"
  
  
.subsections_via_symbols

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy