0

This code works as a IN func for i/o ports in inline c:

int func(short port)
{
    short data;
    __asm__(
        ".intel_syntax \n\t"
        "in byte %0, %1\n\t"

        ".att_syntax\n\t"
        :"=a" (data)
        :"dN" (port)
        :
    );
    return data;
}

but on compiling the c code the assembler outputs:

file.c:5: Error: junk `ax' after expression

Here is the compiling command

gcc -ffreestanding -m32 -c file.c -o file.o

I tried viewing the code in assemly using -S flag but it seems to be fine

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Programmerabc
  • 329
  • 2
  • 10
  • 1
    You forgot the `noprefix` part of `.intel_syntax noprefix`. Also, what is `byte` doing there? That's not how GAS Intel syntax works, and it doesn't match the size of the register GCC will pick for a `short`. `byte` is the expression after which GAS found another token, not a comma, thus junk. – Peter Cordes Aug 06 '22 at 07:01
  • Thanks @PeterCordes I changed the code as you mentioned and it fixed the error – Programmerabc Aug 06 '22 at 07:03
  • 1
    BTW, using Intel syntax with GCC inline asm is cumbersome ([Can I use Intel syntax of x86 assembly with GCC?](https://stackoverflow.com/q/9347909)), and that's not a great way to do it. It will break if you ever compile with `-masm=intel`, since it puts the assembler into AT&T mode for the rest of the file. Avoiding that requires dialect-alternatives like `in{b %1, %0 | %0, %1}` (which picks one side of the `{att | intel}`) – Peter Cordes Aug 06 '22 at 07:11
  • 1
    Better to just use AT&T syntax for simple stuff, so you can write `inb` to force the assembler to check for a byte register instead of relying on the register to imply the IO width. [seg fault on IN instruction, inline assembly GCC](https://stackoverflow.com/q/48178058) shows one way to write a correct AT&T wrapper. (The bug there is trying to use it in user-space.) – Peter Cordes Aug 06 '22 at 07:14
  • 1
    Hmm, with the mutant hybrid `.intel_syntax prefix` you used, that asks for register names like `%al` or `%ax` you're getting from GCC, despite being in Intel syntax mode for operand-order. But GAS is ok either way, and clang only accepts the two sane options: `.intel_syntax noprefix` or `.att_syntax [prefix]`. – Peter Cordes Aug 06 '22 at 07:17

0 Answers0