⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faskbhit.how

📁 国外网站上的一些精典的C程序
💻 HOW
字号:
 #define FastClearbuf() \ (*(unsigned far*)0x0040001AL = *(unsigned far*)0x0040001CL) #define FastKBhit() \ (*(unsigned far*)0x0040001AL != *(unsigned far*)0x0040001CL)Q: Please explain the logic behind these statements. I have not seen   statements like these before and my textbooks have nothing like this in   them. Is 0x0040001A a long address cast as a far pointer to a pointer? Now   I'm really lost...A: In a PC using DOS (this is obviously *very* environment-specific code!),the BIOS maintains the keyboard buffer in an area of page 40h. The actualy16-bit key codes are stored in a 32-bit cirular buffer using head and tailpointers to point to new characters waiting to be fetched. Using head andtail pointers allows this buffer to be relocated to other areas of RAM and/orresized.  A convention of PC C compilers is that real mode addresses (such as thoseon page 40h) are represented as a concatenation of their segment and offsetaddresses, expressed as a long int. The far pointers to the head and tail ofthe circular buffer are...Head pointer storage location = 0040:001Ah (cast to unsigned long=0x0040001aL)Tail pointer storage location = 0040:001Ch (cast to unsigned long=0x0040001cL)...Since the key codes are all 16 bits wide, these must be cast to farpointers to unsigned ints or unsigned shorts.  Since the keyboard buffer is a circular buffer, in order to quickly tell ifthere are any keypresses waiting, all you need to do is simply compare thevalue of the head and tail pointers. If they're both pointing at the sameplace, there's nothing in the buffer. Similarly, in order to clear thebuffer, simply set them equal to the same value and all pending keypressesdisappear.  Now let's put it all together, using the FastKBhit() macro as an example...First of all, we cast the long int values shown above into far pointers tounsigned ints. We then dereference each to obtain the value located at eachstorage location. Finally, we #define the macro as the inequality of the twovalues, i.e. if they're different, there are one or more keypresses waiting.  Two notes... These are functions rather than macros in later versions ofSNIPPETS, and these will obviously not work in protected or flat mode or any32-bit environment.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -