如何测试以确保仅输入一个整数,并确保在Assembly中输入的长度为5个字节或更小? [英] How do I test to ensure only an integer is entered and ensure length of input is 5 bytes or less in Assembly?

查看:96
本文介绍了如何测试以确保仅输入一个整数,并确保在Assembly中输入的长度为5个字节或更小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试以确保在以下代码中仅输入整数并确保输入长度为5个字节或更少?

How do I test to ensure only an integer is entered and ensure length of input is 5 bytes or less in the following code?

我试图了解如何适当地控制输入,以使超过5个字节的输入在程序退出时不会输出到终端.

I am trying to understand how to properly control input so that the input beyond 5 bytes is not outputted to the terminal upon exiting of the program.

此外,我将如何测试以确保仅输入一个字符串,最后在最后一种情况下,仅输入一个双精度字?

In addition, how would I test to ensure only a string is entered and finally in the last scenario, only a double is entered?

***根据x82和Peter C的指导更新了代码.我做了一些C的操作,并能够在下面修改我的原始代码.它仍然有一些缺陷,但是你们两个都有很大的帮助!我只是停留在输入5个以上的整数字节时,它不会像我输入字符数据时那样再次提示,因为它继续将多余的字节数据转储到tty中.

*** Updated code based on x82 and Peter C's guidance. I did some C disas and was able to amend my original code below. It still has some flaws but you are both a great deal of help! I am just stuck on when more than 5 integer bytes are entered it wont re-prompt as it does when I enter in a character data as it continues to dump extra bytes data to tty.

SECTION .data                   ; initialized data section
promptInput db 'Enter Number: ', 0
lenPromptInput equ $ - promptInput
displayInput db 'Data Entered: ', 0
lenDisplayInput equ $ - lenDisplayInput

SECTION .bss                ; uninitialized data section
number resb 1024            ; allocate 1024 bytes for number variable

SECTION .text               ; code section
global _start               ; linker entry point

_start:
nop                         ; used for debugging

Read:
mov eax, 4                  ; specify sys_write call
mov ebx, 1                  ; specify stdout file descriptor
mov ecx, promptInput        ; display promptInput
mov edx, lenPromptInput     ; length of promptInput
int 0x80                    ; call sys_write

mov eax, 3                  ; specify sys_read call
mov ebx, 0                  ; specify stdin file descriptor
mov ecx, number             ; pass address of the buffer to read to
mov edx, 1024               ; specify sys_read to read 1024 bytes stdin
int 0x80                    ; call sys_read

cmp eax, 0                  ; examine sys_read return value in eax
je Exit                     ; je if end of file

cmp byte [number], 0x30     ; test input against numeric 0
jb Read                     ; jb if below 0 in ASCII chart
cmp byte [number], 0x39     ; test input against numeric 9
ja Read                     ; ja if above 9 in ASCII chart

Write:
mov eax, 4                  ; specify sys_write call
mov ebx, 1                  ; specify stdout file descriptor
mov ecx, displayInput       ; display displayInput
mov edx, lenDisplayInput    ; length of displayInput
int 0x80                    ; call sys_write    

mov eax, 4                  ; specify sys_write call
mov ebx, 1                  ; specify stdout file descriptor
mov ecx, number             ; pass address of the number to write
mov edx, 5                  ; pass number of numbers to write
int 0x80                    ; call sys_write

Exit:
mov eax, 1                  ; specific sys_exit call
mov ebx, 0                  ; return code 0 to OS
int 0x80                    ; call sys_exit

推荐答案

(由于您接受了此答案,因此我指出,有关在TTY上使用read的此问题的实际答案是

(Since you accepted this answer, I'll point out that the actual answer to this question about using read on TTYs is my other answer on this question.)

以下是您的的答案低质量的后续问题,当您删除它时,我将要发布该问题.

Here's an answer to your low-quality followup question which I was about to post when you deleted it.

请注意,我说的是您可以在一个新问题中寻求调试帮助",而不是您应该在一个问题中提出3个不同的问题,然后重新发布几乎没有更改的整个代码,而无需认真尝试解决自己的问题.将新问题变成问题仍然由您决定.

Note that I said "you can ask for debugging help in a new question", not that you should ask 3 different questions in one, and re-post your whole code barely changed with no serious attempt at solving your own problem. It's still up to you to make the new question a good question.

如果我没有导致您将其发布在第一位的话,我可能不会回答.欢迎使用StackOverflow,我很慷慨,因为您是新手,也不知道有什么好问题.

I probably wouldn't have answered it if I hadn't sort of led to you posting it in the first place. Welcome to StackOverflow, I'm being generous since you're new and don't know what's a good question yet.

字符"0"到"9"的常用术语是数字",而不是整数".更具体了.

The usual term for the characters '0' through '9' is "digit", not "integer". It's much more specific.

确保仅将整数输入到缓冲区中

ensure only integers are inputted in the buffer

不能.如果检测到此类输入,则必须决定要执行的操作.

You can't. You have to decide what you want to do if you detect such input.

需要帮助创建要遍历的数组

Need help creating an array to loop through

您的缓冲区是字节数组.

Your buffer is an array of bytes.

您可以使用类似的方法遍历它

You can loop over it with something like

    # eax has the return value from the read system call, which you've checked is strictly greater than 0
    mov    esi, number        ; start pointer

scan_buffer:
    movzx  edx, byte [esi]
        # do something with the character in dl / edx
        ...

    inc    esi                ; move to the next character

    dec    eax
    jnz   scan_buffer         ; loop n times, where n = number of characters read by the system call.


确保1024缓冲区上的字符不会将数据发送到tty

ensure characters over the 1024 buffer do not send data to the tty

如果您担心1024不一定足以容纳此玩具程序,请使用select(2)poll(2)来检查是否有更多的输入要读取,而没有阻塞.

If you're worried that 1024 isn't necessarily big enough for this toy program, then use select(2) or poll(2) to check if there's more input to be read without blocking if there isn't.

这篇关于如何测试以确保仅输入一个整数,并确保在Assembly中输入的长度为5个字节或更小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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