Date created: Sunday, April 10, 2016 8:05:07 PM. Last modified: Monday, June 3, 2019 8:05:25 AM

Compile C to Readable ASM

Compile the source with debugging enabled, compile but don't link:

bensley@ubuntu:~/c$ cat if.c
#include <stdio.h>

int main () {

    const unsigned int VAL1 = 1;
    const unsigned long VAL2 = 100000;
    unsigned int value1 = 1;
    unsigned long value2 = 100000;

    if ( (value1 == VAL1) && (value2 == VAL2) ) {
        printf("match\n");
    }

	return 0;
}


bensley@ubuntu:~/c$ gcc -g -c if.c 
bensley@ubuntu:~/c$ objdump -d -M intel -S if.o 

if.o:     file format elf32-i386


Disassembly of section .text:

00000000 :
#include <stdio.h>

int main () {
   0:	55                   	push   ebp
   1:	89 e5                	mov    ebp,esp
   3:	83 e4 f0             	and    esp,0xfffffff0
   6:	83 ec 20             	sub    esp,0x20

    const unsigned int VAL1 = 1;
   9:	c7 44 24 10 01 00 00 	mov    DWORD PTR [esp+0x10],0x1
  10:	00 
    const unsigned long VAL2 = 100000;
  11:	c7 44 24 14 a0 86 01 	mov    DWORD PTR [esp+0x14],0x186a0
  18:	00 
    unsigned int value1 = 1;
  19:	c7 44 24 18 01 00 00 	mov    DWORD PTR [esp+0x18],0x1
  20:	00 
    unsigned long value2 = 100000;
  21:	c7 44 24 1c a0 86 01 	mov    DWORD PTR [esp+0x1c],0x186a0
  28:	00 

    if ( (value1 == VAL1) && (value2 == VAL2) ) {
  29:	8b 44 24 18          	mov    eax,DWORD PTR [esp+0x18]
  2d:	3b 44 24 10          	cmp    eax,DWORD PTR [esp+0x10]
  31:	75 16                	jne    49 <main+0x49>
  33:	8b 44 24 1c          	mov    eax,DWORD PTR [esp+0x1c]
  37:	3b 44 24 14          	cmp    eax,DWORD PTR [esp+0x14]
  3b:	75 0c                	jne    49 <main+0x49>
        printf("match\n");
  3d:	c7 04 24 00 00 00 00 	mov    DWORD PTR [esp],0x0
  44:	e8 fc ff ff ff       	call   45 <main+0x45>
    }

	return 0;
  49:	b8 00 00 00 00       	mov    eax,0x0
}
  4e:	c9                   	leave  
  4f:	c3                   	ret    

gcc can show the machine code (without the inline c code as above using the -S switch):

bensley@ubuntu:~/c$ gcc -O2 -S if.c 
bensley@ubuntu:~/c$ cat if.s
	.file	"if.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"match"
	.section	.text.startup,"ax",@progbits
	.p2align 4,,15
	.globl	main
	.type	main, @function
main:
.LFB24:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	andl	$-16, %esp
	subl	$16, %esp
	movl	$.LC0, (%esp)
	call	puts
	xorl	%eax, %eax
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
.LFE24:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
	.section	.note.GNU-stack,"",@progbits
bensley@ubuntu:~/c$ gcc -m32 -S -masm=intel if.c
bensley@ubuntu:~/c$ cat if.s
	.file	"if.c"
	.intel_syntax noprefix
	.section	.rodata
.LC0:
	.string	"match"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	push	ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	mov	ebp, esp
	.cfi_def_cfa_register 5
	and	esp, -16
	sub	esp, 32
	mov	DWORD PTR [esp+16], 1
	mov	DWORD PTR [esp+20], 100000
	mov	DWORD PTR [esp+24], 1
	mov	DWORD PTR [esp+28], 100000
	mov	eax, DWORD PTR [esp+24]
	cmp	eax, DWORD PTR [esp+16]
	jne	.L2
	mov	eax, DWORD PTR [esp+28]
	cmp	eax, DWORD PTR [esp+20]
	jne	.L2
	mov	DWORD PTR [esp], OFFSET FLAT:.LC0
	call	puts
.L2:
	mov	eax, 0
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
	.section	.note.GNU-stack,"",@progbits

Previous page: Box-256 - CHECKERBOARD
Next page: Intel Opcode Examples