汇编语言中的 getch 等效项
我正在用汇编语言编程,用 C++ 编写 x86,我需要知道汇编语言而不是 C++ 语言中的 getch 等效项,因为我不想使用 C++ 编程语言中的任何函数。
我在网上找到了代码,但它将给定值保存到变量并用 C++ 创建。我只想使用该功能来停止程序,直到按下任意键为止。我不必在进一步编程中使用输入的键值。
I am prigramming in assembly Language, x86 in C++ and I need to know the getch equivalent in assembly language instead of C++ language as I dont want to use any function from the C++ programming language.
I found the code on the web but it saves the given value to a variable and created in C++. I want to use the function only to stop the program until any key is pressed. I dont have to use the entered key value in further programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个特定于操作系统的问题。例如,如果您使用的是 Linux,则可以像这样发出
read
系统调用:这实际上是在 x86 Linux 上发出系统调用的更过时的方法,但这是我一开始就知道的方法我的头。还有速度更快的
sysenter
。并且有一个映射到每个进程的共享库,它根据您的硬件支持抽象系统调用接口。 (linux-gate.so
。)作为一种品味问题,我反对从汇编语言进行系统调用的方法。从 C 语言中执行此操作要简单得多。对于绝对需要使用汇编语言的内容调用汇编代码(或生成内联汇编)。但一般来说,对于大多数实际问题,您会发现最好不要浪费时间讨论汇编中的操作系统接口。 (除非这是一个学习练习,在这种情况下我完全赞成。)
更新:您在对 @Anders K. 的评论中提到您正在编写一些要放在 MBR 上的内容。您应该在问题的前面提到这一点。一种方法是使用 BIOS 调用:我相信您想要的是
int 16h
和ah=0
。另一种方法是安装键盘中断处理程序。 BIOS 调用如下所示:This is an OS-specific question. For example if you're using Linux, you can issue a
read
system call like this:This is actually the more antiquated way to issue a syscall on x86 Linux, but it's the one I know off the top of my head. There's also
sysenter
which is faster. And there is a shared library mapped into every process that abstracts the syscall interface based on what your hardware supports. (linux-gate.so
.)As a matter of taste, I would argue against the approach of doing system calls from assembly language. It is much simpler to do it from C. Call into the assembly code (or produce inline assembly) for the stuff that absolutely needs to be in assembly language. But in general for most practical matters you will find it better not to waste your time talking to OS interfaces in assembly. (Unless it's a learning exercise, in which case I'm all for it.)
Update: You mentioned as a comment to @Anders K. that you are writing something to put on the MBR. You should have mentioned this earlier in the question. One approach is to use a BIOS call: I believe the one you want is
int 16h
withah=0
. Another would be to install a keyboard interrupt handler. Here's what the BIOS call would look like:查看您之前的问题,我假设这是针对您的引导加载程序的。在这种情况下,您没有任何可用的操作系统系统调用,因此您必须依赖 BIOS。特别是,
int 16h
可用于访问 IBM 兼容 PC 上的基本 BIOS 键盘服务。Looking at your previous questions, I'm assuming this is for your boot loader. In that case, you do not have any OS system calls available, so you'll have to rely on the BIOS. In particular,
int 16h
can be used to access basic BIOS keyboard services on IBM-compatible PCs.这可能不是您想要的,但为什么不在编译 C++ 代码后查看程序集列表呢?大多数编译器都有 asm 输出作为选项。
This may not be what you are looking for but why don't you just look at the assembly listing after compiling the c++ code? Most compilers have asm output as an option.
确实没有这样的事情。 `getch' 只是标准 C RTL 的一部分;它是库中围绕一个或多个系统调用的一些 C 代码,具体取决于您运行的系统。没有定义处理器指令来表示“从用户正在键入的位置读取字符”。你可以学习从汇编调用CRTL例程,也可以研究相应的系统调用并学习调用它们。
There's not really such a thing. `getch' is just a piece of the standard C RTL; it is some C code in the library surrounding one or more system calls, depending on the system you are running on. There is no processor instruction defined to mean 'read a character from wherever some user is typing.' You can learn to call the CRTL routine from assembly, or you can research the corresponding system calls and learn to call them.
对于引导加载程序,您不应该使用像 getch 这样的调用。如果您需要一次处理一个字符的缓冲区,那么编写一个循环,一次一个字节地执行。如果您绝对必须与控制台交互,请编写一个 C++ 控制台程序,在调试器中单步调试它,并查看运行时库执行了哪些代码。它可能是一条中断指令,并且可能在中断指令之前设置了参数。在 PC 上,中断将调用 BIOS 来执行实际的控制台 IO。
For a boot loader, you should not be using calls like getch. If you need to process a buffer one character at a time, then write a loop that steps through, one byte at a time. If you absolutely must interact with the console, then write a C++ console program, step through it in the debugger, and see what code is executed by the runtime library. It will likely be an interrupt instruction and may have arguments set up before the interrupt instruction. On a PC the interrupt will call into the BIOS to do the actual console IO.