Thread overview
Buggy inline assembler
Aug 18, 2006
Heinz
Aug 18, 2006
Walter Bright
Aug 19, 2006
Kristian
Aug 19, 2006
Kristian
Aug 23, 2006
Heinz
Aug 28, 2006
Heinz
August 18, 2006
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.
August 18, 2006
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.
August 19, 2006
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... ;) )
August 19, 2006
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).
August 23, 2006
"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
August 23, 2006
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
August 28, 2006
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?
August 28, 2006
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?