This question already has answers here:
How do I discard user input delivered during sleep function?
(3 answers)
Prevent reading of previous / prior user keyboard input from sys.stdin, that works with Click
(1 answer)
Closed 2 years ago.
I want to drain terminal input before prompting the user, or otherwise to close the terminal input until I want to prompt the user for input.
I am writing a program that uses sys_read to prompt the user for input through the terminal. If a user types characters before being prompted, those characters get included in the input. I can easily drain every unread character after the prompt using sys_read again, but it relies on a return character being at the end of the stream of input to prevent prompting the user more than once (I am assuming the user ends every prompt with the return key). I can't rely on the presence of a return character before the prompt, so I can't drain the input the same way.
I have also tried closing stdin with sys_close, but I can't figure out how to open terminal input again, so the program is left frozen when a prompt comes up. Even if I could open the terminal input again, I'm not sure whether or not characters typed while it's closed still get saved for the next time it's read, which would render this approach completely useless.
If there is some sort of obscure termios flag that could disable user input, that would also be an excellent solution.
Here's a program, for example:
global _start
_start:
mov edx,ldot
mov ecx,mdot
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],1
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,ldesire
mov ecx,mdesire
mov ebx,1
mov eax,4 ; sys_write
int 0x80
call input ; here's the call...
mov edx,lreceive
mov ecx,mreceive
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],1
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,99
mov ecx,inputed
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov eax,1 ; sys_exit
int 0x80 ; the end!
input: ; here is all that I am concerned about
mov edx,99
mov ecx,inputed
mov ebx,0 ; stdin
mov eax,3 ; sys_read
int 0x80
cmp eax,99 ; check for valid input
jl inputdone
cmp byte[inputed+98],0xa
je inputdone
inputclear: ; drain
mov eax,3
int 0x80
cmp eax,99
jl inputerror
cmp byte[inputed+98],0xa
je inputerror
jmp inputclear
inputerror:
mov eax,-1
ret
inputdone:
mov eax,1
ret
section .bss
inputed resb 99
section .data
sleep:
sleep_sec dd 0
sleep_usec dd 0
mdot db '...',0xA
ldot equ $ - mdot
mdesire db 'tell me your truest desire:'
ldesire equ $ - mdesire
mreceive db 'you shall receive... '
lreceive equ $ - mreceive
A user who does things when they are told will find no trouble:
...
tell me your truest desire:money
you shall receive... money
But a user who likes to press buttons as they please will struggle:
...
a gtell me your truest desire:love
you shall receive... a glove
And the program won't wait for anyone who makes a mess with the return key:
...
who
tell me your truest desire:you shall receive... who
Related
This question already has answers here:
Read STDIN using syscall READ in Linux: unconsumed input is sent to bash
(2 answers)
Closed 4 months ago.
section .data
yourinputis db "your input is =",0
len equ $ - yourinputis
section .bss
msginput resb 10
section .text
global _start
_start:
mov eax,3 ;read syscall
mov ebx,2
mov ecx,msginput
mov edx,9 ; I don't know that is correct?
int 80h
mov eax,4 ;write syscall
mov ebx,1
mov ecx,yourinputis
mov edx,len
int 80h
mov eax,4 ;write syscall
mov ebx,1
mov ecx,msginput
mov edx,10
int 80h
exit:
mov eax,1 ;exit syscall
xor ebx,ebx
int 80h
This code working very well. But It is so terrible bug(for me:(). If I enter an input longer than 10 --->
$./mycode
012345678rm mycode
your input is 012345678$rm mycode
$
This is happening. And of course "mycode" is not exist right now.
What should I do?
EDIT:The entered input is correctly printed on the screen. But if you enter a long input, it moves after the 9th character to the shell and runs it.
In the example, the "rm mycode" after "012345678" is running in the shell.
If you enter more than 9 characters, they're left in the terminal driver's input buffer. When the program exits, the shell reads from the terminal and tries to execute the rest of the line as a command.
To prevent this, your program should keep reading in a loop until it gets a newline.
You can read the characters one by one until you reach 0x0a. Something like:
_read:
mov esi, msginput
_loop:
mov eax,3 ;read syscall
mov ebx,0
mov ecx, esi
mov edx,1 ; I don't know that is correct?
int 80h
cmp byte[esi], 0x0a
je end
inc esi
jmp _loop
end:
ret
You would have to increase the size of msginput tho.
IMPORTANT: Do note that this is not the efficient way to do this (see the comments), it is only put here as an example to the answer above.
This question already has answers here:
How do i read single character input from keyboard using nasm (assembly) under ubuntu?
(3 answers)
Reading input from keyboard with x64 linux syscalls (assembly)
(1 answer)
How to stop echo in terminal using c?
(1 answer)
Closed 2 years ago.
Is there a way to disable and enable the display of terminal input within an assembly program?
I am writing a command line program in nasm where user input it periodically read from the terminal (which I want to be displayed) and the program sleeps for certain lengths of time. During that time (unlike what I've found in the Windows command line) the user is still able to type in the terminal. I want to hide that text.
Here's a program that asks for a prompt and waits between certain message:
section .text
global _start
_start:
mov edx,lhello
mov ecx,mhello
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],1
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,lwrite
mov ecx,mwrite
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov edx,99
mov ecx,usergift
mov ebx,0
mov eax,3 ; sys_read
int 0x80
mov edx,lsleep
mov ecx,msleep
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],2
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,ldone
mov ecx,mdone
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov eax,1 ; sys_exit
int 0x80 ; the end!
section .bss
usergift resb 99
section .data
sleep:
sleep_sec dd 0
sleep_usec dd 0
mhello db 'hello-',0xA
lhello equ $ - mhello
mwrite db 'give me something:'
lwrite equ $ - mwrite
msleep db 'wait for 2 seconds...',0xA
lsleep equ $ - msleep
mdone db 'thank you.',0xA
ldone equ $ - mdone
gives the following output when love is inputted by the user when prompted-
hello-
give me something:love
wait for 2 seconds...
thank you.
but the user is also able to write outside of the prompt-
hello-
ha!give me something:love
wait for 2 seconds...
lies!thank you.
This question already has answers here:
X86 NASM Assembly converting lower to upper and upper to lowercase characters
(5 answers)
X86 Assembly Converting lower-case to uppercase
(1 answer)
Closed 3 years ago.
I want to change the string to all caps, although I am having trouble getting the length of the input. What i have tried so far is moving the address of the message into a registrar then indexing through the string and also increment a counter variable. Then comparing the char in the address to a '.' (signifying the end of the message) and if its found not to be equal it will recall this block of statements. At least this is what I want my code to do. Not sure if this is even the right logic. I know there are alot of errors and its messy but I'm learning so please just focus on my main question. thank you! EDIT: the input i use is 'this is a TEST.'
;nasm 2.11.08
SYS_Write equ 4
SYS_Read equ 3
STDIN equ 0
STDOUT equ 1
section .bss
message resb 15
counter resb 2
section .data
msg1: db 'Enter input (with a period) that I will turn into all capitals!',0xa ;msg for input
len1 equ $- msg1
section .text
global _start
_start:
mov eax, SYS_Write ; The system call for write (sys_write)
mov ebx, STDOUT ; File descriptor 1 - standard output
mov ecx, msg1 ; msg to print
mov edx, len1 ; len of message
int 0x80 ; Call the kernel
mov eax, SYS_Read ;system call to read input
mov ebx, STDIN ;file descriptor
mov ecx, message ;variable for input
mov edx, 15 ;size of message
int 0x80 ;kernel call
mov [counter], byte '0'
getLen:
mov eax, message
add eax, [counter]
inc byte [counter]
cmp eax, '.'
jne getLen
mov eax, SYS_Write ; this is to print the counter to make sure it got the right len
mov ebx, STDOUT
mov ecx, counter
mov edx, 2
int 0x80
jmp end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov eax, [message]
;add eax, counter
cmp eax, 90
jg toUpper
toUpper:
sub eax, 32
mov [message], eax
mov eax, SYS_Write ; The system call for write (sys_write)
mov ebx, STDOUT ; File descriptor 1 - standard output
mov ecx, message ; Put the offset of hello in ecx
mov edx, 10 ; helloLen is a constant, so we don't need to say
int 0x80 ; Call the kernel
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
end:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 0x80 ;
Recently, I wrote a bit of assembly code that asks for the password and if the user enters the correct password as stored internally, it prints out "Correct!". Else, it prints out "Incorrect!".
Here is the code:
section .text
global _start
_start:
mov edx, len_whatis
mov ecx, whatis
mov ebx, 1
mov eax, 4
int 80h ; outputs: "What is the password?"
mov edx, 5 ; expect 5 bytes of input(so 4 numbers)
mov ecx, pass
mov ebx, 0
mov eax, 3
int 80h ; accepts intput and stores in pass
mov eax, [pass] ; move the pass variable into eax
sub eax, '0' ; change the ascii number in eax to a numerical number
mov ebx, [thepass] ; move the thepass variable into ebx
sub ebx, '0' ; change the ascii number in ebx to a numerical number
cmp eax, ebx ; compare the 2 numbers
je correct ; if they are equal, jump to correct
jmp incorrect ; if not, jump to incorrect
correct:
mov edx, len_corr
mov ecx, corr
mov ebx, 1
mov eax, 4
int 80h ; outputs: "Correct!"
mov ebx, 0
mov eax, 1
int 80h ; exits with status 0
incorrect:
mov edx, len_incor
mov ecx, incor
mov ebx, 1
mov eax, 4
int 80h ; outputs: "Incorrect!"
mov eax, 1
int 80h ; exits with status: 1
section .data
whatis db "What is the password?", 0xA
len_whatis equ $ - whatis
thepass db "12345"
corr db "Correct!", 0xA
len_corr equ $ - corr
incor db "Incorrect!", 0xA
len_incor equ $ - incor
section .bss
pass resb 5
Assemble:nasm -f elf password.s
Link:ld -m elf_i386 -s -o password password.o
(If you did try to assemble link and run this, you may notice that it checks the password incorrectly - ignore this. It is "off topic")
Then, I ran a test:
I ran the code with ./password
When I was prompted for the password, I typed in 123456, one more byte than the code expects
After I hit enter and the code exits, the terminal immediately tries to run a command 6
What is causing this behavior? Is it something to do with the assembler, or how my computer is reading the code?
EDIT:
And, when I run the code with 12345, the terminal prompts for a command twice when the program closes, as if someone just hit the enter button without entering a command.
You're only reading five bytes from standard input, so when you type 123456↵, your application ends up reading 12345 and leaving 6↵ in the buffer. That gets passed on to the shell.
If you want to read the whole line, use a larger buffer.
i have problem. I tried build a loop in assembly (nasm,linux). The loop should "cout" number 0 - 10, but it not work and i don't know why. Here is a code :
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
mov eax,4
mov ebx,1
mov ecx,esi
mov edx,2
int 80h
jmp _ccout
_end:
mov eax,1
int 80h
section .data
Well, the loop is working, but you aren't using the syscall correctly. There are some magic numbers involved here, so let's get that out of the way first:
4 is the syscall number for write
1 is the file descriptor for the standard output
So far, so good. write requires a file descriptor, the address of a buffer and the length of that buffer or the part of it that it's supposed to write to the file descriptor. So, the way this is supposed to look is similar to
mov eax,4 ; write syscall
mov ebx,1 ; stdout
mov ecx,somewhere_in_memory ; buffer
mov edx,1 ; one byte at a time
compare that to your code:
mov eax,4
mov ebx,1
mov ecx,esi ; <-- particularly here
mov edx,2
int 80h
What you are doing there (apart from passing the wrong length) is passing the contents of esi to write as a memory address from which to read the stuff it's supposed to write to stdout. By pure happenstance this doesn't crash, but there's no useful data at that position in memory.
In order to solve this, you will need a location in memory to put it. Moreover, since write works on characters, not numbers, you'll have to to the formatting yourself by adding '0' (which is 48 in ASCII). All in all, it could look something like this:
section .data
text db 0 ; text is a byte in memory
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
lea eax,['0'+esi] ; print '0' + esi. lea == load effective address
mov [text],al ; is useful here even though we're not really working on addresses
mov eax,4 ; write
mov ebx,1 ; to fd 1 (stdout)
mov ecx,text ; from address text
mov edx,1 ; 1 byte
int 80h
jmp _ccout
_end:
mov [text],byte 10 ; 10 == newline
mov eax,4 ; write that
mov ebx,1 ; like before.
mov ecx,text
mov edx,1
int 80h
mov eax,1
mov ebx,0
int 80h
The output 123456789: is probably not exactly what you want, but you should be able to take it from here. Exercise for the reader and all that.