How do I generate assembly from a C file?

Any C compiler has multiple stages. A fundamental one is assembler. The compiler runs the C preprocessor, parses the C file, and transforms that AST into assembler. Usually, we instruct the compiler to then assemble that into an object file and link it, producing a binary.

If we want to see the assembly, we pass -S to the compiler. Take the C file:

int main(void) {
  return 0;
}

We ordinarily compile and run this with:

% cc main.c
% ./a.out

But we can generate assembler instead:

% cc -S main.c

This gives us the file:

	.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
	xorl	%eax, %eax
	movl	$0, -4(%rbp)
	popq	%rbp
	retq
	.cfi_endproc


.subsections_via_symbols
I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.