Home > Blockchain >  NASM - How can I solve the input reading problem from the terminal?
NASM - How can I solve the input reading problem from the terminal?

Time:10-22

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.

CodePudding user response:

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.

CodePudding user response:

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,2
    mov ecx, esi
    mov edx,1 ; I don't know that is correct?
    int 80h

    cmp byte[msginput], 0x0a
    je end

    inc esi
    jmp _loop

end:
   ret

You would have to increase the size of msginput tho.

  • Related