C Builder implements port reading and writing Reprinted from "Computer World Daily" (Wen / Lu Jun Zhang Jian Zhang Jian) --- This article describes how to implement port reading and writing under C Builder and give a specific example. __Emit__ function introduction __emit__ function is generally used to use, by using C Builder's Help menu, online help information can be obtained. ----__ Emit__ function usage is: void __emit __ (argument, ...); ---- This function is a C Builder an internal function, the argument called the machine language instruction. It is compiled, and the machine language instruction is directly embedded in the target code, and it is not necessary to use the assembly language and assembly compiler. In Borland C , its prototypes illustrate in the header file, and C Builder's compiler can automatically know it without having to join the header file. ---- This function has no return value. ---- The following uses this function to define an InportB and Outportb function, enabling it with the functionality of the InportB and Outportb functions in Borland C . Void Outportb (unsigned color int port, unsigned char value) // port parameter is the output port address, // value parameter is the output value {__Emit __ (0x8b, 0x95, & port); // Send port addresses to processor 32-bit EDX Register __emit __ (0x8a, 0x85, & value); // Send Value to the processor 8-bit Al register __emit __ (0x66, 0xee); // Send the value in the Al register to the port} --- This Outportb function After calling, send the value specified by the value parameter to the port specified by the Port parameter. UNSIGNED CHAR INT Port // Port Parameters is the input port address {unsigned char value; // Specify variable value to unsigned characters __Emit __ (0x8b, 0x95, & port); // Put the port address to the processor 32-bit EDX registers __emit __ (0x66, 0xec); // Send a data byte from the specified port to 8-bit Al registers __emit __ (0x88, 0x85, & value); // Assign the value in the Al register to Value Return Value; // Return Function Value} - After this InportB function is called, read and return a byte from the port specified by the port parameter. ---- If the user wants to use the __emit__ function, you must be familiar with the 80x86 processor's machine language instruction. If the calling parameter is an error machine language instruction, the program will be absent, and it is easy to cause a crash.
Application Example ---- The programming environment of this program is a Win98 operating system and a C Builder 4.0 programming language. ---- Start C Builder 4.0, activate the menu file / new application, create a project. Add two Button controls on your form. Each object property is set as follows: Component Name Property Property Value Table Mement Name Form1 CAPTION Read Write Port Command Button Name Button1 CAPTION Write Port Command Button Name Button2 CAPTION Read Port ---- In unit files. h file to add the following code: public: // declaration inportb and outportb function of public member functions void __fastcall outportb (unsigned short int port, unsigned char value) {__emit __ (0x8b, 0x95, & port); __emit __ (0x8a, 0x85, & value) __EMIT __ (0x66, 0xee);} // ------------------_ (0x8b, 0x95, & port); __Emit__ (0x66, 0xec); __emit __ (0x88, 0x85, & value); return value;} --- You can also put the implementation of the InportB and Outportb functions in unit files. In the CPP file. C Builder supports member functions implementation in c standards outside the class or categories. ---- Double-click the two Button controls to generate an OnClick event function.