0

I have C-program with assembly insert. Here is code: main.c:

#include <stdio.h>

int f() {
    int a = 0, b;
    __asm__ (".intel_syntax noprefix\n\t"
             "mov edx, 1\n\t"
             :"=r"(b)
             :"r"(a)
             :"eax"
            );
    return b;
}

int  main() {
    printf("%d", f());
}

I have found out how to compile this code (gcc -std=c11 -S main.c -o main), but when i run it (./main) - here is a problem in terminal: bash: ./main: Permission denied.

When I try to compile it without -S flag, I get many unreasonable mistakes:

/tmp/ccw3H0Mb.s: Assembler messages:
/tmp/ccw3H0Mb.s:23: Error: Instruction does not exist: «movl%edx,-4(%rbp)»
/tmp/ccw3H0Mb.s:24: Error: Instruction does not exist: «movl4(%rbp),%eax»
/tmp/ccw3H0Mb.s:46: Error: Instruction does not exist: «movl$0,%eax»
/tmp/ccw3H0Mb.s:48: Error: Instruction does not exist: «movl%eax,%esi»
/tmp/ccw3H0Mb.s:49: Error: Garbage «(%rip)» after expression
/tmp/ccw3H0Mb.s:50: Error: Instruction does not exist: «movl$0,%eax»
/tmp/ccw3H0Mb.s:52: Error: Instruction does not exist: «movl$0,%eax»

What is the error and how can I fix it to run my code correctly? Thanks! :)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    The option `-S` causes assembly code to be generated. It does not produce a binary. Remove the option `-S` and you should obtain a binary that can be executed. – fuz Nov 02 '20 at 20:18
  • Your inline asm is broken. You hard-code `edx`, but you tell the compiler it can pick any register for the output operand. – Peter Cordes Nov 03 '20 at 02:53

1 Answers1

0

All I needed was just adding -masm=intel to the compilation command, and, of course, I removed -S flag (thanks to fuz). It is important because I am using Intel syntax, so gcc must be awared about it.

Now it looks like:

gcc -std=c11 -masm=intel main.c -o main