我有我需要编写的程序集分配,我们需要做的任务是接受用户输入并循环遍历每个字符并计算字母,数字和其他字符的数量。组装循环和增量
我发现最简单的方法是做三个独立的循环,一个用于计算数字,一个用于大写字母,一个用于小写字母,通过减去数字和字母计数输入字符串长度。
我定义我的字母和数字的计数变量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
的问题是,当我运行它,我得到了用户的输入完全正常BU t对于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
这是字母数,另外两个用于数字和杂项。是相同的。
我需要更改'mcount:resb 4'吗?并修复了错误,但现在没有数字值在运行时写入终端。 –
不,这可以正确保留4个字节(双字)。你没有显示输出的部分代码,所以我们不能说出什么是错的。也许你忘了转换为文本。提供[MCVE]并学习使用调试器。 – Jester
你会如何转换为文本?对不起,我是装配新手。只需移动到注册并添加'0',然后移回去? –