2011-12-11 60 views
0

很抱歉,如果這個問題很簡單,但我嘗試了所有我知道的知識,並沒有弄明白。MASM32程序集 - 從控制檯讀取一個數字

我想做一個簡單的過程,從控制檯接受一個字符串和一個計數,並打印計數指定的字符串次數。

一切都很好,但是當我將Count移動到eax的循環中時,get的值變成了亂七八糟,最終導致無限循環打印。

我試圖用atodw將Count更改爲DWORD,但沒有奏效。

下面的代碼:

PrintString PROTO :DWORD, :DWORD 

.data 

     String db 100 DUP(0) 

     Count db 10 DUP(0) 

.code 
    start: 
     ;1- get user input 

     invoke StdIn, addr String, 99 
     invoke StdIn, addr Count, 10 

     ;2- Remove the CRLF from count 
     invoke StripLF, addr Count 

     ;3- Convert the count to DWORD 
     invoke atodw, addr InputCount 
     mov Counter, eax 

     ;4- Call the Printer function 

     invoke Printer, addr String, addr Count 

Printer PROC StringToPrint:DWORD, count:DWORD   

mov eax,count ;;;;;; This is the problem I think 

Looppp: 
      push eax 

      invoke StdOut, StringToPrint 

      pop eax 
      dec eax 

      jnz Looppp 
    ret 
Printer endp 

回答

0

你傳遞addr Count - 字符串的地址 - 作爲第二個參數來Printer。但它期望一個整數,所以您想要改爲通過Counter

由於您使用的是沒有進行類型檢查的語言,因此爲您的標識符采用命名約定(如Hungarian notation)可以幫助您查看並避免此類問題。例如,在這裏命名爲strCountdwCount的變量會更明顯地表明您使用了錯誤的變量。

順便說一句,eax必須最終達到零,因此您的打印循環將不無限 - 只是,而時間比你預期...

+0

非常感謝,這解決了這個問題。我會看看匈牙利符號,看起來更優雅和高效。 –