Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,394,106

how to convert c/c++ into assembly then compile using gcc

Tutorial Name: how to convert c/c++ into assembly then compile using gcc  

Category: PC Tutorials

Submitted By: Ella

Date Added:

Comments: 0

Views: 5,597

Related Forum: PC Building Forum

Share:

alright, don't really know what the use of doing this would be maybe if you were writing an OS or just felt like looking at c/c++ code in assembly. also this could be used as a way to encrypt your programs maybe?
well anyways here it is.

first make a hello world application in either c/c++
Hello World - C

#include <stdio.h>
int main(void){
printf("Hello World\n");
}


Hello World - C++

#include <iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
}


you can just copy/paste it into gedit or whatever text editor your using.
and then save as whatever.cpp or whatever.c

next your going to open up terminal

alt+t or in my case just click on it.
and your going to go to the directory in which your c/cpp file is in.

e.g. Desktop
blah blah blah.

then your going to type

g++ -S whatever.cpp
or
gcc -S whatever.c


either or you will wind up with a file named whatever.s
this is your converted code from c/cpp to assembly.
and to compile and run simply use either gcc or g++ and compile as you normally would a c/cpp program


gcc -o whatever whatever.s
or
g++ -o whatever whatever.s
then
./whatever


I must stress to get the source code in assembly you must type a capital "S" when you convert.
if it is lower case it will just automatically give you an a.out and won't give you whatever.s

hope you enjoyed
here is what this hello world program looks like in assembly


   .file   "dragon.cpp"
   .local   _ZStL8__ioinit
   .comm   _ZStL8__ioinit,1,1
   .section   .rodata
.LC0:
   .string   "Hello World"
   .text
.globl main
   .type   main, @function
main:
.LFB963:
   .cfi_startproc
   pushl   %ebp
   .cfi_def_cfa_offset 8
   movl   %esp, %ebp
   .cfi_offset 5, -8
   .cfi_def_cfa_register 5
   andl   $-16, %esp
   subl   $16, %esp
   movl   $.LC0, 4(%esp)
   movl   $_ZSt4cout, (%esp)
   call   _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
   movl   $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
   movl   %eax, (%esp)
   call   _ZNSolsEPFRSoS_E
   movl   $0, %eax
   leave
   .cfi_restore 5
   .cfi_def_cfa 4, 4
   ret
   .cfi_endproc
.LFE963:
   .size   main, .-main
   .type   _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB972:
   .cfi_startproc
   pushl   %ebp
   .cfi_def_cfa_offset 8
   movl   %esp, %ebp
   .cfi_offset 5, -8
   .cfi_def_cfa_register 5
   subl   $24, %esp
   cmpl   $1, 8(%ebp)
   jne   .L2
   cmpl   $65535, 12(%ebp)
   jne   .L2
   movl   $_ZStL8__ioinit, (%esp)
   call   _ZNSt8ios_base4InitC1Ev
   movl   $_ZNSt8ios_base4InitD1Ev, %eax
   movl   $__dso_handle, 8(%esp)
   movl   $_ZStL8__ioinit, 4(%esp)
   movl   %eax, (%esp)
   call   __cxa_atexit
.L2:
   leave
   .cfi_restore 5
   .cfi_def_cfa 4, 4
   ret
   .cfi_endproc
.LFE972:
   .size   _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
   .type   _GLOBAL__I_main, @function
_GLOBAL__I_main:
.LFB973:
   .cfi_startproc
   pushl   %ebp
   .cfi_def_cfa_offset 8
   movl   %esp, %ebp
   .cfi_offset 5, -8
   .cfi_def_cfa_register 5
   subl   $24, %esp
   movl   $65535, 4(%esp)
   movl   $1, (%esp)
   call   _Z41__static_initialization_and_destruction_0ii
   leave
   .cfi_restore 5
   .cfi_def_cfa 4, 4
   ret
   .cfi_endproc
.LFE973:
   .size   _GLOBAL__I_main, .-_GLOBAL__I_main
   .section   .ctors,"aw",@progbits
   .align 4
   .long   _GLOBAL__I_main
   .weakref   _ZL20__gthrw_pthread_oncePiPFvvE,pthread_once
   .weakref   _ZL27__gthrw_pthread_getspecificj,pthread_getspecific
   .weakref   _ZL27__gthrw_pthread_setspecificjPKv,pthread_setspecific
   .weakref   _ZL22__gthrw_pthread_createPmPK14pthread_attr_tPFPvS3_ES3_,pthread_create
   .weakref   _ZL20__gthrw_pthread_joinmPPv,pthread_join
   .weakref   _ZL21__gthrw_pthread_equalmm,pthread_equal
   .weakref   _ZL20__gthrw_pthread_selfv,pthread_self
   .weakref   _ZL22__gthrw_pthread_detachm,pthread_detach
   .weakref   _ZL22__gthrw_pthread_cancelm,pthread_cancel
   .weakref   _ZL19__gthrw_sched_yieldv,sched_yield
   .weakref   _ZL26__gthrw_pthread_mutex_lockP15pthread_mutex_t,pthread_mutex_lock
   .weakref   _ZL29__gthrw_pthread_mutex_trylockP15pthread_mutex_t,pthread_mutex_trylock
   .weakref   _ZL31__gthrw_pthread_mutex_timedlockP15pthread_mutex_tPK8timespec,pthread_mutex_timedlock
   .weakref   _ZL28__gthrw_pthread_mutex_unlockP15pthread_mutex_t,pthread_mutex_unlock
   .weakref   _ZL26__gthrw_pthread_mutex_initP15pthread_mutex_tPK19pthread_mutexattr_t,pthread_mutex_init
   .weakref   _ZL29__gthrw_pthread_mutex_destroyP15pthread_mutex_t,pthread_mutex_destroy
   .weakref   _ZL30__gthrw_pthread_cond_broadcastP14pthread_cond_t,pthread_cond_broadcast
   .weakref   _ZL27__gthrw_pthread_cond_signalP14pthread_cond_t,pthread_cond_signal
   .weakref   _ZL25__gthrw_pthread_cond_waitP14pthread_cond_tP15pthread_mutex_t,pthread_cond_wait
   .weakref   _ZL30__gthrw_pthread_cond_timedwaitP14pthread_cond_tP15pthread_mutex_tPK8timespec,pthread_cond_
timedwait
   .weakref   _ZL28__gthrw_pthread_cond_destroyP14pthread_cond_t,pthread_cond_destroy
   .weakref   _ZL26__gthrw_pthread_key_createPjPFvPvE,pthread_key_create
   .weakref   _ZL26__gthrw_pthread_key_deletej,pthread_key_delete
   .weakref   _ZL30__gthrw_pthread_mutexattr_initP19pthread_mutexattr_t,pthread_mutexattr_init
   .weakref   _ZL33__gthrw_pthread_mutexattr_settypeP19pthread_mutexattr_ti,pthread_mutexattr_settype
   .weakref   _ZL33__gthrw_pthread_mutexattr_destroyP19pthread_mutexattr_t,pthread_mutexattr_destroy
   .ident   "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
   .section   .note.GNU-stack,"",@progbits


hope you enjoyed this tutorial.

Ratings

Current rating: 1.91 by 11 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"how to convert c/c++ into assembly then compile using gcc" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.