[英]MASM loop counter only displays '+0' (irvine32)
我有一個幾乎完成的腳本。 我正在努力完成單詞計數器。 在這種情況下,我正在計算空格的每個實例,並假設這意味着它是一個單詞的結尾。
'totalWords' 變量被初始化為 0 並且每次在字符串中找到一個 ' ' 時遞增。
但是,無論何時測試,output 始終為“+0”。 我知道腳本的 rest 有效,因為它成功地轉換了字母的大小寫。
增加變量並顯示它的最佳做法是什么?
INCLUDE Irvine32.inc
.data
source BYTE 40 DUP (0)
byteCount DWORD ?
target BYTE SIZEOF source DUP('#')
sentencePrompt BYTE "Please enter a sentence: ",0
wordCountPrompt BYTE "The number of words in the input string is: ",0
outputStringPrompt BYTE "The output string is: ",0
totalWords DWORD 0 ; SET TOTALWORDS VARIABLE TO 0 INITIALLY
one DWORD 1
space BYTE " ",0
.code
main PROC
mov edx, OFFSET sentencePrompt
call Crlf
call WriteString
mov edx, OFFSET source
MOV ecx, SIZEOF source
call ReadString
call Crlf
call Crlf
call TRANSFORM_STRING
call Crlf
exit
main ENDP
TRANSFORM_STRING PROC
mov esi,0
mov edi,0
mov ecx, SIZEOF source
transformStringLoop:
mov al, source[esi]
.IF(al == space)
inc totalWords ; INCREMENT TOTALWORDS DATA
mov target[edi], al
inc esi
inc edi
.ELSE
.IF(al >= 64) && (al <=90)
add al,32
mov target[edi], al
inc esi
inc edi
.ELSEIF (al >= 97) && (al <=122)
sub al,32
mov target[edi], al
inc esi
inc edi
.ELSE
mov target[edi], al
inc esi
inc edi
.ENDIF
.ENDIF
loop transformStringLoop
mov edx, OFFSET wordCountPrompt
call WriteString
mov edx, OFFSET totalWords ; DISPLAY TOTALWORDS
call WriteInt
call Crlf
mov edx, OFFSET outputStringPrompt
call WriteString
mov edx, OFFSET target
call WriteString
ret
TRANSFORM_STRING ENDP
END main
這部分不正確:
mov edx, OFFSET totalWords ; DISPLAY TOTALWORDS
call WriteInt
WriteInt
不希望在edx
中有偏移; 它期望eax
中的實際 integer 值。 所以代碼應該是:
mov eax, totalWords ; DISPLAY TOTALWORDS
call WriteInt
此外,您的空間變量毫無意義。 你可以寫
.IF(al == ' ')
而且你計算單詞數量的方式聽起來有點破舊。 諸如"foo bar"
類的字符串僅包含 1 個空格,但包含兩個單詞。 請注意,您也不能真正使用number_of_spaces+1
,因為這會給諸如" "
、 "foo bar"
和"foo "
之類的字符串提供錯誤的結果。
您可能會通過以下方式獲得更好的結果:
if (al != ' ' && (esi == 0 || source[esi-1] == ' ')) totalWords++;
這只是表達這個想法的一些偽代碼。 我將由您將其轉換為 x86 程序集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.