Page 1 of 1

Outport and Inport with Microsoft Visual C++

Posted: Tue Oct 03, 2006 8:06 am
by Jeko
I always develop with gcc, but now I'm developing with visual C++ from Microsoft.
With gcc I use this functions for outport and inport:

Code: Select all

inline unsigned char inportb(unsigned short int port)
{
        unsigned char ret;
        asm volatile ("inb %%dx,%%al":"=a" (ret):"d"(port));
        return ret;
}

inline void outportb(unsigned short int port, unsigned char value)
{
        asm volatile ("outb %%al,%%dx": :"d" (port), "a"(value));
}
How can I make this with Visual C++?[/code]

Posted: Tue Oct 03, 2006 10:53 am
by delroth
With Visual C++, you can use Intel syntax for inline assembler.
So your code is :

Code: Select all

inline unsigned char inportb(unsigned short int port) 
{ 
        unsigned char ret; 
        __asm
        {
                in port, ret
        }
        return ret; 
} 
 
inline void outportb(unsigned short int port, unsigned char value) 
{ 
        __asm
        {
                out port, value
        }
}

Posted: Wed Oct 04, 2006 2:46 pm
by Thomac
With Intel syntax you are still restricted to using registers with IN and OUT.
OUT and IN only accept DX as the address, and any 8 bit register as input/output. (AL, BL, BH, AH, etcetera)

This snippet shouldn't throw opcode/syntax errors:

Code: Select all

inline unsigned char inportb(unsigned short int port)
{
        _asm
        {
                mov dx, word ptr [port]
                in dx, al
                stos byte ptr [port]
        }
        return port;
}
 
inline void outportb(unsigned short int port, unsigned char value)
{
        _asm
        {
                mov dx, word ptr [port]
                lods byte ptr [value]
                out dx, al
        }
}

Posted: Thu Oct 05, 2006 9:45 am
by Jeko
thank you very much!

Posted: Fri Oct 06, 2006 5:13 am
by Jeko
There are some errors in your code!
They don't work!

Posted: Fri Oct 06, 2006 8:22 am
by Thomac
For some reason I can't get the IN instruction to work.
I managed to get outportb to work though:

Code: Select all

_inline void outportb(unsigned short int port, unsigned char value)
{
        _asm
        {
                mov dx, word ptr [port]
                mov al, byte ptr [value]
                out dx, al
        }
}

Posted: Fri Oct 06, 2006 8:37 am
by Jeko
thank you, outportb works!
But, what can I make with inportb?

Posted: Fri Oct 06, 2006 11:50 am
by Thomac
I forgot that dx and al's positions are reversed with IN. :P
Here's a working version:

Code: Select all

_inline char outportb(unsigned short int port)
{
        _asm
        {
                mov dx, word ptr [port]
                in al, dx
                mov al, byte ptr [port]
        }
        return (char)port;
}

Posted: Mon Oct 09, 2006 7:26 am
by Jeko
thank you! Now it works