Oct 6th, 2023

How to get the assembly code of a C Program?

Author Picture

Dhairya Shah

Software Engineer

Here’s how to use GCC (GNU Compiler Collection) to get the assembly output from C code:

Step:1 Write your code

// sample.c
#include <stdio.h>

int main() {
    printf("Hello, Assembly!\n");
    return 0;

Step 2: Compile with the -S Flag

clang -S -o sample.s sample.c

Step 3: Assembly output

Assembly output of the sample.c

.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 14, 0	sdk_version 14, 0
	.globl	_main                           ; -- Begin function main
	.p2align	2
_main:                                  ; @main
; %bb.0:
	sub	sp, sp, #32
	.cfi_def_cfa_offset 32
	stp	x29, x30, [sp, #16]             ; 16-byte Folded Spill
	add	x29, sp, #16
	.cfi_def_cfa w29, 16
	.cfi_offset w30, -8
	.cfi_offset w29, -16
	mov	w8, #0
	str	w8, [sp, #8]                    ; 4-byte Folded Spill
	stur	wzr, [x29, #-4]
	adrp	x0, l_.str@PAGE
	add	x0, x0, l_.str@PAGEOFF
	bl	_printf
	ldr	w0, [sp, #8]                    ; 4-byte Folded Reload
	ldp	x29, x30, [sp, #16]             ; 16-byte Folded Reload
	add	sp, sp, #32
                                        ; -- End function
	.section	__TEXT,__cstring,cstring_literals
l_.str:                                 ; @.str
	.asciz	"Hello, Assembly!\n"


I hope this article was helpful to you! Thanks for reading!

Have a great day!