| In C ++ builder, The outputb and inputb ports in Turbo C cannot be used to read and write functions. However, there are two other ways to implement this function. This article introduces how to implement port read/write in C ++ builder and provides two methods: Source code . There are two methods to read and write ports in C ++ builder: one is embedded assembly language and the other is using the _ emit _ function. 1. implement port read/write Through Embedded Assembly Language In C ++ builder, Assembly statements must be included in a pair of braces starting with the keyword ASM: ASM { Assembly Statement 1 ...... } Use the embedded assembly language to compile the port output function as follows: Void outport (unsigned short port, unsigned char value) // The port parameter is the output port address, and the value parameter is the output value. { ASM { MoV dx, port // send the port address to the DX register of the processor MoV Al, value // send value to the processor Al register Out dx, Al // send the values in the Al register to the port }; } This function writes the unsigned signed 8-bit data value to the port. The port data type is unsigned short and the 16-bit unsigned short integer. Use the embedded assembly language to compile the port input function as follows: Unsigned char inport (unsigned short port) // The port parameter is the input port address, and the return value is the input value. { Unsigned char value; ASM { MoV dx, port // send the port address to the DX register of the processor In Al, DX // send data from the specified port of DX to the Al register MoV ind, value // assign the value in the Al register to value }; Return Value; // return port data } The function inport reads an unsigned 8-bit bytes data from the port with the port address. Its parameter is only one, that is, the port number. The returned data is of the unsigned char type and is the value read from the port. 2. Use the _ emit _ function to read and write ports. The _ emit _ function is rarely used. The usage is as follows: Void _ emit _ (argument ,...); This function is an internal function of C ++ builder. The called parameter is a machine language instruction. During compilation, it directly embeds machine language commands into the target code without the need for compilation by means of assembly language and Assembly compilation.Program. To use the _ emit _ function, you must be familiar with the machine language commands of the 80x86 processor. If the called parameter is an incorrect machine language instruction, the program will run abnormally and easily cause a crash. Use the _ emit _ function to compile the port output function as follows: Void outport (unsigned short port, unsigned char value) // The port parameter is the output port address, and the value parameter is the output value. { _ Emit _ (0x8b, 0x95, & Port); // send the port address to the edX register of the processor. _ Emit _ (0x8a, 0x85, & value); // send the value to the Al register of the processor. _ Emit _ (0x66, 0xEE); // send the values in the Al register to the port } Use the _ emit _ function to compile the port input function as follows: Unsigned char inport (unsigned short port) // The port parameter is the input port address, and the return value is the input value. { Unsigned char value; _ Emit _ (0x8b, 0x95, & Port); // send the port address to the DX register of the processor. _ Emit _ (0x66, 0xec); // send data from the specified port of DX to the Al register. _ Emit _ (0x88,0x85, & value); // assign the values in the Al register to the value. Return Value; // return port data } The function comments compiled by the two methods show that each of them has the same function, but one is embedded in the assembly language, and the other is directly used in the machine language. 3. Application Example In C ++ builder, create a project from the file/New Application menu. Add two button controls in the form. The caption is "write port" and "read port" respectively ". Copy the outport and inport functions compiled in the first method to the header file of the form, and use these two functions as the public member functions of the Form class. Double-click the two buttons to generate the onclick event function. Add the following to the. cpp file of the unit file:Code: //--------------------------------------------------------------------------- Void _ fastcall tform1: button1click (tobject * sender) { Outport (0 x forward, 0x00); // output data to the port with the 378h address Outport (0 x forward, 0x00); // output data to the port with the 379h address } //--------------------------------------------------------------------------- Void _ fastcall tform1: button2click (tobject * sender) { Int value; Value = inport (0x37a); // read data from the port with the address 37ah } //--------------------------------------------------------------------------- (Note: Port 0x0000-0x37f is the I/O port of the print port. This method can be used to control the printer .) Compile and run. Copy the outport and inport functions compiled by the second method to the header file of the form, and use these two functions as the public member functions of the Form class in the Form class, to replace the outport and inport functions compiled by the first method. Compile and run the program again, and the result is exactly the same. The programming environment of this program is Win98 operating system and C ++ builder5.0.Programming Language. |