【发布时间】:2020-09-07 06:15:01
【问题描述】:
我正在尝试自学如何在 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'd 退出状态并返回 0。
所以这是一种奇怪的行为。
【问题讨论】:
-
与您的问题无关,但您的提示后面不应有空字节,因为您将长度传递给系统调用。您正在将空字节写入输出设备,这可能会产生奇怪的效果,具体取决于输出设备。
标签: linux assembly terminal nasm