组装x86 NASM-避免读取返回键 [英] Assembly x86 NASM - Avoid read return key
问题描述
我刚刚开始学习汇编,但是找不到任何有用的内容.
I just started learning assembly and i'm not finding any useful content that helps.
我正在创建一个简单的程序,基本上可以读取用户输入:
I'm creating a simple program that reads user input,basically:
section .bss
opA: resw 1
opB: resw 1
section .text
global _start
inputA:
mov EAX, 3
mov EBX, 0
mov ECX, opA
mov EDX, 1
int 80h
inputB:
mov EAX, 3
mov EBX, 0
mov ECX, opB
mov EDX, 1
int 80h
/*terminate*/
问题在于,在我输入第一个值并按回车键之后,第二个sys_read被跳过(我相信它正在读取'\ n'字符).
The problem is that after i enter the first value and hit return,the second sys_read is skipped(i believe it is reading the '\n' character).
所以我尝试比较opB是否存储了'\ n',在肯定的情况下,我跳回了'inputB:',就像这样:
So i tried comparing if opB stored '\n' and in positive case i jump back to 'inputB:',like this:
cpm word[opA], '\n'
je inputB
但是它不起作用!我该如何解决?
But it is not working!How cal i solve this?
更简单,如何从变量中删除换行符?
推荐答案
一个选项是刷新标准输入缓冲区:
One option is to flush the stdin buffer:
section .data
opA: db 0
opB: db 0
LF: db 10
section .text
global _start
_start:
inputA:
mov EAX, 3
mov EBX, 0
mov ECX, opA
mov EDX, 1
int 80h
mov eax,54 ; kernel function SYS_IOCTL
mov ebx,0 ; EBX=0: STDIN
mov ecx,0x540B ; ECX=0x540B: TCFLSH
xor edx, edx ; EDX=0: TCIFLUSH
int 0x80 ; sys_call
inputB:
mov EAX, 3
mov EBX, 0
mov ECX, opB
mov EDX, 1
int 80h
mov eax,54 ; kernel function SYS_IOCTL
mov ebx,0 ; EBX=0: STDIN
mov ecx,0x540B ; ECX=0x540B: TCFLSH
xor edx, edx ; EDX=0: TCIFLUSH
int 0x80 ; sys_call
print:
mov edx,3
mov ecx,opA
mov ebx,1
mov eax,4
int 0x80
exit:
mov eax, 1
mov ebx, 0
int 0x80
与管道配合使用的另一个选项是读取stdin直到EOF或LF:
Another option - which works with pipes - is to read stdin until EOF or LF:
section .data
opA: db 0
opB: db 0
LF: db 10
dummy: db 0
section .text
global _start
reads:
.1: ; Loop
mov eax,3 ; kernel function SYS_READ
mov ebx, 0 ; EBX=0: STDIN
mov ecx, dummy ; dummy buffer
mov edx, 1 ; number of bytes to read
int 0x80 ; sys_call
test eax, eax ; EOF?
jz .2 ; yes: ok
mov al,[dummy] ; no: fetched character
cmp al, 10 ; character == LF ?
jne .1 ; no -> loop (i.e. fetch next character)
.2
ret
_start:
inputA:
mov EAX, 3
mov EBX, 0
mov ECX, opA
mov EDX, 1
int 80h
call reads
inputB:
mov EAX, 3
mov EBX, 0
mov ECX, opB
mov EDX, 1
int 80h
call reads
print:
mov edx,3
mov ecx,opA
mov ebx,1
mov eax,4
int 0x80
exit:
mov eax, 1
mov ebx, 0
int 0x80
这篇关于组装x86 NASM-避免读取返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!