In BCB, the __emit__ function can be used to embed binary program code directly into the program, so that some low-level operations can be implemented. Because of the low-level of the direct operating system, this method can cause system instability.
The following is a way to read and write hardware ports using the __EMIT__ function.
Read Port
The port parameter is the input port address and value is the return value.
unsigned char __fastcall INPORTB (unsigned short int port)
{
unsigned char value;
__emit__ (0x8b,0x95,&port); Send the port address to the EDX register
__emit__ (0x66,0xec); Read data from the port into the AL register
__emit__ (0x88,0x85,&value); The value in the AL register is supplemented to
return value;
}
//---------------------------------------------------------------------------
Write Port
Port parameter is an output port address and the value parameter is an output value
void __fastcall OUTPORTB (unsigned short int port,unsigned char value)
{
__emit__ (0x8b,0x95,&port); Send the port address to the EDX register
__emit__ (0x8a,0x85,&value); Send value to the AL register
__emit__ (0x66,0xee); Writes the value in the AL register to the port
}