Hi guys i need to write a program that reads a line from the user and then searches and counts the number of alphabet letters in it and the number of any one punctuation.
i figured out how to go through the list of all uppercase and lowercase letters and punctuations ':' and ';' but wanna know how to compare with each character in the line.
im using assmbly lang for intel IA-32 and MASM 8
ive used macros to read the line and display it the definition of the macros is given
PLEASE HELP!
;------------------------------------------------------
mReadString MACRO varName:REQ
;
; Reads from standard input into a buffer.
; Receives: the name of the buffer. Avoid passing
; ECX and EDX as arguments.
;------------------------------------------------------
push ecx
push edx
mov edx,OFFSET varName
mov ecx,SIZEOF varName
call ReadString
pop edx
pop ecx
ENDM
;------------------------------------------------------
mWriteString MACRO buffer:REQ
;
; Writes a string variable to standard output.
; Receives: string variable name.
;------------------------------------------------------
push edx
mov edx,OFFSET buffer
call WriteString
pop edx
ENDM INCLUDE Irvine32.inc
INCLUDE Macros.inc
.data
mPutchar MACRO char
push eax
mov al,char
call WriteChar
pop eax
ENDM
sen BYTE 500 DUP(?)
.code
main PROC
call ClrScr ;Clear screen
mWrite<" Input a sentence to see the number of alphabet letters and punctuation",0dh,0ah>
mReadString sen
call Crlf
mWrite<"Sentence input by user..",0dh,0ah>
mWriteString sen
call Crlf
mov edx, OFFSET sen
call WriteChar
call Crlf
mov al,65
mov ecx,26
L1:
mPutchar al ;macro call
inc al
call Crlf
loop L1
mov al,97
mov ecx,26
L2:
mPutchar al
inc al
Call Crlf
loop L2
mov al,58
mov ecx,2
L3:
mPutchar al
inc al
Call Crlf
loop L3
exit
main ENDP
END mainthe simpliest way that I can find right now is saving each occurance of a char in a new array. Basically like this:
string DB 'Hello Demonoid2008!",$
yourloop:
XOR EAX,EAX
MOV Al,[string+EBX] ;reads char
MOV BL,[array+EAX] ;reads how ofter char occured till now...
INC BL ;and increments it 'cause it just occured right now again...
MOV [array+EAX],BL ;..and saves it again
LOOP yourloop
array RESB 256
After this loop you have a map of all occurances. You can now sort it and say "o: 3x, l: 2x" etc.
so i've this but there's some problem in the control of the program..can someone help me figure out??..thanks
TITLE counting
;Program description: Counts number of English alphabet letters and punctuation
INCLUDE Irvine32.inc
INCLUDE Macros.inc
.data
mPutchar MACRO char
push eax
mov al,char
call WriteChar
pop eax
ENDM
sen BYTE 500 DUP(?) ;mem allocation for sentence
bcount DWORD ? ;counter for bytes in the sentence
dcount DWORD 0
count BYTE 0 ;counter
ca DWORD 0 ;counters
cb DWORD 0 ;for
cc DWORD 0 ;letters A(or a) to Z(or z)
cd DWORD 0 ;and punctuation :
ce DWORD 0
cntf DWORD 0
cg DWORD 0
cnth DWORD 0
ci DWORD 0
cj DWORD 0
ck DWORD 0
cntl DWORD 0
cm DWORD 0
cn DWORD 0
co DWORD 0
cp DWORD 0
cq DWORD 0
cr DWORD 0
cnts DWORD 0
ct DWORD 0
cu DWORD 0
cv DWORD 0
cw DWORD 0
cntx DWORD 0
cy DWORD 0
cz DWORD 0
cpunc DWORD 0
.code
main PROC
call ClrScr ;Clear screen
mWrite<" Input a sentence to see the number of alphabet letters and punctuation",0dh,0ah> ;input prompt
mov edx, OFFSET sen
mov ecx, SIZEOF sen
call ReadString ;reading sentence
mov bcount, eax ;displaying length
call WriteDec ;of screen
call Crlf
mWrite<"Sentence input by user..",0dh,0ah> ;prints
call WriteString ;sentence input by user
mov esi, OFFSET sen ;assigning offset of sentence to esi
call Crlf
START:
inc dcount
mov count,0 ;initialize count to 0
mov al,65
mov ecx,26
L1:
inc count
mPutchar al ;macro call to display character
cmp al,BYTE PTR [esi] ;L1, L2 and L3 are labels for
je MAT ;traversing A-Z,a-z and ':' punctuation
inc al
call Crlf
loop L1
mov count,0
mov al,97
mov ecx,26
L2:
inc count
mPutchar al
cmp al,BYTE PTR[esi]
je MAT
inc al
Call Crlf
loop L2
mov count,0
L3:
mov al,58
mPutchar al
cmp al,BYTE PTR[esi]
je MAT
Call Crlf
MAT:
call matcher
mov ebx,dcount
cmp ebx,bcount
jne START
L5:
call Crlf
mWrite<"A's: ",0dh,0ah>
mov eax,ca
call WriteDec
call Crlf
mWrite<"B's: ",0dh,0ah>
mov eax,cb
call WriteDec
call Crlf
mWrite<"C's: ",0dh,0ah>
mov eax,cc
call WriteDec
call Crlf
mWrite<"D's: ",0dh,0ah>
mov eax,cd
call WriteDec
call Crlf
mWrite<"E's: ",0dh,0ah>
mov eax,ce
call WriteDec
call Crlf
mWrite<"F's: ",0dh,0ah>
mov eax,cntf
call WriteDec
call Crlf
mWrite<"G's: ",0dh,0ah>
mov eax,cg
call WriteDec
call Crlf
mWrite<"H's: ",0dh,0ah>
mov eax,cnth
call WriteDec
call Crlf
mWrite<"I's: ",0dh,0ah>
mov eax,ci
call WriteDec
call Crlf
mWrite<"J's: ",0dh,0ah>
mov eax,cj
call WriteDec
call Crlf
mWrite<"K's: ",0dh,0ah>
mov eax,ck
call WriteDec
call Crlf
mWrite<"L's: ",0dh,0ah>
mov eax,cntl
call WriteDec
call Crlf
mWrite<"M's: ",0dh,0ah>
mov eax,cm
call WriteDec
call Crlf
mWrite<"N's: ",0dh,0ah>
mov eax,cn
call WriteDec
call Crlf
mWrite<"O's: ",0dh,0ah>
mov eax,co
call WriteDec
call Crlf
mWrite<"P's: ",0dh,0ah>
mov eax,cp
call WriteDec
call Crlf
mWrite<"Q's: ",0dh,0ah>
mov eax,cq
call WriteDec
call Crlf
mWrite<"R's: ",0dh,0ah>
mov eax,cr
call WriteDec
call Crlf
mWrite<"S's: ",0dh,0ah>
mov eax,cnts
call WriteDec
call Crlf
mWrite<"T's: ",0dh,0ah>
mov eax,ct
call WriteDec
call Crlf
mWrite<"U's: ",0dh,0ah>
mov eax,cu
call WriteDec
call Crlf
mWrite<"V's: ",0dh,0ah>
mov eax,cv
call WriteDec
call Crlf
mWrite<"W's: ",0dh,0ah>
mov eax,cw
call WriteDec
call Crlf
mWrite<"X's: ",0dh,0ah>
mov eax,cntx
call WriteDec
call Crlf
mWrite<"Y's: ",0dh,0ah>
mov eax,cy
call WriteDec
call Crlf
mWrite<"Z's: ",0dh,0ah>
mov eax,cz
call WriteDec
call Crlf
mWrite<":'s: ",0dh,0ah>
mov eax,cpunc
call WriteDec
call Crlf
exit
main ENDP
matcher PROC
cmp count,1
je AF
cmp count,2
je BF
cmp count,3
je CF
cmp count,4
je DDF
cmp count,5
je EF
cmp count,6
je FF
cmp count,7
je GF
cmp count,8
je HF
cmp count,9
je IIF
cmp count,10
je JF
cmp count,11
je KF
cmp count,12
je LF
cmp count,13
je MF
cmp count,14
je NF
cmp count,15
je OF
cmp count,16
je PF
cmp count,17
je QF
cmp count,18
je RF
cmp count,19
je SF
cmp count,20
je TF
cmp count,21
je UF
cmp count,22
je VF
cmp count,23
je WF
cmp count,24
je XF
cmp count,25
je YF
cmp count,26
je ZF
cmp count,0
je PU
AF:
inc ca
add esi,2
jmp EX
BF:
inc cb
add esi,2
jmp EX
CF:
inc cc
add esi,2
jmp EX
DDF:
inc cd
add esi,2
jmp EX
EF:
inc ce
add esi,2
jmp EX
FF:
inc cntf
add esi,2
jmp EX
GF:
inc cg
add esi,2
jmp EX
HF:
inc cnth
add esi,2
jmp EX
IIF:
inc ci
add esi,2
jmp EX
JF:
inc cj
add esi,2
jmp EX
KF:
inc ck
add esi,2
jmp EX
LF:
inc cntl
add esi,2
jmp EX
MF:
inc cm
add esi,2
jmp EX
NF:
inc cn
add esi,2
jmp EX
OF:
inc co
add esi,2
jmp EX
PF:
inc cp
add esi,2
jmp EX
QF:
inc cq
add esi,2
jmp EX
RF:
inc cr
add esi,2
jmp EX
SF:
inc cnts
add esi,2
jmp EX
TF:
inc ct
add esi,2
jmp EX
UF:
inc cu
add esi,2
jmp EX
VF:
inc cv
add esi,2
jmp EX
WF:
inc cw
add esi,2
jmp EX
XF:
inc cntx
add esi,2
jmp EX
YF:
inc cy
add esi,2
jmp EX
ZF:
inc cz
add esi,2
jmp EX
PU:
inc cpunc
add esi,2
EX:
ret
matcher ENDP
END mainso i finally figured it out here's the code: it counts occurence of each letter, doesn't matter if it's upper case or lower and counts punctuatinos(;:,.?!)
TITLE counting
;Program description: Counts number of English alphabet letters and punctuation
INCLUDE Irvine32.inc
INCLUDE Macros.inc
.data
sen BYTE 500 DUP(?) ;mem allocation for sentence
bcount DWORD ? ;counter for bytes in the sentence
dcount DWORD 0
count BYTE 0 ;counter
ca DWORD 0 ;counters
cb DWORD 0 ;for
cc DWORD 0 ;letters A(or a) to Z(or z)
cd DWORD 0 ;and punctuation :
ce DWORD 0
cntf DWORD 0
cg DWORD 0
cnth DWORD 0
ci DWORD 0
cj DWORD 0
ck DWORD 0
cntl DWORD 0
cm DWORD 0
cn DWORD 0
co DWORD 0
cp DWORD 0
cq DWORD 0
cr DWORD 0
cnts DWORD 0
ct DWORD 0
cu DWORD 0
cv DWORD 0
cw DWORD 0
cntx DWORD 0
cy DWORD 0
cz DWORD 0
cpunc DWORD 0
.code
main PROC
call ClrScr ;Clear screen
mWrite<" Input a sentence to see the number of alphabet letters and punctuation ':'",0dh,0ah> ;input prompt
mov edx, OFFSET sen
mov ecx, SIZEOF sen
call ReadString ;reading sentence
mov bcount, eax ;displaying length
call WriteDec ;on screen
call Crlf
mWrite<"Sentence input by user..",0dh,0ah> ;prints
call WriteString ;sentence input by user
mov esi, OFFSET sen ;assigning offset of sentence to esi
call Crlf
START:
;inc dcount
mov al,65
mov ecx,26
L1:
inc count
cmp al,BYTE PTR [esi]
je MAT ;traversing A-Z,a-z and punctuations
inc al ;and comparing to their ascii values
loop L1 ;and calling procedure to count the letter/punct.
mov count,0
mov al,97
mov ecx,26
L2:
inc count
cmp al,BYTE PTR[esi]
je MAT
inc al
loop L2
L3:
mov count,0
mov al,58
cmp al,BYTE PTR[esi]
je MAT
L4:
mov count,27
mov al,46
cmp al,BYTE PTR[esi]
je MAT
L6:
mov count,28
mov al,44
cmp al,BYTE PTR[esi]
je MAT
L7:
mov count,29
mov al,59
cmp al,BYTE PTR[esi]
je MAT
L8:
mov count,30
mov al,63
cmp al,BYTE PTR[esi]
je MAT
L9:
mov count,31
mov al,33
cmp al,BYTE PTR[esi]
je MAT
inc dcount
add esi,1
mov eax,dcount
cmp eax,bcount
jne START
jmp L5
MAT:
inc dcount ;incrementing counter for number of characters processed
call matcher ;calling counter function
mov eax,dcount
cmp eax,bcount ;comparing if all characters are processed
jne START
L5: ;Displaying the final number of occurence of each value
call Crlf
mWrite<"A's: ">
mov eax,ca
call WriteDec
call Crlf
mWrite<"B's: ">
mov eax,cb
call WriteDec
call Crlf
mWrite<"C's: ">
mov eax,cc
call WriteDec
call Crlf
mWrite<"D's: ">
mov eax,cd
call WriteDec
call Crlf
mWrite<"E's: ">
mov eax,ce
call WriteDec
call Crlf
mWrite<"F's: ">
mov eax,cntf
call WriteDec
call Crlf
mWrite<"G's: ">
mov eax,cg
call WriteDec
call Crlf
mWrite<"H's: ">
mov eax,cnth
call WriteDec
call Crlf
mWrite<"I's: ">
mov eax,ci
call WriteDec
call Crlf
mWrite<"J's: ">
mov eax,cj
call WriteDec
call Crlf
mWrite<"K's: ">
mov eax,ck
call WriteDec
call Crlf
mWrite<"L's: ">
mov eax,cntl
call WriteDec
call Crlf
mWrite<"M's: ">
mov eax,cm
call WriteDec
call Crlf
mWrite<"N's: ">
mov eax,cn
call WriteDec
call Crlf
mWrite<"O's: ">
mov eax,co
call WriteDec
call Crlf
mWrite<"P's: ">
mov eax,cp
call WriteDec
call Crlf
mWrite<"Q's: ">
mov eax,cq
call WriteDec
call Crlf
mWrite<"R's: ">
mov eax,cr
call WriteDec
call Crlf
mWrite<"S's: ">
mov eax,cnts
call WriteDec
call Crlf
mWrite<"T's: ">
mov eax,ct
call WriteDec
call Crlf
mWrite<"U's: ">
mov eax,cu
call WriteDec
call Crlf
mWrite<"V's: ">
mov eax,cv
call WriteDec
call Crlf
mWrite<"W's: ">
mov eax,cw
call WriteDec
call Crlf
mWrite<"X's: ">
mov eax,cntx
call WriteDec
call Crlf
mWrite<"Y's: ">
mov eax,cy
call WriteDec
call Crlf
mWrite<"Z's: ">
mov eax,cz
call WriteDec
call Crlf
mWrite<"Punctuation ':''s : ">
mov eax,cpunc
call WriteDec
call Crlf
exit
main ENDP
matcher PROC
;comparing the count value to see what character is found
cmp count,1
je AF
cmp count,2
je BF
cmp count,3
je CF
cmp count,4
je DDF
cmp count,5
je EF
cmp count,6
je FF
cmp count,7
je GF
cmp count,8
je HF
cmp count,9
je IIF
cmp count,10
je JF
cmp count,11
je KF
cmp count,12
je LF
cmp count,13
je MF
cmp count,14
je NF
cmp count,15
je OF
cmp count,16
je PF
cmp count,17
je QF
cmp count,18
je RF
cmp count,19
je SF
cmp count,20
je TF
cmp count,21
je UF
cmp count,22
je VF
cmp count,23
je WF
cmp count,24
je XF
cmp count,25
je YF
cmp count,26
je ZF
cmp count,0
je PU
cmp count,27
je PU
cmp count,28
je PU
cmp count,29
je PU
cmp count,30
je PU
cmp count,31
je PU
AF:
inc ca ;Incrementing each character's count as it occurs
add esi,1 ;And moving the pointer to next character
jmp EX
BF:
inc cb
add esi,1
jmp EX
CF:
inc cc
add esi,1
jmp EX
DDF:
inc cd
add esi,1
jmp EX
EF:
inc ce
add esi,1
jmp EX
FF:
inc cntf
add esi,1
jmp EX
GF:
inc cg
add esi,1
jmp EX
HF:
inc cnth
add esi,1
jmp EX
IIF:
inc ci
add esi,1
jmp EX
JF:
inc cj
add esi,1
jmp EX
KF:
inc ck
add esi,1
jmp EX
LF:
inc cntl
add esi,1
jmp EX
MF:
inc cm
add esi,1
jmp EX
NF:
inc cn
add esi,1
jmp EX
OF:
inc co
add esi,1
jmp EX
PF:
inc cp
add esi,1
jmp EX
QF:
inc cq
add esi,1
jmp EX
RF:
inc cr
add esi,1
jmp EX
SF:
inc cnts
add esi,1
jmp EX
TF:
inc ct
add esi,1
jmp EX
UF:
inc cu
add esi,1
jmp EX
VF:
inc cv
add esi,1
jmp EX
WF:
inc cw
add esi,1
jmp EX
XF:
inc cntx
add esi,1
jmp EX
YF:
inc cy
add esi,1
jmp EX
ZF:
inc cz
add esi,1
jmp EX
PU:
inc cpunc
add esi,1
EX:
mov count,0 ;reinitializing counter
ret
matcher ENDP
END main