c++ portout function

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
slacker

c++ portout function

Post by slacker »

anyone know what is wrong with this function?

Code: Select all

void Port_Out(char data, short port)
{
    asm("outb %al, %dx"::"d"(port), "a"(data));
}
Tim

Re:c++ portout function

Post by Tim »

Maybe gcc knows. Did you get some kind of error message?
slacker

Re:c++ portout function

Post by slacker »

invalid asm operand number missing after %-letter
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c++ portout function

Post by Pype.Clicker »

you must double the % sign!
GCC uses %* as a parameter reference (for instance %0 is the first parameter of the asm command). registers must have the form %%eax ...

You could do

Code: Select all

void Port_Out(char data, short port)
{
    asm("outb %1, %0"::"d"(port), "a"(data));
}
or

Code: Select all

void Port_Out(char data, short port)
{
    asm("outb %%al, %%dx"::"d"(port), "a"(data));
}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c++ portout function

Post by Solar »

Google search for "inline asm", link #8 "DJGPP FAQ - Inline Assembly code with GCC", suggests

info gcc "C Extensions" "Extended Asm"

At about 34% of the file, it reads:
If you refer to a particular hardware register...
That paragraph will probably help you. (No time to try things myself.)


Heh, Pype beat me to it... ;-)
Every good solution is obvious once you've found it.
slacker

Re:c++ portout function

Post by slacker »

k so whenever i have input/output variables i need two % to reference registers?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c++ portout function

Post by Solar »

That's what the doc and Pype said.
Every good solution is obvious once you've found it.
Post Reply