Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getch equivalent in assembly language

Tags:

c++

c

assembly

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.

like image 231
Farid-ur-Rahman Avatar asked Dec 21 '22 08:12

Farid-ur-Rahman


2 Answers

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.
like image 71
asveikau Avatar answered Dec 24 '22 02:12

asveikau


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.

like image 35
hammar Avatar answered Dec 24 '22 01:12

hammar