Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat am I missing in my code? How to update the value of a variable? [Assembly, MASM]
    primarykey
    data
    text
    <p>I’m learning assembly language. I wrote a program in MASM and wanted to prompt users to give me an answer to a math problem. My program works except for one variable is not updated correctly. </p> <p>Example: 1st-try: User enters: 123 /variable value should be 123 - Program shows the value, 123 2nd-try: User enters: 1/variable value should be 1 - Program shows the value, 1231</p> <p>Here is some of the code related to the procedure I wrote. Can someone take a look and give me some tips on how to update the variable (input_dec)? Thanks in advance for your help.</p> <pre><code> .data new_prb1 BYTE "Another problem? (y/n): ", 0 input_str BYTE 21 DUP(0) ;string to be entered input_dec DWORD ? .code main PROC ; prompt and get user's answer push OFFSET input_str ;ebp+12 push OFFSET input_dec ;ebp+8 call getData ; play again? ; the program repeats until the user chooses to quit ;push OFFSET again_resp ;ebp+8 ;call play_again play_again: displayString new_prb1 ;get user input mov edx, OFFSET again_resp ;move OFFSET of again_resp to receive user response mov ecx, 12 call ReadString cmp eax, 1 jg input_notOK mov esi,edx ;point at char in string convert_str: mov al,[esi] ;move value of char into al register inc esi ;point to next char cmp al,121 ; 'y' is character 121 je next_game cmp al,110 ; 'n' is character 110 je game_over jmp convert_str input_notOK: displayString error_msg jmp play_again next_game: push OFFSET input_str push OFFSET input_dec call getData jmp play_again game_over: displayString good_bye call CrLf exit ; exit to operating system main ENDP ; *************************************************************** ;Procedure to prompt and get an answer from the user. ;receives: addresses of parameters on the system stack ;returns: user input value for the reference ;preconditions: none ;registers changed: eax, ebx, ecx, edx, esi ; *************************************************************** getData PROC push ebp mov ebp, esp again: displayString prompt_1 mov edx, [ebp+12] ;move OFFSET of input_str to receive string of integers mov ecx, 12 call ReadString cmp eax, 10 jg input_notOK mov ecx, eax ;loop for each char in string mov esi,[ebp+12] ;point at char in string pushad convert_str: mov ebx,[ebp+8] mov eax,[ebx] ;move address of input_dec into eax mov ebx,10d ;10d mul ebx ;multiply answer by 10 - eax=x*10 mov ebx,[ebp+8] ;move address of input_dec into ebx mov [ebx],eax ;add product to answer mov al,[esi] ;move value of char into al register mov eax, [esi] 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 input_notOK cmp al,9 ;error checking to ensure values are digits 0-9 jg input_notOK mov ebx,[ebp+8] ;move address of input_dec into ebx add [ebx],al ;add int to value in input_dec loop convert_str popad jmp continue input_notOK: popad displayString error_msg jmp again continue: pop ebp ret 8 getData ENDP </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload