0

I'm trying to create an operating system with Assembly and C (not C++) and I have some files that write text to the screen. However, I need to detect if a key is pressed on the keyboard. I am using Windows 10 and Visual Studio Code as the code editor.


I tried using iostream's std::cin.get() but gcc returns the error fatal error: iostream: No such file or directory.

I tried with conio.h's getch() and _getch() but gcc returns fatal error: conio.h: No such file or directory.


The OS is in 64-bit protected mode.

mine_greg
  • 16
  • 5
  • 3
    These functions access the keyboard via the operating system. If you are *writing* the operating system then you can't use them. Probably you are using `-ffreestanding` or some similar option that, quite rightly, tells the compiler not to search headers that are part of the standard library (which requires an OS). – Nate Eldredge Aug 22 '23 at 13:34
  • 1
    So for this question, you'll have to explain more about what your OS can and can't do at this point. Are you in 16-bit, 32-bit, 64-bit mode? Do you have access to a BIOS? Is your OS supposed to run on standard PC hardware, or something else? – Nate Eldredge Aug 22 '23 at 13:35
  • @NateEldredge I do use `--freestanding` to compile the code with gcc. – mine_greg Aug 22 '23 at 13:38
  • Have you considered monitoring keyboard via your [HID](https://stackoverflow.com/a/17679571/645128) device? – ryyker Aug 22 '23 at 13:39
  • @NateEldredge the OS is in 64-bit protected mode so I can't directly use BIOS interrupts. – mine_greg Aug 22 '23 at 13:39
  • @ryyker no, how do you do that? – mine_greg Aug 22 '23 at 13:41
  • Click the link in my last comment for a very introductory level look, then do some more Googling. – ryyker Aug 22 '23 at 13:51
  • Do you want to treat the keyboard as an actual USB device, or as a legacy PS/2 keyboard which it can emulate? – Nate Eldredge Aug 22 '23 at 13:53
  • 1
    You tagged this [tag:c], but you mention `iostream`. Did you mean to tag this [tag:c++]? – ikegami Aug 22 '23 at 13:55
  • @ikegami - If he is writing an OS, `iostream` will not be available. But still, from OP's latest edit, tag should apparently read `C++`. – ryyker Aug 22 '23 at 14:02
  • 1
    There is no notion of key presses in C or C++, and even if there were, you are at a too low level to use these high-level facilities in your environment, which is likely to have ZERO I/O support from the C or C++ standard library. You need to work directly with your hardware. – n. m. could be an AI Aug 22 '23 at 14:04
  • @n.m.couldbeanAI - _"There is no notion of key presses in C"_. What does _no notion_ mean here? Don't functions such as GetAsyncKeyState introduce a notion of key presses? – ryyker Aug 22 '23 at 14:10
  • @ryyker C and C++ are languages defined by their respective standards, which do not have a notion of a key press. `GetAsyncKeyState` is a facility provided by a specific OS, not a part of any language. – n. m. could be an AI Aug 22 '23 at 14:23
  • Maybe pick a less ambitious progress until you have learnt the basics of how operative systems and programming standard libraries work and interact with each other. And obviously you cannot develop an OS in a hosted systems compiler. – Lundin Aug 22 '23 at 14:24
  • @ryyker, Re "*If he is writing an OS, `iostream` will not be available.*", I know, and that's irrelevant to my question. No idea why you're telling me that. The question stands: OP, are you using C or C++? – ikegami Aug 22 '23 at 14:32
  • You may want to start reading [this](https://stackoverflow.com/questions/34561275/setting-up-interrupts-in-protected-mode-x86) for example. – n. m. could be an AI Aug 22 '23 at 14:36
  • @Lundin "you cannot develop an OS in a hosted systems compiler" Where can I buy a freestanding compiler? Is there an online store that sells those? – n. m. could be an AI Aug 22 '23 at 14:38
  • @ryyker I am using C. – mine_greg Aug 22 '23 at 17:23
  • @n.m.couldbeanAI There's plenty: IAR, Keil, Greenhills and so on. Alternatively you can often use a gcc build for the target system and compile with the appropriate settings for the specific part, as well as `-ffreestanding`, `-fno-strict-aliasing` and so on. – Lundin Aug 23 '23 at 06:27
  • @Lundin I wonder which of those were used to compile the OS you typed your response on. – n. m. could be an AI Aug 23 '23 at 09:42
  • @n.m.couldbeanAI Call Bill Gates and ask him? Or alternatively in Linux, it was definitely gcc in freestanding mode, without the regular crt and standard lib, linked as raw binary and not an executable. – Lundin Aug 23 '23 at 11:07

0 Answers0