我正在尝试计算字符串中字符的出现次数。
我的代码如下:
data segment 'DSEG'
text db "This is a sentence.",0 ; string
textSize dw $ - text - 1 ; size of string, - 1 to account for null-termination character
freqTable dd 256 DUP(0)
ends 'DSEG'
code segment 'CSEG'
start:
mov ax, data ; set segment registers
mov ds, ax
mov es, ax
;---------------------------------------
sub cx, cx
mov cx, textSize ; number of times to loop
L1:
mov ax, [OFFSET text + cx - 1] ; loop from back using cx, put character in ax
inc [OFFSET freqTable + 4*ax] ; increment table's index at the ascii value of character
LOOP L1
;---------------------------------------
mov ax, 4c00h ; return to OS
int 21h
ends 'CSEG'
end start ; set entry point
我做了一个数组
DWORDS
其中每个索引将代表一个字符。然后我遍历字符串并尝试在每个字符的 ascii 值处增加数组。但是,我得到了一个
wrong parameters
当我尝试在循环中递增时出错。我不确定是什么导致了这个错误。我猜我不能只是增加我想要的方式。如何正确创建频率表?我错过了一些小东西吗?
最佳答案
X86 不允许像您一样通过使用 AX 和 CX 寄存器进行内存寻址。但是,您可以为此目的使用 EAX 和 ECX。
整个下一个代码片段位 EAX[8,31] 和 ECX[16,31] 将保持为零。
;---------------------------------------
xor eax, eax
xor ecx, ecx
mov cx, textSize ; number of times to loop
L1:
mov al, [OFFSET text + ecx - 1]
inc [OFFSET freqTable + eax*4]
loop L1
;---------------------------------------
关于assembly - 计算字符数组中的字符频率 - x86 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383575/