MASM字符串转换为整数处理无效的输入 [英] MASM convert string to integer: processing invalid input

查看:199
本文介绍了MASM字符串转换为整数处理无效的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串转换为整数的程序。程序工作良好,只要用户输入有效数字。但是,一旦我添加了一些错误检查,以确保输入的值实际上是数字,我遇到了一些挂钩。最初,我有TRYAGAIN标记为code的的getData过程中的第一道防线。但是,错误检查意味着,之后才下一次迭代过程中有效的数字输入的输入无效,程序不停地返回消息无效输入。把TRYAGAIN标签在code的第一行会导致程序来保持,即使有效输入如下无效的输入返回无效的输入信息。首先,我试图把TRYAGAIN标签在其当前职位,但我认为这是对一个无限循环运行。其次,我试图如果程序跳转到invalidInput重置变量,但是这并没有固定它(我在原来的位置和当前位置与TRYAGAIN尝试这样做)。

这是MASM的x86处理器。预先感谢您的建议。

这里的code:

 。数据
导致DWORD?
临时BYTE 21 DUP(0)
回答DWORD?。code
主要PROC

(有些preliminary过程调用)

 推OFFSET温度; EBP + 16
推OFFSET的答案; EBP + 12
推answerSize; EBP + 8
打电话的getData

(更多过程调用)

 退出;退出到操作系统
主要ENDP
; *******************
;提示/获取用户的答案。
;接收:偏移答案,温度和answerSize的价值
;返回:无
; preconditions:无
;改变寄存器:EAX,EBX,ECX,EDX,EBP,ESI,ESP
; *******************
PROC的getData
推EBP
MOV EBP,ESP再试一次:
mWriteStr prompt_1
MOV EDX,[EBP + 16];将OFFSET温度的接收整数串
MOV ECX,12
调用ReadString
CMP EAX,10
JG invalidInputMOV ECX,EAX;循环为字符串中的每个字符
MOV ESI,[EBP + 16]点在串字符PUSHAD
loopString:;环着眼于字符串中的每个字符
    MOV EBX,[EBP + 12]
    MOV EAX,[EBX];将答案地址到EAX
    MOV EBX,10D
    MUL EBX;乘以10回答
    MOV EBX,[EBP + 12];答案的举动地址到EBX
    MOV [EBX],EAX;增加产品的答案
    MOV人,[ESI];焦炭移动值到人寄存器
    INC ESI;指向下一个字符
    子人,48D;从CHAR得到整数ASCII值减去48    CMP人,0;错误检查,以确保值是数字(0-9)
    JL invalidInput
    CMP人,9
    JG invalidInput    MOV EBX,[EBP + 12];答案的举动地址到EBX
    添加[EBX]人;在回答加INT价值    环loopString
POPAD
JMP这个MoveOn
invalidInput:;复位寄存器和变量为0
    MOV人,0
    MOV EAX,0
    MOV EBX,[EBP + 12]
    MOV [EBX],EAX
    MOV EBX,[EBP + 16]
    MOV [EBX],EAX
    mWriteStr错误
    JMP TRYAGAIN
继续:
    流行EBP
    RET 12
ENDP的getData

只要你有我这个要去的地方的感觉,这是我的伪code:


  1. 开始在字符串的开头


  2. 乘以10回答的值。


  3. 拆分每个字符掉字符串,并以48D减去获得的整数。防爆。学生进入156. 49存储在变量temp中的第一个字符。从49减48的整数是1。


  4. 添加整数答案的价值。


  5. 公司ESI(移动一个字符右)。


  6. 循环。



解决方案

摆脱 PUSHAD POPAD 。每次和无效字符便被输入,它就跳回到TRYAGAIN然后又 PUSHAD 被执行 - 每次跳回TRYAGAIN时间。

如果您需要保存任何寄存器,这样做的序幕和尾声恢复它们。

I have a program that converts a string to integers. The program works well as long as the user inputs valid numbers. However, once I added some error checking to ensure that the values entered are actually numbers, I ran into some hitches. Initially, I had the tryAgain tag as the first line of code in the getData procedure. However, the error checking meant that following an invalid input when valid numbers are entered during the next iteration, the program kept returning the message "invalid input". Putting the tryAgain tag in the first line of code causes the program to keep returning the invalid input message even when valid input follows invalid input. First, I attempted to put the tryAgain tag in its current position, but I think it's running on an infinite loop. Second, I attempted to reset variables if the program jumps to invalidInput, but that hasn't fixed it (I tried this with tryAgain in its original position and current position).

This is MASM for x86 processors. Thanks in advance for your suggestions.

Here's the code:

.data
result  DWORD   ?
temp        BYTE        21 DUP(0)
answer  DWORD   ?

.code
main    PROC

(some preliminary procedure calls)

push OFFSET temp        ;ebp+16
push OFFSET answer      ;ebp+12
push answerSize     ;ebp+8
call    getData

(more procedure calls)

exit        ; exit to operating system
main ENDP


;*************************************************
; prompts / gets the user’s answer.
; receives: the OFFSET of answer and temp and value of answerSize
; returns: none
; preconditions: none
; registers changed:  eax, ebx, ecx, edx, ebp, esi, esp
;*************************************************
getData PROC
push        ebp
mov     ebp,esp

tryAgain:
mWriteStr   prompt_1
mov     edx, [ebp+16]       ;move OFFSET of temp to receive string of integers
mov     ecx, 12
call        ReadString
cmp     eax, 10
jg      invalidInput

mov     ecx, eax        ;loop for each char in string
mov     esi,[ebp+16]    ;point at char in string

pushad
loopString:             ;loop looks at each char in string
    mov     ebx,[ebp+12]
    mov     eax,[ebx]   ;move address of answer into eax
    mov     ebx,10d     
    mul     ebx         ;multiply answer by 10
    mov     ebx,[ebp+12]    ;move address of answer into ebx
    mov     [ebx],eax       ;add product to answer
    mov     al,[esi]        ;move value of char into al register
    inc     esi         ;point to next char
    sub     al,48d      ;subtract 48 from ASCII value of char to get integer  

    cmp     al,0            ;error checking to ensure values are digits 0-9
    jl      invalidInput
    cmp     al,9
    jg      invalidInput

    mov     ebx,[ebp+12]    ;move address of answer into ebx
    add     [ebx],al        ;add int to value in answer

    loop        loopString  
popad
jmp     moveOn
invalidInput:               ;reset registers and variables to 0
    mov     al,0
    mov     eax,0
    mov     ebx,[ebp+12]
    mov     [ebx],eax
    mov     ebx,[ebp+16]
    mov     [ebx],eax       
    mWriteStr   error
    jmp     tryAgain
moveOn:
    pop     ebp
    ret     12
getData ENDP

Just so you have a sense of where I'm going with this, here's my pseudocode:

  1. Start at the beginning of the string

  2. Multiply the value of answer by 10.

  3. Split each character off the string and subtract by 48d to get the integer. Ex. student enters 156. 49 is stored as the first char in the variable temp. Subtract 48 from 49. The integer is 1.

  4. Add integer to value of answer.

  5. Inc esi (move one character right).

  6. Loop.

解决方案

Get rid of the pushad and popad. Every time and invalid character is inputed, it jumps back to tryAgain then another pushad gets executed - each time you jump back to tryAgain.

If you need to save any registers, do it in the prologue and restore them in the epilogue.

这篇关于MASM字符串转换为整数处理无效的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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