linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Frank Kotler <fbkotler@comcast.net>
To: glburt@comcast.net
Cc: linux-assembly@vger.kernel.org
Subject: Re: Getting ALT and CTRL keystrokes
Date: Sun, 15 May 2005 14:06:00 -0400	[thread overview]
Message-ID: <42878F88.427D24CD@comcast.net> (raw)
In-Reply-To: 051520051620.1970.428776E8000A45E9000007B222007481849B9D9A0D0409@comcast.net

glburt@comcast.net wrote:
> 
> In Linux Assembly, X86/NASM, how can I find out if a ALT-a or CTRL-a has been pressed?

Well, here's a half-baked attempt to explore that question.
CTRL-A is 1, as expected... CTRL-C or CTRL-Z quit... as does
SysReq (???). CTRL_Q, and a few other combinations don't
return anything(???). ALT- combinations... and function
keys, editing keys, etc. return multiple bytes, starting
with 1Bh (ESC).

Maybe you can have some fun playing with it anyway...

Best,
Frank

; shows hex bytes returned from "getc"
;
; nasm -f elf myprog.asm
; ld -s -o myprog myprog.o

global _start

; misc. equates
%define NL 10
%define STDIN 0
%define STDOUT 1

; sys_calls
%define SYS_EXIT 1
%define SYS_READ 3
%define SYS_WRITE 4
%define SYS_IOCTL 54

; ioctl subfunctions
%define TCGETS		0x5401 ; tty-"magic"
%define TCSETS		0x5402

; flags for 'em
%define ICANON	2	;.Do erase and kill processing.
%define ECHO	8	;.Enable echo.


    struc termios
	alignb 4
	.c_iflag:	resd 1	; input mode flags
	.c_oflag:	resd 1	; output mode flags
	.c_cflag:	resd 1	; control mode flags
	.c_lflag:	resd 1	; local mode flags
	.c_line:	resb 1	; line discipline
	.c_cc:		resb 19	; control characters
    endstruc
;---------------------------------

section .text
_start:
    nop    ; parking place for gdb
top:
    call getc
    call show_al_hex
    jmp top    

exit:
    xor ebx, ebx
    mov eax, SYS_EXIT
    int 80h
;-------------------

;------------------------------
show_al_hex:
    push eax
    push ebx
    push ecx
    push edx    

    mov ebx, 20200000h

    push eax
    shr eax, 4
    and al, 0Fh
    cmp al, 10
    sbb al, 69h
    das
    mov bl, al
    pop eax

    and al, 0Fh
    cmp al, 10
    sbb al, 69h
    das
    mov bh, al
    push ebx
    mov ecx, esp
    mov ebx, STDOUT
    mov edx, 3
    mov eax, SYS_WRITE
    int 80h
    add esp, byte 4

    pop edx
    pop ecx
    pop ebx
    pop eax
    ret
;-----------------------------------

;-----------------------------

getc:
    push ebp
    mov ebp, esp
    
    sub esp, termios_size     ; make a place for current kbd
mode
    
    push edx
    push ecx
    push ebx
    
    mov eax, SYS_IOCTL        ; get current mode
    mov ebx, STDIN
    mov ecx, TCGETS
    lea edx, [ebp - termios_size]
    int 80h
                              ; monkey with it
    and dword [ebp - termios_size + termios.c_lflag],
~(ICANON | ECHO)

    mov eax, SYS_IOCTL
    mov ebx, STDIN
    mov ecx, TCSETS
    lea edx, [ebp - termios_size]
    int 80h
    
    xor eax, eax
    push eax         ; this is the buffer to read into

    mov eax, SYS_READ
    mov ebx, STDIN
    mov ecx, esp     ; character goes on the stack
    mov edx, 1       ; just one
    int 80h          ; do it

                     ; restore normal kbd mode
    or dword [ebp - termios_size + termios.c_lflag], ICANON
| ECHO
    
    mov eax, SYS_IOCTL
    mov ebx, STDIN
    mov ecx, TCSETS
    lea edx, [ebp - termios_size]
    int 80h
    
    pop eax          ; get character into al

    pop ebx          ; restore caller's regs
    pop ecx
    pop edx

    mov esp, ebp     ; leave
    pop ebp
    ret
;-------------------------

      reply	other threads:[~2005-05-15 18:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-15 16:20 Getting ALT and CTRL keystrokes glburt
2005-05-15 18:06 ` Frank Kotler [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42878F88.427D24CD@comcast.net \
    --to=fbkotler@comcast.net \
    --cc=glburt@comcast.net \
    --cc=linux-assembly@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).