www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Buggy inline assembler

reply Heinz <malagana15 yahoo.es> writes:
Hi everyone, i've seen many bugs with the inline assembler in D. I've tried
many functions that work with other assemblers, simple functions such as print
to screen (i've seen D users complaining about this too).
I'm writing a block of code to make some sound through the pc speaker. It
compiles fine but at runtime i get a Win32 Exception.
Here's the code:

public void Beep(uint Frequency)
{
    ushort count 1193180 / Frequency;
    asm
    {
        mov AL, 0xB6;
        out 0x43, AL;// It crashes here.
        mov AX, count;
        out 0x42, AL;
        mov AL, AH;
        out 0x42, AL;
        in AL, 0x61;
        or AL, 0x3;
        out 0x61, AL;
        in AL, 0x61;
        and AL, 0xFC;
        out 0x42, AL;
    }
}

I tried this block with tasm and works fine but not with D. The program
crashes at the second line of asm code, looks like we can't send data to
ports, 0x43 in this case.

Does somebody have a hint about this?

Big thanks.
Aug 18 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
Heinz wrote:
 Hi everyone, i've seen many bugs with the inline assembler in D. I've tried
 many functions that work with other assemblers, simple functions such as print
 to screen (i've seen D users complaining about this too).
 I'm writing a block of code to make some sound through the pc speaker. It
 compiles fine but at runtime i get a Win32 Exception.

The problem has nothing to do with the inline assembler. You can use OBJ2ASM to verify that the correct instructions are generated. The problem is that executing IN and OUT instructions under Win32 will generated an operating system exception. To print to screen, use the Win32 Console API functions. I don't remember what the Win32 sound API is, but that has to be used to generate sound.
Aug 18 2006
next sibling parent reply Kristian <kjkilpi gmail.com> writes:
On Fri, 18 Aug 2006 22:59:05 +0300, Walter Bright  
<newshound digitalmars.com> wrote:

 Heinz wrote:
 Hi everyone, i've seen many bugs with the inline assembler in D. I've  
 tried
 many functions that work with other assemblers, simple functions such  
 as print
 to screen (i've seen D users complaining about this too).
 I'm writing a block of code to make some sound through the pc speaker.  
 It
 compiles fine but at runtime i get a Win32 Exception.

The problem has nothing to do with the inline assembler. You can use OBJ2ASM to verify that the correct instructions are generated. The problem is that executing IN and OUT instructions under Win32 will generated an operating system exception. To print to screen, use the Win32 Console API functions. I don't remember what the Win32 sound API is, but that has to be used to generate sound.

There is a solution for calling the IN and OUT instructions under Win32: Inpout32.dll http://www.logix4u.net/inpout32.htm It works seamless with all versions of windows (98/NT/2000/XP). I have succesfully used it in a C++ program of mine. (Don't ask _me_ how to use it in D, I'm still a rookie in the D world... ;) )
Aug 19 2006
parent Kristian <kjkilpi gmail.com> writes:
On Sat, 19 Aug 2006 11:13:53 +0300, Kristian <kjkilpi gmail.com> wrote:

 On Fri, 18 Aug 2006 22:59:05 +0300, Walter Bright  
 <newshound digitalmars.com> wrote:

 Heinz wrote:
 Hi everyone, i've seen many bugs with the inline assembler in D. I've  
 tried
 many functions that work with other assemblers, simple functions such  
 as print
 to screen (i've seen D users complaining about this too).
 I'm writing a block of code to make some sound through the pc speaker.  
 It
 compiles fine but at runtime i get a Win32 Exception.

The problem has nothing to do with the inline assembler. You can use OBJ2ASM to verify that the correct instructions are generated. The problem is that executing IN and OUT instructions under Win32 will generated an operating system exception. To print to screen, use the Win32 Console API functions. I don't remember what the Win32 sound API is, but that has to be used to generate sound.

There is a solution for calling the IN and OUT instructions under Win32: Inpout32.dll http://www.logix4u.net/inpout32.htm It works seamless with all versions of windows (98/NT/2000/XP). I have succesfully used it in a C++ program of mine. (Don't ask _me_ how to use it in D, I'm still a rookie in the D world... ;) )

The Beep() function plays sound through the speaker, but it doesn't work on ME/98/95 (i.e. it does nothing).
Aug 19 2006
prev sibling parent reply Heinz <malagana15 yahoo.es> writes:
"The problem has nothing to do with the inline assembler. You can use
OBJ2ASM to verify that the correct instructions are generated. The
problem is that executing IN and OUT instructions under Win32 will
generated an operating system exception."...Walter.

Thanks Walter for your answer, you're almost right, executing IN and OUT in
Win32
throws exceptions BUT why if i insert this code into TASM it works perfect?

.model small
.stack
.data
string1 db"hello world$"

.code

start:
mov al, 182;
out 43h, al;

mov AX, 4560;
out 42h, AL;
mov AL, AH;
out 42h, AL;

in AL, 61h;
or AL, 3h;
out 61h, AL;

in AL, 61h;
and AL, 252;
out 42h, AL;

end start

This code does not do anything, it's just for testing in/out to ports.

Heinz
Aug 22 2006
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
What version of TASM are you using?  Is it producing an old DOS 
executable or a Win32 one?

-[Unknown]


 "The problem has nothing to do with the inline assembler. You can use
 OBJ2ASM to verify that the correct instructions are generated. The
 problem is that executing IN and OUT instructions under Win32 will
 generated an operating system exception."...Walter.
 
 Thanks Walter for your answer, you're almost right, executing IN and OUT in
Win32
 throws exceptions BUT why if i insert this code into TASM it works perfect?
 
 .model small
 .stack
 .data
 string1 db"hello world$"
 
 .code
 
 start:
 mov al, 182;
 out 43h, al;
 
 mov AX, 4560;
 out 42h, AL;
 mov AL, AH;
 out 42h, AL;
 
 in AL, 61h;
 or AL, 3h;
 out 61h, AL;
 
 in AL, 61h;
 and AL, 252;
 out 42h, AL;
 
 end start
 
 This code does not do anything, it's just for testing in/out to ports.
 
 Heinz

Aug 23 2006
parent reply Heinz <malagana15 yahoo.es> writes:
I've got TASM 2.0 and TLINK 3.01
I think this produces an OLD DOS exe, that's why it works.

 What version of TASM are you using?  Is it producing an old DOS
 executable or a Win32 one?

Aug 27 2006
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Right, so Windows is doing the emulation for you, instead of throwing 
the exception (or more likely in addition to?)

-[Unknown]


 I've got TASM 2.0 and TLINK 3.01
 I think this produces an OLD DOS exe, that's why it works.
 
 What version of TASM are you using?  Is it producing an old DOS
 executable or a Win32 one?


Aug 27 2006