我的剧本快写好了。我正在努力完成“计数器”这个词。在这种情况下,我计算一个空格的每个实例,并假设这意味着它是一个单词的结尾。
“totalWords”变量被初始化为0,每次在字符串中找到“”时都会递增。
但是,无论何时测试,输出总是'+0‘。我知道脚本的其余部分可以工作,因为它成功地转换了字母的大小写。
增加变量并显示它的最佳实践是什么?
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发布于 2020-05-13 06:10:54
本部分不正确:
mov edx, OFFSET totalWords ; DISPLAY TOTALWORDS
call WriteIntWriteInt不需要edx中的偏移量;它需要eax中的实际整数值。因此,守则应该是:
mov eax, totalWords ; DISPLAY TOTALWORDS
call WriteInt而且,您的空间变量是没有意义的。你可以直接写
.IF(al == ' ')你计算单词数量的方法听起来有点不太好。像"foo bar"这样的字符串只包含一个空格,但包含两个单词。请注意,您也不能真正使用number_of_spaces+1,因为这会给" "、"foo bar"和"foo "这样的字符串带来错误的结果。
你可能会得到更好的结果,比如:
if (al != ' ' && (esi == 0 || source[esi-1] == ' ')) totalWords++;这只是一些用来表达想法的伪代码。我将由您来将其转换为x86程序集。
https://stackoverflow.com/questions/61765423
复制相似问题