如何使用 NASM 程序集忽略输入中的换行符? [英] How do I ignore line breaks in input using NASM Assembly?

查看:21
本文介绍了如何使用 NASM 程序集忽略输入中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习 NASM 汇编,我正在尝试制作一个读取两个一位数输入的程序.

Learning NASM Assembly, I am trying to make a program that reads two one-digit number inputs.

我在 .bss 中声明了两个变量:

I have two variables declared in the .bss:

num1 resb 1
num2 resb 1

然后,我让用户写下这样的数字:

Then, I ask the user to write the numbers like this:

; Get number 1
mov EAX,3
mov EBX,1
mov ECX,num1
mov EDX,1
int 0x80

; Get number 2
mov EAX,3
mov EBX,1
mov ECX,num2
mov EDX,1
int 0x80

由于我只对一位数字输入感兴趣,所以我将 EDX 设置为 1.这样,无论用户输入什么,只有第一个字符会存储在我的变量中(对吗?).

Since I am only interested in one-digit number inputs, I set EDX to 1. This way, whatever the user types, only the first character will be stored in my variable (right?).

问题是第一个字符之后的所有内容都将用于以后的读取.如果你输入 5 然后按 ENTER5 将被存储在 num1 中就好了,但是换行符您按ENTER 生成的将继续执行下一条读取指令,该指令将存储在num2 中.显然这不是我想要的(我希望用户输入一个数字,按 ENTER,输入另一个数字,然后按 ENTER).

The problem is that everything that follows after that first character will be used for the future reads. If you type 5 and then press ENTER, 5 will be stored in num1 just fine, but the line break you generated by pressing ENTER will carry on to the next read instruction, which will be stored in num2. Clearly that's not what I was intending (I want the user to type a number, press ENTER, type another number, and press ENTER).

我不完全确定如何以最简单的方式解决这个问题.

I am not entirely sure how to work around this in the simplest way possible.

最愚蠢的想法是在 num1num2 之间放置一个虚拟"读取指令,它将捕获换行符(并且什么也不做).这显然不好.

The dumbest idea was to put a "dummy" read instruction between num1 and num2, which will capture the line break (and do nothing with it). This is obviously not good.

推荐答案

这是一种非常基本的读取输入的方法,直到您获得所需的数字.它会跳过除数字以外的任何内容.如果这种方法提供了您想要的功能,它就很好.如果您需要根据其他非数字输入的不同行为,则需要指定该行为.然后也可以对这种行为进行编程.

Here's a very basic way of reading input until you get digits you want. It will skip anything but digits. This approach is fine if it provides the functionality you want. If you need different behavior depending upon other non-numeric input, then you need to specify that behavior. Then that behavior can be programmed as well.

    ; Get number 1
    mov   ECX,num1
    call  GetNumber

    ; Get number 2
    mov   ECX,num2
    call  GetNumber
    ...

GetNumber:
    pusha              ; save regs
get:
    mov   EAX,3        ; system call for reading a character
    mov   EBX,0        ; 0 is standard input
    mov   EDX,1        ; number of characters to read
    int   0x80         ; ECX has the buffer, passed into GetNumber
    cmp   byte [ecx],0x30
    jlt   get          ; Retry if the byte read is < '0'
    cmp   byte [ecx],0x39
    jgt   get          ; Retry if the byte read is > '9'

    ; At this point, if you want to just return an actual number,
    ; you could subtract '0' (0x30) off of the value read
    popa               ; restore regs
    ret

这篇关于如何使用 NASM 程序集忽略输入中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆