1

I have written the following code in C and i am trying to convert it to assembly and view each letter as the corresponding hex value :

#include <stdio.h>

int main()
{
    int x = 1337;
    char *s = "Hello world";
    char buff[4] = {'a','b','c','d'};

    int i = 0;
    while (i<4)
    {
        printf("%02x", buff[i++]);
    }

    putchar(0xa);
    return 0;
}

But the output i am getting is a lot different than the one i wanted :

0000000000001149 <main>:
    1149:       55                      push   %rbp
    114a:       48 89 e5                mov    %rsp,%rbp
    114d:       48 83 ec 20             sub    $0x20,%rsp
    1151:       c7 45 f8 39 05 00 00    movl   $0x539,-0x8(%rbp)
    1158:       48 8d 05 a5 0e 00 00    lea    0xea5(%rip),%rax        # 2004 <_IO_stdin_used+0x4>
    115f:       48 89 45 f0             mov    %rax,-0x10(%rbp)
    1163:       c7 45 ec 61 62 63 64    movl   $0x64636261,-0x14(%rbp)
    116a:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)
    1171:       eb 29                   jmp    119c <main+0x53>
    1173:       8b 45 fc                mov    -0x4(%rbp),%eax
    1176:       8d 50 01                lea    0x1(%rax),%edx
    1179:       89 55 fc                mov    %edx,-0x4(%rbp)
    117c:       48 98                   cltq
    117e:       0f b6 44 05 ec          movzbl -0x14(%rbp,%rax,1),%eax
    1183:       0f be c0                movsbl %al,%eax
    1186:       89 c6                   mov    %eax,%esi
    1188:       48 8d 05 81 0e 00 00    lea    0xe81(%rip),%rax        # 2010 <_IO_stdin_used+0x10>
    118f:       48 89 c7                mov    %rax,%rdi
    1192:       b8 00 00 00 00          mov    $0x0,%eax
    1197:       e8 a4 fe ff ff          call   1040 <printf@plt>
    119c:       83 7d fc 03             cmpl   $0x3,-0x4(%rbp)
    11a0:       7e d1                   jle    1173 <main+0x2a>
    11a2:       bf 0a 00 00 00          mov    $0xa,%edi
    11a7:       e8 84 fe ff ff          call   1030 <putchar@plt>
    11ac:       b8 00 00 00 00          mov    $0x0,%eax
    11b1:       c9                      leave
    11b2:       c3                      ret

The command i used was objdump -d test.out. The output i want is like the following :

MOV      %eax, 0x61 (for letter 'a')

or something like :
MOV      w0, 0x61

How can i get this output?

bd55
  • 55
  • 7
  • 1
    It's there :) `movl $0x64636261,-0x14(%rbp)`. The cheeky compiler merged this all into a 32 bit number, then little endian on top. Un-endian it and you get: 0x61 0x62 0x63 0x64. – Lundin Sep 05 '22 at 14:00
  • 1
    isn't that what you got? movl $0x64636261,-0x14(%rbp) – user253751 Sep 05 '22 at 14:06
  • 1
    And `movl $0x539,-0x8(%rbp)` is `x = 1337;`. – the busybee Sep 05 '22 at 14:07
  • If you want `MOV w0, 0x61`, compile for AArch64. And remove the array initializer, instead do each byte one at a time with assignment statements, like `buff[0] = 'a';` if you want to see each element as a separate immediate in a debug build. – Peter Cordes Sep 05 '22 at 14:20
  • 1
    Also, in AT&T syntax, `MOV %eax, 0x61` stores EAX to absolute address `0x61`. So a C statement like `*(char*)0x61 = x` might do it. – Peter Cordes Sep 05 '22 at 14:21
  • Thank you all for your help :) @PeterCordes i know but i tried downloading compilers such as gcc-linaro-aarch64-linux-gnu but didnt work for me. Do you have any recommendation as to what to download to do such compilation? thanks – bd55 Sep 05 '22 at 15:44
  • 1
    `aarch64-linux-gnu-gcc` works for me on Arch Linux. It makes an executable I can disassemble with `llvm-objdump -d` or with `aarch64-linux-gnu-objdump -drwC`. Of course if you just want to look at the asm, use https://godbolt.org/, which has compilers for many architectures. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Sep 05 '22 at 16:05

0 Answers0