Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 自动运行终端命令的汇编代码_Linux_Assembly_Terminal_Runtime Error_Nasm - Fatal编程技术网

Linux 自动运行终端命令的汇编代码

Linux 自动运行终端命令的汇编代码,linux,assembly,terminal,runtime-error,nasm,Linux,Assembly,Terminal,Runtime Error,Nasm,最近,我编写了一段汇编代码,要求输入密码,如果用户输入内部存储的正确密码,它会打印出“correct!”。否则,它会打印出“不正确!” 代码如下: 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

最近,我编写了一段汇编代码,要求输入密码,如果用户输入内部存储的正确密码,它会打印出“correct!”。否则,它会打印出“不正确!”

代码如下:

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
汇编:
nasm-f elf password.s

链接:
ld-m elf_i386-s-o密码。o

(如果您确实尝试组装链接并运行此操作,您可能会注意到它检查密码不正确-忽略此操作。这是“离题”)

然后,我做了一个测试:

  • 我用
    /password
  • 当提示我输入密码时,我输入了
    123456
    ,比代码预期的多了一个字节
  • 在我按下enter键并退出代码后,终端立即尝试运行命令
    6
  • 是什么导致了这种行为?这是与汇编程序有关,还是与我的计算机如何读取代码有关

    编辑:


    而且,当我使用
    12345
    运行代码时,当程序关闭时,终端会两次提示输入命令,就好像有人没有输入命令就按了回车键一样。

    您只从标准输入中读取了五个字节,所以当您键入
    123456时↵,应用程序最终读取
    12345
    并离开
    6↵在缓冲区中。这会传递到外壳上

    如果要读取整行,请使用较大的缓冲区