Manual decompilation of asm fragment

I am trying to decompile the following asm fragment (all that I have):

55                      push   %rbp
48 89 e5                mov    %rsp,%rbp
48 81 ec d0 00 00 00    sub    $0xd0,%rsp
64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
00 00 
48 89 45 f8             mov    %rax,-0x8(%rbp)
31 c0                   xor    %eax,%eax
48 c7 85 30 ff ff ff    movq   $0x0,-0xd0(%rbp)
00 00 00 00 
48 8d b5 38 ff ff ff    lea    -0xc8(%rbp),%rsi
b8 00 00 00 00          mov    $0x0,%eax
ba 18 00 00 00          mov    $0x18,%edx
48 89 f7                mov    %rsi,%rdi
48 89 d1                mov    %rdx,%rcx
f3 48 ab                rep stos %rax,%es:(%rdi)
48 8b 15 19 06 20 00    mov    0x200619(%rip),%rdx
48 8d 85 30 ff ff ff    lea    -0xd0(%rbp),%rax
be ce 0f 40 00          mov    $0x400fce,%esi
48 89 c7                mov    %rax,%rdi
b8 00 00 00 00          mov    $0x0,%eax
e8 4e fc ff ff          callq  4008a0 <sprintf@plt>

Here is my attempt:

char buf[192] = {0};
sprintf(buf, "hello %s", name);

I compiled this with gcc 4.8.5 and it gave me:

55                      push   %rbp
48 89 e5                mov    %rsp,%rbp
48 81 ec d0 00 00 00    sub    $0xd0,%rsp
64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
00 00 
48 89 45 f8             mov    %rax,-0x8(%rbp)
31 c0                   xor    %eax,%eax
48 8d b5 30 ff ff ff    lea    -0xd0(%rbp),%rsi
b8 00 00 00 00          mov    $0x0,%eax
ba 18 00 00 00          mov    $0x18,%edx
48 89 f7                mov    %rsi,%rdi
48 89 d1                mov    %rdx,%rcx
f3 48 ab                rep stos %rax,%es:(%rdi)
48 8b 15 14 14 20 00    mov    0x201414(%rip),%rdx
48 8d 85 30 ff ff ff    lea    -0xd0(%rbp),%rax
be 2e 10 40 00          mov    $0x40102e,%esi
48 89 c7                mov    %rax,%rdi
b8 00 00 00 00          mov    $0x0,%eax
e8 cb fb ff ff          callq  4008a0 <sprintf@plt>

I am trying to understand why this exists:

movq   $0x0,-0xd0(%rbp)

and the subsequent use of -0xd0 (% rbp) as a pointer to the sprintf argument. I am puzzled because rep start with -0xc8 (% rbp) and not -0xd0 (% rbp).

This is most likely a compiler, but still I'm curious what might be the source code that created this asm.

+4
source share
1 answer

I imagine something like:

char buf[192] = {0, 0, 0, 0, 0, 0, 0, 0};
sprintf(buf + 8, "hello %s", name);

... will give you this result.

movq 0 (8- ) . -0xc8(%rbp) .

+1

Source: https://habr.com/ru/post/1652496/


All Articles