Converting string to integer in MASM: esi difficulty

assemblymasmx86

I've written a program in assembly language (MASM) to allow students to practice calculating combinations. The program randomly assigns n and r, calculates the combination, then prompts the student to provide the answer. Problem solved *For the most part the program is working well, but I'm running into some challenges converting the integer string provided by the student into an integer that can then be compared with the result calculated by the program. I've used esi before without problem, but for whatever reason, when the program gets to the point where the contents of esi (or the address of the string) are to be evaluated and converted to an integer, debugger is showing that the contents of esi are not the first char in the string.* Just to be clear, I am familiar with ReadInt, but am trying to figure out how to parse integers without using ReadInt.

UPDATE: The program now works well as long as the user inputs valid numbers. I've solved the issues with esi not pointing in the right spot. I've also put in some error checking to ensure that the values entered are actually numbers. However, initially 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).

EDIT: Just to clarify, this is for x86 processors.

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

EDITED CODE:

;*************************************************
; 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:

Pseudocode has been updated

  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.

Best Answer

Split each character off the string and divide by 48d.

You want to subtract, not divide.

As to the esi problem, please show how this function is called. Maybe [ebp+16] isn't the offset of the buffer, or there is a buffer overflow. You can also use your debugger to set a memory write watch on that address and see who changes it.

Also, your invalidInput label is not in the code, but I suspect the tryAgain label is in the wrong place. You probably want it after the prologue.

By the way, the typical method to do this conversion is to go from the start of the string forwards, and keep multiplying the partial result by 10 in each step. That way you don't need the PowerTen function.

Update: you are loading 4 bytes, hence 4 characters from your string at once, that's why you see the unexpected value. Instead of mov eax,[esi] do movzx eax, [esi] or mov al, [esi]

Related Topic