终端自动输入来自程序的命令作为命令
Terminal automatically entering input from program as a command
我正在尝试自学如何在 NASM 上用 X86 编写程序集。我正在尝试编写一个程序,该程序采用单个整数值并在退出前将其打印回标准输出。
我的代码:
section .data
prompt: db "Enter a number: ", 0
str_length: equ $ - prompt
section .bss
the_number: resw 1
section .text
global _start
_start:
mov eax, 4 ; pass sys_write
mov ebx, 1 ; pass stdout
mov edx, str_length ; pass number of bytes for prompt
mov ecx, prompt ; pass prompt string
int 80h
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov edx, 1 ; number of bytes
mov ecx, [the_number] ; pass input of the_number
int 80h
mov eax, 4
mov ebx, 1
mov edx, 1
mov ecx, [the_number]
int 80h
mov eax, 1 ; exit
mov ebx, 0 ; status 0
int 80h
我从那里进行组装 nasm -felf -o input.o input.asm
和链接 ld -m elf_i386 -o input input.o
。
我 运行 测试并输入一个整数,当我按回车键时,程序退出并且 Bash 尝试将输入的数字作为命令执行。我什至 echo
'' 退出状态并返回 0.
所以这是一个奇怪的行为。
读取调用失败,未读取任何输入。当您的程序退出时,该输入仍在等待在 TTY(这是该程序的标准输入)上读取,此时 bash 读取它。
您应该检查 return 系统调用的状态。如果系统调用returns时EAX为负数,则为错误码。例如,在本例中,EAX 包含 -14,即 EFAULT(“错误地址”)。
读取失败的原因是您传递了一个无效指针作为缓冲区地址。您需要加载 the_number
的地址,而不是它的值。使用 mov ecx, the_number
.
我正在尝试自学如何在 NASM 上用 X86 编写程序集。我正在尝试编写一个程序,该程序采用单个整数值并在退出前将其打印回标准输出。
我的代码:
section .data
prompt: db "Enter a number: ", 0
str_length: equ $ - prompt
section .bss
the_number: resw 1
section .text
global _start
_start:
mov eax, 4 ; pass sys_write
mov ebx, 1 ; pass stdout
mov edx, str_length ; pass number of bytes for prompt
mov ecx, prompt ; pass prompt string
int 80h
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov edx, 1 ; number of bytes
mov ecx, [the_number] ; pass input of the_number
int 80h
mov eax, 4
mov ebx, 1
mov edx, 1
mov ecx, [the_number]
int 80h
mov eax, 1 ; exit
mov ebx, 0 ; status 0
int 80h
我从那里进行组装 nasm -felf -o input.o input.asm
和链接 ld -m elf_i386 -o input input.o
。
我 运行 测试并输入一个整数,当我按回车键时,程序退出并且 Bash 尝试将输入的数字作为命令执行。我什至 echo
'' 退出状态并返回 0.
所以这是一个奇怪的行为。
读取调用失败,未读取任何输入。当您的程序退出时,该输入仍在等待在 TTY(这是该程序的标准输入)上读取,此时 bash 读取它。
您应该检查 return 系统调用的状态。如果系统调用returns时EAX为负数,则为错误码。例如,在本例中,EAX 包含 -14,即 EFAULT(“错误地址”)。
读取失败的原因是您传递了一个无效指针作为缓冲区地址。您需要加载 the_number
的地址,而不是它的值。使用 mov ecx, the_number
.