lilo12 0 Newbie Poster
TITLE charwordcount
DOSSEG
.MODEL SMALL
.STACK 100h
.DATA

msg1 db "Enter a sentence please: $"
msg2 db "		Do you want to try again (Y/N)? $"
msg3 db "Character count = $"
msg4 db "Word count = $"
sentence db 84 dup("$")
try db 5 dup("$")
crlf db 13, 10, "$"
error1 db "*** Error: Null Input ***$"
error2 db "*** Error: No period or invalid terminating character at the end of the sentence ***$"
error3 db "*** Error: Two or more spaces between words ***$"
count db 00

.CODE

BEGIN: 	mov ax, @data
	mov ds, ax
	mov es, ax

; clear screen
	mov al, 03h
	mov ah, 00h
	int 10h

; print msg1	
ulit:	lea dx, msg1
	mov ah, 09h
	int 21h

; get sentence
	mov byte ptr sentence, 81
	lea dx, sentence
	mov ah, 0Ah
	int 21h

; next line
	lea dx, crlf
	mov ah, 09h
	int 21h

; check for null value and output error msg
	mov si, 0002
	mov al, sentence[si]
	cmp al, 0dh
	je err1
	cmp al, ' '
	je err1

; compare for two or more spaces
	;mov si, 0002
	mov si, 0000
c10:	inc si
	mov al, sentence[si]
	cmp al, '$'
	je next
	cmp al, ' '
	je L11
	jmp c10

L11: 	inc si
	mov al, sentence[si]
	cmp al, ' '
	je err3
	dec si
	jmp c10

; compare for terminaton
	;mov si, 0001
next:	mov si, 0000
L1:	inc si
	mov al, sentence[si]
	cmp al, '.'
	je cchar
	cmp al, '$'
	je err2
	jmp L1

; error message for null input
err1:	lea dx, error1
	mov ah, 09h
	int 21h
	jmp tanong

err3:	lea dx, error3
	mov ah, 09h
	int 21h
	jmp tanong

; print character message
cchar:	lea dx, msg3
	mov ah, 09h
	int 21h
	
; count for characters
	mov si, 0001
	mov al, sentence[si]
	sub al, 01
	aam
	add ax, 3030h
	push ax

; print number of char
	mov dl, ah
	mov ah, 02h
	int 21h
	pop ax
	mov dl, al
	mov ah, 02h
	int 21h
	
; next line
	lea dx, crlf
	mov ah, 09h
	int 21h

; print msg4	
	lea dx, msg4
	mov ah, 09h
	int 21h
	
; count number of words
words:	mov byte ptr count, 00
	;mov si, 0002
	mov si, 0000
c1:	inc si
	mov al, sentence[si]
	cmp al, '$'
	je pcount
	cmp al, ' ' 
	je L10
	jmp c1

; start counting words
L10: 	inc byte ptr count
	jmp c1

; print number of words
pcount:	mov al, count
	add al, 01
	aam
	add ax, 3030h
	push ax

; print number of words
	mov dl, ah
	mov ah, 02h
	int 21h
	pop ax
	mov dl, al
	mov ah, 02h
	int 21h
	jmp tanong

; error message for invalid terminator
err2:	lea dx, error2
	mov ah, 09h
	int 21h
	jmp tanong


; next line
tanong:	lea dx, crlf
	mov ah, 09h
	int 21h

; try again
	lea dx, msg2
	mov ah, 09h
	int 21h

; get try answer
	mov byte ptr try, 2
	lea dx, try
	mov ah, 0Ah
	int 21h

; next line
	lea dx, crlf
	mov ah, 09h
	int 21h

; check for answer
	mov si, 0002
	mov al, try[si]
	cmp al, 'Y'
	Jne tapos
	jmp ulit

; next line
	lea dx, crlf
	mov ah, 09h
	int 21h

tapos:

	mov ah, 4ch
	int 21h
	end BEGIN

;;; Hi guys, I'm really new in Assembly programming.. This code is running pretty good.. but i have a problem when it loops.. i think the counter is not resetting.. it gave me wrong values for the word count.. please help.. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.