Assembly generated by GCC

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 09:41, 30 September 2015 (EDT)


Simple C Program


  • We are interested only in seeing how declarations are performed, and how assignment and simple arithmetic is done.


// integerArithmetic.c
// D. Thiebaut
// Declares 4 ints, and perform simple operations with them.
// Compile as follows:
//
//         gcc -S -m32 integerArithmetic.c
//

// create some globals
int a;
int b;
int c;
int d;

// main entry point
int main() {
   
    a = 3;
    b = 5;
    c = a*2 + b;
    d = (a + b + 10 + 2*c ) * 2 - 20;

    return 0;
}


  • Output listing file: (Warning! There exists 2 different syntaxes for writing assembly for the Intel processors, Intel's and AT&T. We use Intel's. The GCC compiler (of course) uses AT&T's.) The difference between the two syntaxes are illustrated in this document.


	.file	"integerArithmetic.c"
	.comm	a,4,4
	.comm	b,4,4
	.comm	c,4,4
	.comm	d,4,4
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	movl	$3, a
	movl	$5, b
	movl	a, %eax
	leal	(%eax,%eax), %edx
	movl	b, %eax
	addl	%edx, %eax
	movl	%eax, c
	movl	a, %edx
	movl	b, %eax
	addl	%edx, %eax
	leal	10(%eax), %edx
	movl	c, %eax
	addl	%eax, %eax
	addl	%edx, %eax
	subl	$10, %eax
	addl	%eax, %eax
	movl	%eax, d
	movl	$0, %eax
	popl	%ebp
	.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
[