2013-05-30 33 views
1

我在學習彙編語言。我在MASM中編寫了一個程序,希望提示用戶給我一個數學問題的答案。我的程序工作除了一個變量沒有正確更新。我在代碼中缺少什麼?如何更新變量的值? [Assembly,MASM]

示例: 第一-嘗試:用戶輸入:123 /變量值應爲123 - 計劃示出的值,123 第二-嘗試:用戶輸入:1 /變量值應爲1 - 計劃示出的值,1231

這裏是一些與我寫的程序相關的代碼。有人可以看看,並給我一些關於如何更新變量(input_dec)的提示嗎?在此先感謝您的幫助。

.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 

回答

3

清除input_decgetData開頭。這樣的事情:

getData PROC 
push ebp 
mov ebp, esp 

mov eax, [ebp+8] 
mov dword ptr [eax],0 ; input_dec = 0 
+0

它可以在我按照你的建議做了。很好的幫助!非常感謝你。 = d – user2203774

0

我認爲你仍然將你的結果乘以註冊。嘗試在每次打印操作後重置該寄存器。

相關問題