0

In C I have:

__asm__ (   
            "mov    $0x2,%rax;"
            "mov    $0x6000dd,%rdi;"
            "mov    $0x0,%rsi;"
            "syscall;"
        );

But when I comppile it in the assembly file I see:

# 12 "mine.cxx" 1
mov    $0x2,%rax;mov    $0x6000dd,%rdi;mov    $0x0,%rsi;syscall;

why they are in 1 line like this How to separate them?

  • 2
    Why do you say "unordered"? The instructions within it are in the same order you wrote, still separated by the `;` GAS statement-separator after C string-literal concatenation at compile-time. – Peter Cordes Jun 21 '21 at 12:33
  • 2
    But there's nothing stopping the compiler from reordering your whole Basic Asm statement with surrounding code (no `"memory"` clobber). And it's also totally unsafe because you're modifying registers without telling the compiler about it, RAX, RDI, RSI, and implicitly RCX and R11. (**It's basically never safe or a good idea to to use Basic Asm inside a function, other than `__attribute__((naked))`**. https://stackoverflow.com/tags/inline-assembly/info) – Peter Cordes Jun 21 '21 at 12:34

2 Answers2

1

You should add \n to separate lines.

__asm__ (   
            "mov    $0x2,%rax;\n"
            "mov    $0x6000dd,%rdi;\n"
            "mov    $0x0,%rsi;\n"
            "syscall;\n"
        );
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks, how can I add tab before each command to alline it –  Jun 21 '21 at 12:35
  • By adding tab (`\t`) like `"mov $0x2,%rax;\n\t"`. – MikeCAT Jun 21 '21 at 12:35
  • 3
    Please don't copy unsafe inline asm into answers without commenting on the problems. This needs clobbers on RAX, RDI, RSI, and RCX + R11. And thus has to use GNU C Extended asm. – Peter Cordes Jun 21 '21 at 12:35
0

Adding \n will be helpful for you.

__asm__ (   
            "mov    $0x2,%rax;\n"
            "mov    $0x6000dd,%rdi;\n"
            "mov    $0x0,%rsi;\n"
            "syscall;\n"
        );
moto
  • 38
  • 4
  • 2
    This is the same answer MikeCAT posted 6 minutes ago. As I said there, please don't copy unsafe inline asm into answers without commenting on the problems. This needs clobbers on RAX, RDI, RSI, and RCX + R11. And thus has to use GNU C Extended asm. – Peter Cordes Jun 21 '21 at 12:37