In this blog we will learn how to convert c program to assembly language. To convert a c or c++ program to assembly language is a simple task. We will use gcc compiler to convert c program to assembly language on Linux based operating system like Ubuntu. Let see step by step instructions.

how to convert c program to assembly language ( Step by step )

Step 1) Open vim editor in Linux terminal

We can use any text editor but try to practice with vim Linux editor tool.

bosch@bosch-Inspiron-N5050:~$ vi sample.c

Step 2) Write C or c++ code in vim editor

how to convert c program to assembly language

#include<stdio.h>

void Hello() {
  printf("Hello world\n");
}

int main()
{
  Hello();
  return 0;
}

Step 3) Save your c program on current working directory

When vi or vim editor opens it is in command mode. To change from command mode to insert mode press i button from keyboard. After writing your code you can save the code we have to press following sequence of button press.

  • Press Esc button. It will change the vi or vim editor from insert mode to command mode.
  • Press Shif + : ( it will open vim internal command prompt )
  • Type wq at vim command (:) and hit Enter button. it will save the code to the open file.

Step 4) Convert c program to assembly by following command

bosch@bosch-Inspiron-N5050:~$ gcc -S sample.c

After running command “gcc -S sample.c” we will get a new file named as sample.s which is assembly language program equivalent to sample.c source code. Let us see the contents of sample.s

bosch@bosch-Inspiron-N5050:~$ cat sample.s ( its output would be as below )

	.file	"sample.c"
	.section	.rodata
.LC0:
	.string	"Hello world"
	.text
	.globl	Hello
	.type	Hello, @function
Hello:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	Hello, .-Hello
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$0, %eax
	call	Hello
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
	.section	.note.GNU-stack,"",@progbits

Video How to



Related Contents to follow