Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting help: __asm__ __volatile__

I would like to port C's outb function to D.

static __inline void outb (unsigned char value, unsigned short int port)
{
    __asm__ __volatile__ ("outb %b0,%w1"
                          :
                          :
                         "a" (value),
                          "Nd" (port));
}

This is D version.

extern(C) 
{
    void outb (ubyte value, ushort port)
    {
        // I couldn't figure out this part
   }

}

These are some links about the subject.

D Inline Assembler

http://dlang.org/iasm.html

GCC-Inline-Assembly-HOWTO

http://ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

But I don't know assembly language so I need some help. Any help would be appreciated. Thanks.

like image 823
Erdem Avatar asked Dec 23 '11 10:12

Erdem


People also ask

What is __ asm __ volatile?

The __volatile__ modifier on an __asm__ block forces the compiler's optimizer to execute the code as-is. Without it, the optimizer may think it can be either removed outright, or lifted out of a loop and cached.

What is asm () in C?

The asm keyword allows you to embed assembler instructions within C code. GCC provides two forms of inline asm statements. A basic asm statement is one with no operands (see Basic Asm), while an extended asm statement (see Extended Asm) includes one or more operands.

What is r in asm?

The lines with "r" or "=r" are operand constraints. The "=" means output operand. Essentially, this: :"=r"(y) :"r"(x) means that %0 (ie: the first operand) corresponds to y and is for output, and %1 (the second operand) corresponds to x.

What is asm Goto?

asm goto allows assembly code to jump to one or more C labels. The GotoLabels section in an asm goto statement contains a comma-separated list of all C labels to which the assembler code may jump.


1 Answers

The outb instruction should only be called as outb %al, %dx where %al is the value and %dx is the port.

D uses Intel syntax for x86, as opposed to GNU assembler which uses the AT&T syntax by default. The corresponding Intel syntax would be out dx, al, and corresponding code in D would look like:

void outb (ubyte value, ushort port)
{
    asm {
        mov AL, value;
        mov DX, port;
        out DX, AL;
    }
}

Note that you don't need to write the assembly at all, because druntime has the core.bitop.outp function which perform the same instruction.

void outb (ubyte value, ushort port)
{
    import core.bitop;
    outp(port, value);
}
like image 131
kennytm Avatar answered Sep 19 '22 17:09

kennytm