Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:59:51+00:00 2026-06-15T10:59:51+00:00

I have a program that converts a string to integers. The program works well

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:59:52+00:00Added an answer on June 15, 2026 at 10:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that converts a byte[] to a string of hex: byte[]
Let's say I have a program which converts letters to numbers such that: Input:
I have this function in my program that converts integers to strings: QString Stats_Manager::convertInt(int
i'm having problems creating a program that converts a string of numbers into an
I have tried to write a simple program that converts numbers to words (i.e
I have program that has a variable that should never change. However, somehow, it
I'm doing a magicsquare program that allows a user to input numbers >0 to
I have a real basic command-line program, in Objective-C, that searches for user inputed
I have a program that I am trying to take a set of numbers
I have a program that needs to constantly ping a device However this ping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.