This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: which registers must be preserved for the caller


On 22/08/2017 15:16, Andrew Makhorin wrote:

> I asked my question, because GCC doesn't seem to follow System V ABI.
> In the following example %ebx, %esi, and %edi are not preserved being
> clobbered by the generated code.

I think you have misread the generated assembly code.
See below.

> mao@corvax:~/Desktop/foo$ cat foo.c
> int foo(int x[], int y[], int z[], int i, int j, int k)
> {
>       int t;
>       t = (x[i+j] + y[j+k] + 2) * (z[i+j+k] + 1) * (z[k+1] + 2);
>       return t;
> }
> mao@corvax:~/Desktop/foo$ gcc -c -O2 -save-temps foo.c
> mao@corvax:~/Desktop/foo$ cat foo.s
> 	.file	"foo.c"
> 	.text
> 	.p2align 4,,15
> 	.globl	foo
> 	.type	foo, @function
> foo:
> .LFB0:
> 	.cfi_startproc
> 	subl	$12, %esp
> 	.cfi_def_cfa_offset 16
> 	movl	32(%esp), %eax
> 	movl	36(%esp), %edx
> 	movl	%ebx, (%esp)

Here, ebx is saved on the stack.

> 	.cfi_offset 3, -16
> 	movl	28(%esp), %ebx
> 	movl	%esi, 4(%esp)

Here, esi is saved on the stack.

> 	movl	24(%esp), %ecx
> 	movl	%edi, 8(%esp)

Here, edi is saved on the stack.

> 	.cfi_offset 6, -12
> 	.cfi_offset 7, -8
> 	movl	20(%esp), %edi
> 	leal	(%eax,%edx), %esi
> 	addl	%eax, %ebx
> 	movl	16(%esp), %eax
> 	movl	(%eax,%ebx,4), %eax
> 	addl	%edx, %ebx
> 	movl	(%ecx,%ebx,4), %ebx
> 	addl	(%edi,%esi,4), %eax
> 	movl	4(%ecx,%edx,4), %edx
> 	movl	4(%esp), %esi

Here, esi is restored from the stack.

> 	addl	$1, %ebx
> 	addl	$2, %eax
> 	movl	8(%esp), %edi

Here, edi is restored from the stack.

> 	imull	%ebx, %eax
> 	movl	(%esp), %ebx

Here, ebx is restored from the stack.

> 	addl	$12, %esp
> 	.cfi_restore 7
> 	.cfi_restore 6
> 	.cfi_restore 3
> 	.cfi_def_cfa_offset 4
> 	addl	$2, %edx
> 	imull	%edx, %eax
> 	ret
> 	.cfi_endproc
> .LFE0:
> 	.size	foo, .-foo
> 	.ident	"GCC: (Debian 4.7.2-5) 4.7.2"
> 	.section	.note.GNU-stack,"",@progbits
> mao@corvax:~/Desktop/foo$ gcc --version
> gcc (Debian 4.7.2-5) 4.7.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]