【问题标题】:MASM loop counter only displays '+0' (irvine32)MASM 循环计数器仅显示 '+0' (irvine32)
【发布时间】:2020-05-13 02:37:12
【问题描述】:

我有一个几乎完成的脚本。我正在努力完成单词计数器。 在这种情况下,我计算每个空格的实例,并假设这意味着它是一个单词的结尾。

“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

【问题讨论】:

    标签: assembly masm irvine32 masm32


    【解决方案1】:

    这部分不正确:

    mov edx, OFFSET totalWords                              ; DISPLAY TOTALWORDS
    call WriteInt
    

    WriteInt 不希望在edx 中有偏移;它需要eax 中的实际整数值。所以代码应该是:

    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 程序集。

    【讨论】:

    • 这很有帮助!谢谢
    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2011-07-12
    • 1970-01-01
    相关资源
    最近更新 更多