Assembler Code

Einfache C-Funktion

int a[8];
int b;

void
f()
{
    int i;

    b = 0;
    for (i=0; i<8; ++i) {
        b += a[i];
    }
}

Wir erzeugen nur den Assembler Code, keinen Maschinen Code:

$shell> gcc -S -fno-asynchronous-unwind-tables loop.c                          
$shell> cat loop.s                                                             
	.comm	_a,32,5
	.comm	_b,4,2
	.text
	.globl _f
_f:
	pushq	%rbp
	movq	%rsp, %rbp
	movq	_b@GOTPCREL(%rip), %rax
	movl	$0, (%rax)
	movl	$0, -4(%rbp)
	jmp	L2
L3:
	movq	_a@GOTPCREL(%rip), %rax
	movl	-4(%rbp), %edx
	movslq	%edx, %rdx
	movl	(%rax,%rdx,4), %edx
	movq	_b@GOTPCREL(%rip), %rax
	movl	(%rax), %eax
	addl	%eax, %edx
	movq	_b@GOTPCREL(%rip), %rax
	movl	%edx, (%rax)
	addl	$1, -4(%rbp)
L2:
	cmpl	$7, -4(%rbp)
	jle	L3
	popq	%rbp
	ret
	.subsections_via_symbols

Jetzt mit eingeschaltetem Optimierer:

$shell> gcc -O3 -S -fno-asynchronous-unwind-tables loop.c                      
$shell> cat loop.s                                                             
	.text
	.align 4,0x90
	.globl _f
_f:
	movq	_a@GOTPCREL(%rip), %rax
	movdqa	(%rax), %xmm0
	paddd	16(%rax), %xmm0
	movq	_b@GOTPCREL(%rip), %rax
	movdqa	%xmm0, %xmm2
	psrldq	$8, %xmm2
	paddd	%xmm2, %xmm0
	movdqa	%xmm0, %xmm3
	psrldq	$4, %xmm3
	paddd	%xmm3, %xmm0
	movd	%xmm0, (%rax)
	ret
	.comm	_b,4,2
	.comm	_a,32,5
	.subsections_via_symbols