flat assembler
Message board for the users of flat assembler.

Index > Linux > How to read console input?

Author
Thread Post new topic Reply to topic
maeha



Joined: 14 Feb 2004
Posts: 6
Location: Japan
maeha
Hello,

I am planning to convert my Windows program into Linux by means of fasm.
In Windows, a character from keyboard moves into INPUT_RECORD buffer
by means of ReadConsoleInput(....).

How to implement same function in Linux by means of int 0x80 system call?

Thank you in advance.
maeha
Post 02 Mar 2004, 03:18
View user's profile Send private message Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica
Let's see I think these lines should do it: (I can't check right now as I'm in school)
Code:
mov eax, 3 ; __NR_read (/linux/include/asm/unistd.h)
mov ebx, 0 ; iirc handle 0 is stdin
mov ecx, sizeof_input_buffer ; how many chars to read iirc (or if it was max)
mov edx, buffer ; the address to our input buffer
int 80h    


I think bazik posted some some nice include with lot's of equates, I'll see if I can find it when I get back home.

_________________
... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself.
- Bradley Kuhn
Post 02 Mar 2004, 08:11
View user's profile Send private message Visit poster's website Reply with quote
gorshing



Joined: 27 Jul 2003
Posts: 72
Location: Okla, US
gorshing
Code:
format ELF executable

entry start

section readable writeable
  str1 db 'enter something: '
  str1Len = $-str1
  str2      rb      0x100

start:
  mov eax, 4
  mov ebx, 1
  mov ecx, str1
  mov edx, str1Len
  int 0x80

  mov eax, 3
  mov ebx, 0
  mov ecx, str2
  mov edx, 0x100
  int 0x80

  mov eax, 4
  mov ebx, 1
  mov ecx, str2
  mov edx, 0x100
  int 0x80

  mov eax, 1
  xor ebx, ebx
  int 0x80    

_________________
gorshing
Post 03 Mar 2004, 03:15
View user's profile Send private message Visit poster's website Reply with quote
maeha



Joined: 14 Feb 2004
Posts: 6
Location: Japan
maeha
Hello scientica, gorshing,

Thank you very much for your kind reply.
You are right!
But my explanation was not enough due to language ability.
I think that the function of ReadConsoleInput(...) in Windows is
different from "int 0x80 with mov eax,3" you shown.

Becaue, ReadConsoleInput(...) returns to the main when single key touching
and sends data into particular structure in INPUT_RECORD buffer.
The structure KEY_EVENT_RECORD contains not only AsciiChar but also
KeyCode, ScanCode, ControlKeyState, etc.
Mouse movement is also detected and its data is sent into MOUSE_EVENT_RECORD.

I think that it is possible to implement same function in Linux.
But I don't know now.

Regards,
maeha
Post 04 Mar 2004, 12:09
View user's profile Send private message Reply with quote
andyz74



Joined: 26 Nov 2007
Posts: 36
Location: Germany
andyz74
I have also a problem with reading from keyboard.
I have written this code...
Code:

format ELF executable
entry start

segment readable writeable executable

start:

  mov     eax,4
       mov     ebx,1
       mov     ecx,msg
     mov     edx,msg_size
        int     80h
 
    mov     eax,3
       mov     ebx,1
       mov     ecx,inp_buf
 mov     edx,100h
    int     80h
 
    mov     [inp_buf_size],ax
   
    
    mov     eax,4
       mov     ebx,1
       mov     ecx,msg2
    mov     edx,msg2_size
       int     80h
 
    mov     eax,4
       mov     ebx,1
       mov     ecx,inp_buf
 xor     edx,edx
     mov     edx,inp_buf_size
    int     80h

     mov     eax,4
       mov     ebx,1
       mov     ecx,crlf
    mov     edx,1
       int     80h

     mov     eax,1
       xor     ebx,ebx
     int     80h


segment readable writeable

crlf db 0ah

msg db 'Input some data: '
msg_size = $-msg

inp_buf rb 100h
inp_buf_size rw 1

msg2 db 'You entered: '
msg2_size = $-msg2

    


...now my keyboard input is stored to the var "input_buf", and the length of the input gets from register ax to var "inp_buf_size".

After that, I expected, I can output the text with correct length via syscall 4, but the length of my string is never correct, it is always much too long.
I hope, you understand my poor english and my problem and anybody can explain me the error.

with kindest reagrds, Andy
Post 15 Sep 2010, 14:13
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17248
Location: In your JS exploiting you and your system
revolution
Are you sure that Linux expects inp_buf_size as a word sized value? Maybe you can try using a dword (inp_buf_size rd 1) and storing eax in there.

[edit] Also use mov edx,[inp_buf_size] with the square brackets.
Post 15 Sep 2010, 14:22
View user's profile Send private message Visit poster's website Reply with quote
andyz74



Joined: 26 Nov 2007
Posts: 36
Location: Germany
andyz74
Tested it right now as double and with EAX...same error. Sad

/edit: The square brackets!!! Holy shit. now it works..., but why? The other lengths of the variables did work without the brackets?

btw: thanks! Smile
Post 15 Sep 2010, 14:28
View user's profile Send private message Visit poster's website Reply with quote
jeff



Joined: 07 Apr 2005
Posts: 28
jeff
I'm not sure if this is a problem, but some keys create up to 7 characters when pressed. If a key is held down, it repeats and quickly fills up a buffer. Also, it was necessary for me to detect the end of each key so the next key could be decoded. For the console this logic worked OK, but when terminals were used, things got complicated. I'm still somewhat confused by all this and have started moving my programs to Xwindow keyboard handling. Hope this helps.. jeff
Post 11 Dec 2010, 23:22
View user's profile Send private message Reply with quote
andyz74



Joined: 26 Nov 2007
Posts: 36
Location: Germany
andyz74
A few days ago i wanted to do some inputs (maybe around 8 or 10) with

Quote:

mov eax,3
mov ebx,0
mov ecx,buffer
mov edx,bufferlength
int 80h


and had also the problem, that all keys were working fine, but when you pressed "enter" for finishing the first input, than "enter" was read several times, so the next few "eax=3/int80h" were already executed and produced empty strings.

I think, one day, i will create my own "read-string-procedure",
with some construction like

Quote:

readkey
repeat readkey until NOT keypressed.
add readkey_char to string
until char is "enter"



...but this is in future... Smile
Post 12 Dec 2010, 07:58
View user's profile Send private message Visit poster's website Reply with quote
jeff



Joined: 07 Apr 2005
Posts: 28
jeff
Hi andyz74, I assume you know about termios setting up for raw or cooked mode of keyboard entry? Another complication is signals, but most people leave signals in the default state and will not have problems. One thing that may be useful is to look at keyecho.asm in asmtools. It echos the output of each key press to the display and is written in assembler.
Post 12 Dec 2010, 18:12
View user's profile Send private message Reply with quote
catafest



Joined: 05 Aug 2010
Posts: 120
catafest
jeff wrote:
Hi andyz74, I assume you know about termios setting up for raw or cooked mode of keyboard entry? Another complication is signals, but most people leave signals in the default state and will not have problems. One thing that may be useful is to look at keyecho.asm in asmtools. It echos the output of each key press to the display and is written in assembler.

Hi.
keyecho.asm is not in Linux FASM ...
Also , I thinking the problem is :
Code:
mov     [inp_buf_size],ax     

and how data is stored ...
Maybe is need to use push or pop to save the data and use it.
Post 07 Jan 2012, 12:12
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
Agguro



Joined: 16 Feb 2012
Posts: 1
Agguro
i want to write a program that waits until a user presses a key and then the program continues. In my code i can press a key but i must press enter to continue.
I hope i'm a bit clear with my explanation here, but it's the same as in c++ repeat unti keypressed.
I need to use the linux syscall for that (and nope it's no homework, i'm 45 and long way past college)
Post 16 Feb 2012, 21:17
View user's profile Send private message Reply with quote
Lurker



Joined: 07 Feb 2012
Posts: 6
Lurker
0xA is '\n'
The following code should flassemble fine
Code:
;do{read(0,1,&buffer)}while(buffer!='\n');exit(); 
format ELF executable 3 
segment readable executable 

etiquette: 

mov eax,3 
xor ebx,ebx 
mov ecx,1 
mov edx,buffer 
int 80h 

test [buffer],0xA 
jnz etiquette 

mov eax,1
xor ebx,ebx 
int 80h 

segment writeable readable 
buffer rb 1    
Post 17 Feb 2012, 02:24
View user's profile Send private message 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-2020, Tomasz Grysztar.

Powered by rwasa.