flat assembler
Message board for the users of flat assembler.

Index > Linux > Unbuffered input with ioctl, termios not working

Author
Thread Post new topic Reply to topic
z33ky



Joined: 24 May 2010
Posts: 5
Location: Germany
z33ky 24 May 2010, 12:31
Hello there. I'm new to FASM and new around here Smile
I learned an assembler in school, for the myAVR micro-controller, and I thought it'd be fun to learn an assembly language for the average desktop computer.

I've completed a small tutorial and read some parts of the FASM manual, but I can't get my head around this problem:
I'm trying to get unbuffered input from stdin (without a library) and apparently the ioctl syscall should be working for this, but it just breaks the terminals stdin.
Here's the assembly I am using:
Code:
format elf executable

entry main

;syscalls
SYSCALL_EXIT    equ 1
SYSCALL_READ    equ 3
SYSCALL_IOCTL   equ 54

;standard I/O
STDIN   equ 0

;ioctl subfunctions
TCGETS  equ 0x5401
TCSETS  equ 0x5402

;struct termios
struc termios{
        .c_iflag rd 1 ;input mode flags
        .c_oflag rd 1 ;output mode flags
        .c_cflag rd 1 ;control mode flags
        .c_lflag rd 1 ;local mode flags
        .c_line rb 1 ;line discipline
        .c_cc rb 32 ;control characters
        .c_ispeed rd 1 ;input speed
        .c_ospeed rd 1 ;output speed
}

;termios flags
ICANON equ 2 ;Do erase and kill processing
ECHO equ 8 ;Enable echo

;static data
segment readable writeable

oldtermios termios
newtermios termios

;--------
segment executable
main:
        ;get old termios mode
        mov eax, SYSCALL_IOCTL
        mov ebx, STDIN
        mov ecx, TCGETS
        mov edx, oldtermios

        ;copy termios
;       mov esi, oldtermios
;       mov edi, newtermios
;       mov ecx, 14
;       repz movs dword [edi], [esi]
;       repz movs byte [edi], [esi]

        ;set new termios
;       and dword [newtermios.c_lflag], not (ICANON or ECHO)
        mov eax, SYSCALL_IOCTL
        mov ebx, STDIN
        mov ecx, TCSETS
        mov edx, oldtermios ;newtermios
        int 0x80

        ;get input
        mov eax, SYSCALL_READ
        mov ebx, STDIN
        mov ecx, esp
        mov edx, 1
        int 0x80

        ;reset termios
        mov eax, SYSCALL_IOCTL
        mov ebx, STDIN
        mov ecx, TCSETS
        mov edx, oldtermios
;       int 0x80

        ;exit
        mov eax, SYSCALL_EXIT
        mov ebx, 0
        int 0x80    

I am actually just getting the current terminal settings and set them again without modification; In the commented code you can see my intention after I get that working.

Upon executing the program it immediately closes without waiting for input and I cannot input anything further in the terminal.

Here's the struct as in /usr/include/bits/termios.h
Code:
typedef unsigned char   cc_t;
typedef unsigned int    speed_t;
typedef unsigned int    tcflag_t;

#define NCCS 32
struct termios
  {
    tcflag_t c_iflag;           /* input mode flags */
    tcflag_t c_oflag;           /* output mode flags */
    tcflag_t c_cflag;           /* control mode flags */
    tcflag_t c_lflag;           /* local mode flags */
    cc_t c_line;                        /* line discipline */
    cc_t c_cc[NCCS];            /* control characters */
    speed_t c_ispeed;           /* input speed */
    speed_t c_ospeed;           /* output speed */
#define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
#define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
  };    
Post 24 May 2010, 12:31
View user's profile Send private message Visit poster's website Reply with quote
z33ky



Joined: 24 May 2010
Posts: 5
Location: Germany
z33ky 24 May 2010, 21:09
Alright, fixed.
I was missing an int 0x80 after the first SYSCALL_IOCTL :X
Post 24 May 2010, 21:09
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.