【发布时间】: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 指令的操作大小未定义错误,但是当我用 dword 或 word 定义它们时,我得到段错误。
有什么帮助吗??
编辑:
这是我的输出提示和值部分:
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
这是按字母顺序计算的,另外两个是数字和杂项。是相同的。
【问题讨论】: