【发布时间】:2013-11-25 08:42:18
【问题描述】:
在这个线程之后,How do i read single character input from keyboard using nasm (assembly) under ubuntu?,我正在尝试编译一个响应 NASM 中的输入的程序。 我制作了以下文件:
my_load2.asm:
%include "testio.inc"
global _start
section .text
_start: mov eax, 0
call canonical_off
call canonical_on
testio.inc:
termios: times 36 db 0
stdin: equ 0
ICANON: equ 1<<1
ECHO: equ 1<<3
canonical_off:
call read_stdin_termios
; clear canonical bit in local mode flags
push rax
mov eax, ICANON
not eax
and [termios+12], eax
pop rax
call write_stdin_termios
ret
echo_off:
call read_stdin_termios
; clear echo bit in local mode flags
push rax
mov eax, ECHO
not eax
and [termios+12], eax
pop rax
call write_stdin_termios
ret
canonical_on:
call read_stdin_termios
; set canonical bit in local mode flags
or dword [termios+12], ICANON
call write_stdin_termios
ret
echo_on:
call read_stdin_termios
; set echo bit in local mode flags
or dword [termios+12], ECHO
call write_stdin_termios
ret
read_stdin_termios:
push rax
push rbx
push rcx
push rdx
mov eax, 36h
mov ebx, stdin
mov ecx, 5401h
mov edx, termios
int 80h
pop rdx
pop rcx
pop rbx
pop rax
ret
write_stdin_termios:
push rax
push rbx
push rcx
push rdx
mov eax, 36h
mov ebx, stdin
mov ecx, 5402h
mov edx, termios
int 80h
pop rdx
pop rcx
pop rbx
pop rax
ret
然后我运行:
[root@localhost asm]# nasm -f elf64 my_load2.asm
[root@localhost asm]# ld -m elfx86_64 my_load2.o -o my_load2
当我尝试运行它时,我得到:
[root@localhost asm]# ./my_load2
Segmentation fault
调试器说:
(gdb) run
Starting program: /root/asm/my_load2
Program received signal SIGSEGV, Segmentation fault.
0x00000000004000b1 in canonical_off ()
有人可以解释为什么没有“导入”步骤就会崩溃吗? 另外,我在 Win7 64 位下的 Virtualbox 中运行 RHEL。这会导致编译问题吗?
【问题讨论】:
-
在您发布的代码中没有定义符号
write_stdin_termios。您可能发布了不完整的代码版本。 -
你永远不会退出你的程序。
-
修复了这个问题。我尝试编译完整的代码。
-
我没有退出的事实并不能解释为什么它会在 canonical_off 上崩溃,但之后不会。
-
您的
termios结构似乎位于section .text- 只读内存中。把它放在section .data有帮助吗?