我是汇编语言编程的完全初学者。我需要帮助编写一个汇编语言程序来从用户那里获取字符串,计算并显示每个单词在用户输入的字符串中出现的次数。
例如,如果用户输入:
Hello Hello what is new Hello what is not new
输出应该是:
Hello 3
what 2
is 2
not 1
new 2
下面的代码将给出字符串中字符的频率。但是,我不知道如何编辑以便我可以跟踪单词而不仅仅是字符,然后能够显示这些单词及其相应的频率。
INCLUDE Irvine32.inc
Get_frequencies PROTO,
pString:PTR BYTE, ; points to string
pTable:PTR DWORD ; points to frequency table
.data
freqTable DWORD 256 DUP(0)
;aString BYTE 1,2,"This is extremely difficult for the experienced",0
aString BYTE 80 DUP(0),0
str1 BYTE "*** Constructing a Frequency Table *** (DEMO)",
0dh,0ah,0dh,0ah,
"Enter between 1 and 80 characters: ",0
.code
main PROC
call Clrscr
mov edx,OFFSET str1
call WriteString
mov ecx,SIZEOF aString - 1
mov edx,OFFSET aString
call ReadString
INVOKE Get_frequencies, ADDR aString, ADDR freqTable
call DisplayTable
exit
main ENDP
;-------------------------------------------------------------
Get_frequencies PROC,
pString:PTR BYTE, ; points to string
pTable:PTR DWORD ; points to frequencey table
;
; Constructs a character frequency table. Each array position
; is indexed by its corresponding ASCII code.
;
; Returns: Each entry in the table contains a count of how
; many times that character occurred in the string.
;-------------------------------------------------------------
mov esi,pString
mov edi,pTable
cld ; clear Direction flag (forward)
L1: mov eax,0 ; clear upper bits of EAX
lodsb ; AL = [ESI], inc ESI
cmp al,0 ; end of string?
je Exit_proc ; yes: exit
shl eax,2 ; multiply by 4
inc DWORD PTR [edi + eax] ; inc table[AL]
jmp L1 ; repeat loop
Exit_proc:
ret
Get_frequencies ENDP
;-------------------------------------------------------------
DisplayTable PROC
;
; Display the non-empty entries of the frequency table.
; This procedure was not required, but it makes it easier
; to demonstrate that Get_frequencies works.
;-------------------------------------------------------------
.data
colonStr BYTE ": ",0
.code
call Crlf
mov ecx,LENGTHOF freqTable ; entries to show
mov esi,OFFSET freqTable
mov ebx,0 ; index counter
L1: mov eax,[esi] ; get frequency count
cmp eax,0 ; count = 0?
jna L2 ; if so, skip to next entry
mov eax,ebx ; display the index
call WriteChar
mov edx,OFFSET colonStr ; display ": "
call WriteString
mov eax,[esi] ; show frequency count
call WriteDec
call Crlf
L2: add esi,TYPE freqTable ; point to next table entry
inc ebx ; increment index
loop L1
call Crlf
ret
DisplayTable ENDP
END main
这是我迄今为止尝试实施彼得的回答中建议的强力搜索的结果:
.data
str2 BYTE "one two three",0
.code
main proc
mov edi,OFFSET str2
Mov esi,edi
Mov Ecx, 0 ;reset ecx to 0
Not Ecx ;set Ecx to -1 or highest possible integer
Mov Al, ' ' ;Initialize a1 to delimiter of (space) ' '
Cld ;Clear Direction Pointer
Repne Scasb ;scan edi one byte at a time until delimiter found
Not Ecx
Lea Eax, [ecx-1] ;Set Eax to index of found delimiter
Xchg Esi, Edi ;Take Edi which is now equal to string after found delimiter and put in esi
mov edx, esi
call WriteString
main endp
end main
这会打印“二三”,但我希望它打印“一”。