【问题标题】:Assembly loop and incrementation装配循环和增量
【发布时间】:2016-02-12 02:02:11
【问题描述】:

我有这个汇编作业需要编写,我们需要做的任务是接受用户输入并循环遍历每个字符并计算字母、数字和杂项字符的数量。

我发现最简单的方法是执行三个单独的循环,一个用于计算数字,一个用于大写字母,一个用于小写字母,而不是通过从输入中减去数字和字母计数来找到杂项计数字符串长度。

我在.data 部分中将我的字母和数字计数变量定义为0,如下所示:

acount: db 0                  ; alphabetic count variable
ncount: db 0                  ; numeric count variable

这样我就可以增加它们。我所有的循环都以相同的方式设置,所以这里以我的数字计数器为例:

init_numeric:
        ;; Initialize the input for scanning
        mov     ecx, [rlen]             ; initialize the input length
        mov     esi, input              ; point to the start of input

scan_numeric:
        ;; beginning of the character scan for numeric values
        mov     al, [esi]               ; get a character
        inc     esi                     ; update to the next character
        cmp     al, '0'                 ; check the lower bound
        jb      not_num                 ; jump if below '0'
        cmp     al, '9'                 ; check the upper bound
        ja      not_num                 ; jump if above '9'
        inc     [ncount]                ; add 1 to the numeric count

not_num:
        dec     ecx                     ; update the number of characters
        jnz     scan_numeric            ; loop to top if more characters

一旦这些循环完成,我就会得到杂项计数,在 .bss 部分中定义为:

mcount: resb 4            ; reserve space for misc character count

以及这样的计算和操作:

get_misc:
        ;; Subtract the alphabetic and numeric counts from the length for
        ;; miscellanious character count
        mov     eax, [rlen]             ; move the input string length
        sub     eax, [acount]           ; subtract the alpha count
        sub     eax, [ncount]           ; subtract the numeric count
        mov     [mcount], eax           ; move eax value to mcount reserve

问题是,当我运行它时,我得到的用户输入非常好,但我得到 inc 指令的操作大小未定义错误,但是当我用 dwordword 定义它们时,我得到段错误。

有什么帮助吗??


编辑:

这是我的输出提示和值部分:

result_write:
        ;; Write the results to the terminal

        ;; Alphabetic Count
        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, init               ; initial response msg
        mov     edx, ilen               ; initial msg length
        int     080h                    ; kernel execution

        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, [acount]           ; alphabetic count
        mov     edx, 4                  ; length
        int     080h                    ; kernel execution

        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, alpha              ; alphabetic response end
        mov     edx, alen               ; response length
        int     080h                    ; kernel execution

这是按字母顺序计算的,另外两个是数字和杂项。是相同的。

【问题讨论】:

    标签: assembly x86 increment


    【解决方案1】:

    您让ncount 指向db 而不是dwdd,这就是您不能使用inc dword ptr [ncount]inc word ptr [ncount] 的原因。不过,您可以使用inc byte ptr [ncount]

    或者,将ncount 扩大到dw 并使用word ptr,或扩大到dd 并使用dword ptr

    【讨论】:

    • 我还需要更改mcount: resb 4 吗?并且修复了错误,但现在在运行时没有数值被写入终端。
    • 不,正确保留 4 个字节(一个 dword)。您还没有显示执行输出的部分代码,所以我们无法判断出了什么问题。也许您忘记了转换为文本。提供minimal reproducible example 并学习使用调试器。
    • 您将如何转换为文本?抱歉,我是组装新手。只需移动注册并添加“0”,然后返回?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多