CB and Assembly Hybrid programming

Source: Internet
Author: User

I ran into a question when I was writing a timer: How to make a sound? I started with the 32-bit Windows API function MessageBeep (-1); The sound was small and harsh. The original set of PlaySound functions in the 16-bit Windows API was canceled in 32-bit Windows, and the Sound function in DOS was long overdue.

Fortunately, I know the hardware is also known, the PC Speaker sound is through the system timer count chip 8253/8254 generated, as long as the hardware port access to the chip can produce the desired sound. The problem is that Windows works in protected mode, most hardware ports are in privilege level 0 (PL0, this is the hardware people say, later I know in the OS and Driver is called Ring 0, this is more correct, because if not Intel's CPU may not be called PL), that is, operating system nuclear mentality, can access (such as the hard drive, access is not error, but the results are not correct), which means to write the form of the driver, God! VxD and WDM I will not, what to do? In fact, not so difficult, like the PC Speaker this Shita port, Windows is not protected, that is, in the user state can also be normal access.

There is also a question of what statements are used to access the port? DOS in the C language of the several port operation functions are canceled in Windows, have to use the assembly. I started by inserting an ASM statement into the assembly code, and found that BCB at compile-time with ASM would compile the BCB file into a large ASM file, and then restart the assembler assembly, too slowly. Finally, I used the DOS programming method, do a separate ASM file into the project file.

Here are two functions for voice, the first declaration of two external C called functions, is the two bytes of the byte port input/output function, Note: In C + + must note that the external function should be called C call form. There are many forced type conversions in the program to avoid warnings, and I've always asked the program to error/warning/hint all 0.

extern "C" {
Byte InPortB( int aPort );
void OutPortB( int aPort, Byte aValue );
}
void __fastcall Sound( int aFreq )
{
if ( ( aFreq >= 20 ) && ( aFreq <= 20000 ) )
{
aFreq = 1193181 / aFreq;
Byte b = InPortB( 0x61 );
if ( ( b & 3 ) == 0 )
{
OutPortB( 0x61, Byte( b | 3 ) );
OutPortB( 0x43, 0xb6 );
}
OutPortB( 0x42, ( Byte )aFreq );
OutPortB( 0x42, ( Byte )( aFreq >> 8 ) );
}
}
void __fastcall NoSound( void )
{
Byte b = Byte( InPortB( 0x61 ) & 0xfc );
OutPortB( 0x61, b );
}

The following is a two-port I/O function of the assembly source, that is, timed reminders (Alarm) in the Ioportb.asm file, is the BCB produced by the ASM file based on a little bit of optimization. Attention:

1, the first 386p essential, designated with 32-bit protection mode, as for modal flat I also do not understand is What, and 16-bit tiny, small ... Different, probably refers to using 32-bit protection mode of flat address between the model bar;

2, in 32-bit protection mode, the CS/IP is 32 bits, the parameters in the stack position and 16-bit difference;

3, the final public is also not small, the prefix is also necessary to underline, and also remember to use the case-sensitive assembly.

.386p
model flat
_TEXT segment dword public use32 ''CODE''
_InPortB proc near
push ebp
mov ebp, esp
mov dx, word ptr [ebp + 8]
in al, dx
pop ebp
ret
_InPortB endp
_OutPortB proc near
push ebp
mov ebp, esp
mov dx, word ptr [ebp + 8]
mov al, byte ptr [ebp + 12]
out dx, al
pop ebp
ret
_OutPortB endp
public _InPortB
public _OutPortB
_TEXT ends
end

Note: This method does not work on Windows NT because Windows NT protects all ports, must be in WDM, does not even have a VxD, it is only for Windows 95 and can be used in Windows 98, but Windows NT and Windows 2000 are not supported.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.