Linux 自动运行终端命令的汇编代码
最近,我编写了一段汇编代码,要求输入密码,如果用户输入内部存储的正确密码,它会打印出“correct!”。否则,它会打印出“不正确!” 代码如下: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
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
,比代码预期的多了一个字节6
而且,当我使用
12345
运行代码时,当程序关闭时,终端会两次提示输入命令,就好像有人没有输入命令就按了回车键一样。您只从标准输入中读取了五个字节,所以当您键入123456时↵代码>,应用程序最终读取12345
并离开6↵代码>在缓冲区中。这会传递到外壳上
如果要读取整行,请使用较大的缓冲区