Difference between revisions of "CSC231 C++ Assembly Code"

From dftwiki3
Jump to: navigation, search
(Source C++)
(Assembly Output)
 
Line 41: Line 41:
  
 
* This generates a listing file in assembly in '''arithmetic.s''':
 
* This generates a listing file in assembly in '''arithmetic.s''':
 +
 +
<br />
 +
<br />
  
 
<source lang="asm">
 
<source lang="asm">

Latest revision as of 09:33, 13 October 2010

--D. Thiebaut 14:32, 13 October 2010 (UTC)


C++ Source

// arithmetic.cpp
// D. Thiebaut
// A C++ program that does initializes a few variables and
// performs operations on them.
//
// Use this to demonstrate how the code generated by a compiler
// looks.
//
// Compile as follows:
//
//   g++ -S arithmetic.cpp
//
// Observe the assembly:
//
//   less arithmetic.s
//

int x, y, z;

int main() {
  x = 3;
  y = 5;
  z = 13 * (x-1) * y;

  return 0;
}

Assembly Output

  • Compile the program as follows
 g++   -S   arithmetic.cpp
  • This generates a listing file in assembly in arithmetic.s:



	.file	"arithmetic.cpp"
.globl x
	.bss
	.align 4
	.type	x, @object
	.size	x, 4
x:
	.zero	4
.globl y
	.align 4
	.type	y, @object
	.size	y, 4
y:
	.zero	4
.globl z
	.align 4
	.type	z, @object
	.size	z, 4
z:
	.zero	4
	.text
.globl main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	.cfi_personality 0x0,__gxx_personality_v0
	pushl	%ebp
	.cfi_def_cfa_offset 8
	movl	%esp, %ebp
	.cfi_offset 5, -8
	.cfi_def_cfa_register 5
	movl	$3, x
	movl	$5, y
	movl	x, %eax
	leal	-1(%eax), %edx
	movl	%edx, %eax
	addl	%eax, %eax
	addl	%edx, %eax
	sall	$2, %eax
	leal	(%eax,%edx), %edx
	movl	y, %eax
	imull	%edx, %eax
	movl	%eax, z
	movl	$0, %eax
	popl	%ebp
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.4.1 20090725 (Red Hat 4.4.1-2)"
	.section	.note.GNU-stack,"",@progbits