【发布时间】:2015-01-01 01:31:24
【问题描述】:
最近,我写了一段汇编代码,要求输入密码,如果用户输入内部存储的正确密码,它会打印出“正确!”。否则,它会打印出“不正确!”。
代码如下:
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 password password.o
(如果您确实尝试组装链接并运行它,您可能会注意到它错误地检查了密码 - 忽略它。这是“离题”)
然后,我进行了测试:
- 我使用
./password运行代码 - 当我被提示输入密码时,我输入了
123456,比代码预期的多一个字节 - 在我敲回车并退出代码后,终端立即尝试运行命令
6
是什么导致了这种行为?是和汇编器有关,还是我的电脑是如何读取代码的?
编辑:
而且,当我使用12345 运行代码时,程序关闭时终端会提示输入命令两次,就好像有人在没有输入命令的情况下按了回车键。
【问题讨论】:
标签: linux assembly terminal runtime-error nasm