Bonfra
Bonfra

Reputation: 296

C inline asm for x86 in / out port I/O has operand-size mismatch

I'm following a tutorial on youtube and he said that it's needed to write some asm code in C (i'm not very good in assembly) so i just coped the entire code:

unsigned char inPortB (unsigned int _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

and

void outPortB (unsigned int _port, unsigned char _data) {
    __asm__ __volatile__ ("outb %1, %0" : :  "dN" (_port), "a" (_data));
}

but when i compile i get this error:

operand type mismatch for 'in'
operand type mismatch for 'out'

how can i fix it?

Upvotes: 3

Views: 2323

Answers (1)

fuz
fuz

Reputation: 92966

The port number needs to be in dx which is a 16 bit register. To make gcc generate a reference to dx instead of edx as it does with your code, you need to give _port a 16 bit type, e.g. unsigned short:

unsigned char inPortB (unsigned short _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

This should fix your issues.

Upvotes: 8

Related Questions