装配循环和增量

时间:2016-02-12 02:02:12

标签: assembly x86 increment

我需要编写这个程序集,我们需要做的任务是接受用户输入并遍历每个字符并计算字母,数字和杂项字符的数量。

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

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

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指令的操作大小未定义错误,但是当我用dword或{{word来定义它们时1}},我得到了段错误。

请帮忙吗?

编辑:

以下是输出提示和值的部分:

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

这是按字母顺序计数,另外两个是数字和misc。是完全相同的。

1 个答案:

答案 0 :(得分:3)

ncount指的是db而不是dwdd,这就是为什么您无法使用inc dword ptr [ncount]或{{ 1}}。不过,您可以使用inc word ptr [ncount]

或者,将inc byte ptr [ncount]扩展为ncount并使用dw,或word ptr并使用dd

相关问题