i am writing a assembly code to count the number of words in a sentence.
below is my coding:

.model small
.stack 200h
.386
.data

message1 db 10,13, 'Word counter : Enter the sentence to be calculated$'
result db 10,13, 'Number of words : $'
counter db 5,6 dup(0)
sentence db 10,13, 'hello world and me. $' 

.code

Start:
	mov ax,seg message1
	mov ds,ax
	mov dx,offset message1
	mov ah,09h
	int 21h

	mov ax,seg sentence
	mov ds,ax
	mov bx,offset sentence
	mov ax,0
	
	mov cx,0
	mov di,2

check1:
	mov al,[bx+di]

	cmp al,'.'
	je check2
	cmp al,','
	je check2
	cmp al,' '
	je count
	cmp al,'$'
	je done
	jmp skip

check2:
	inc cx
	inc di
	mov al,[bx+di]
	cmp al,' '
	je skip
	jmp check1

skip:
	inc di
	jmp check1

count:
	inc cx
	inc di
	jmp check1

done:
	mov ax,cx
	aaa
	add ax,3030h
	
	cmp ah,30h
	je singleNumber
	jmp doubleNumber

singleNumber:

	mov bx,offset counter
	mov [bx+2],al
	mov al,'$'
	mov [bx+3],al
	jmp print

doubleNumber:
	
	mov bx,offset counter
	mov [bx+2],ah
	mov [bx+3],al
	mov al,'$'
	mov [bx+4],al

print:

	mov dx,offset result
	mov ah,09h
	int 21h

	mov dx,offset counter
	mov ah,09h
	int 21h

.exit
end start

when i run it, there is a ascii symbol infront of the number of word.
isit something wrong with my coding?
please help.
thank you

You loaded your counter variable starting at counter+2 which
causes the ASCII character 5 to be printed on the screen,
that is the initializer you used for the first byte of the
array at counter:

Make these changes:

singleNumber:
mov bx,offset counter
mov [bx+0],al ; changed +2 to +0
mov al,'$' 
mov [bx+1],al ; changed +3 to +1
jmp print
doubleNumber:
mov bx,offset counter
mov [bx+0],ah ; changed +2 to +0
mov [bx+1],al ; changed +3 to +1
mov al,'$'
mov [bx+2],al ; changed +4 to +2

That will eliminate the club symbol (ASCI '\5') from being
printed on the console.

yup2, that solved the problem..
thank you =)
but then, isnt the [offset counter+2] is the memory location that store the total number of count?
i am a bit confuse this,
sorry, i am still a beginner to assembly..

This article has been dead for over six months. Start a new discussion instead.