You are not logged in.

#1 2024-09-16 14:56:41

Baird
Member
Registered: 2023-03-10
Posts: 16

[SOLVED] Why does printf use "%edi" here?

https://godbolt.org/z/ebPYE48c3

Why isn't it "%rsi", isn't it a pointer?

C:

#include <stdio.h>

int main(void)
{
    printf("OK %d\n", 42);
}

Assembly:

.LC0:
        .string "OK %d\n"
main:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $42, %esi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        popq    %rbp
        ret

Last edited by Baird (2024-09-17 04:54:16)

Offline

#2 2024-09-16 16:24:58

mpan
Member
Registered: 2012-08-01
Posts: 1,335
Website

Re: [SOLVED] Why does printf use "%edi" here?

Probably because the platform they used is not the platform you expected it is? What calling convention did you expect?

On AMD64 RDI is the first register used for a pointer or an integer. I guess this is what they use, though that is not indicated anywhere.

For comparison this is output for gcc 14.2.1 with AMD64 ABI:

0000000000001139 <main>:
    1139:	55                   	push   %rbp
    113a:	48 89 e5             	mov    %rsp,%rbp
    113d:	be 2a 00 00 00       	mov    $0x2a,%esi
    1142:	48 8d 05 bb 0e 00 00 	lea    0xebb(%rip),%rax        # 2004 <_IO_stdin_used+0x4>
    1149:	48 89 c7             	mov    %rax,%rdi
    114c:	b8 00 00 00 00       	mov    $0x0,%eax
    1151:	e8 da fe ff ff       	call   1030 <printf@plt>
    1156:	b8 00 00 00 00       	mov    $0x0,%eax
    115b:	5d                   	pop    %rbp
    115c:	c3                   	ret

Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

#3 2024-09-16 16:35:23

Baird
Member
Registered: 2023-03-10
Posts: 16

Re: [SOLVED] Why does printf use "%edi" here?

mpan wrote:

Probably because the platform they used is not the platform you expected it is? What calling convention did you expect?

On AMD64 RDI is the first register used for a pointer or an integer. I guess this is what they use, though that is not indicated anywhere.

For comparison this is output for gcc 14.2.1 with AMD64 ABI:

0000000000001139 <main>:
    1139:	55                   	push   %rbp
    113a:	48 89 e5             	mov    %rsp,%rbp
    113d:	be 2a 00 00 00       	mov    $0x2a,%esi
    1142:	48 8d 05 bb 0e 00 00 	lea    0xebb(%rip),%rax        # 2004 <_IO_stdin_used+0x4>
    1149:	48 89 c7             	mov    %rax,%rdi
    114c:	b8 00 00 00 00       	mov    $0x0,%eax
    1151:	e8 da fe ff ff       	call   1030 <printf@plt>
    1156:	b8 00 00 00 00       	mov    $0x0,%eax
    115b:	5d                   	pop    %rbp
    115c:	c3                   	ret

hmmm...interesting...
For dynamic linking, I got the same code as yours.
But for static linking, "%edi" is there.

Last edited by Baird (2024-09-16 16:36:02)

Offline

#4 2024-09-16 17:28:45

mpan
Member
Registered: 2012-08-01
Posts: 1,335
Website

Re: [SOLVED] Why does printf use "%edi" here?

It’s hard to comment, if we don’t see the exact commands you executed and options passed to them.

The snippet I provided is from:

gcc -o main main.c

gcc (GCC) 14.2.1 20240910 (package: gcc 14.2.1+r134+gab884fffe3fc-1)

EDI is the lower 32 bits of RDI. See this nice x86 registers map, RDI just above the colors legend. If only the lower 32 bits are ever used, it’s possible to set EDI only. Though that shouldn’t be the case for a pointer argument, unless the code knows upper bits are always 0, which is possible for statically linked sections.

Last edited by mpan (2024-09-16 17:47:00)


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

#5 2024-09-17 04:30:29

Baird
Member
Registered: 2023-03-10
Posts: 16

Re: [SOLVED] Why does printf use "%edi" here?

I use the same GCC version
commands:

gcc -static main.s

I think your explanation is reasonable, for statically linked sections it is possible.

Last edited by Baird (2024-09-17 05:11:01)

Offline

Board footer

Powered by FluxBB