Outport and Inport with Microsoft Visual C++

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
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Outport and Inport with Microsoft Visual C++

Post 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]
delroth
Posts: 8
Joined: Sun Oct 01, 2006 3:29 am
Location: At my computer
Contact:

Post 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
        }
}
Thomac
Posts: 11
Joined: Sat Sep 16, 2006 2:23 am

Post 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
        }
}
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

thank you very much!
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

There are some errors in your code!
They don't work!
Thomac
Posts: 11
Joined: Sat Sep 16, 2006 2:23 am

Post 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
        }
}
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

thank you, outportb works!
But, what can I make with inportb?
Thomac
Posts: 11
Joined: Sat Sep 16, 2006 2:23 am

Post 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;
}
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

thank you! Now it works
Post Reply