开发者

getch equivalent in assembly language

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 unt开发者_C百科il any key is pressed. I dont have to use the entered key value in further programming.


This is an OS-specific question. For example if you're using Linux, you can issue a read system call like this:

; Allocate some stack buffer...
sub esp, 256

mov eax, 3      ; 3 = __NR_read from <linux/asm/unistd.h>
mov ebx, 0      ; File descriptor (0 for stdin)
mov ecx, esp    ; Buffer
mov edx, 256    ; Length

int 80h         ; Execute syscall.

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 with ah=0. Another would be to install a keyboard interrupt handler. Here's what the BIOS call would look like:

xor ah, ah    ; ah = 0
int 16h       ; call BIOS function 16h

; al now contains an ASCII code.
; ah now contians the raw scancode.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜