jmp start ;jump over the message storage area
WordCount dw ?;variable to store the wordcount in
i db 6 dup (0) ;place to store our outputted words
WordMessage db 10,13,"Number of Words: $"
bufsize equ 12 ;Size of buffer
buffer db bufsize dup(0) ; buffer to hold digits
message:
DB "It was a dark and stormy night the rain came down in torrents and there where three men in a cave."
DB "and one man said to another would you like to know a story. The man said yes so the story begins "
DB "as follows. It was a dark and stormy night. $"
start:
mov bx, offset message ;offset bx by the size of message
mov WordCount,0 ;start the counter at 0 words
countloop:
mov dl, [bx] ; get the character pointed to by bx in dl
inc bx ; add one to bx
cmp dl, 36 ; is the chracter a $, if it is we have finished the message
je printwordcount ; so jump to print the wordcount
mov ah,06
int 21h
cmp dl,32 ;see if it is a space
jne countloop ;return to start of the loop if it is not space
inc WordCount ;else it will be space, so add one to wordcount
jmp countloop
printwordcount:
lea dx,WordMessage
mov ah,9
int 21h
mov ax,WordCount ; 43605
mov cx,0 ; digit count = 0
mov di,OFFSET buffer
add di,(bufsize-1) ; leave 0 in last position
mov [byte ptr di+1],'$' ; Put $ at end for printing purposes
mov bx,10 ; will divide by 10
divide:
mov dx,0 ; set dividend to 0
div bx ; divide AX by 10
add dl,30h ; convert remainder to ASCII
dec di ; reverse through the buffer
mov [di],dl ; store ASCII digit
inc cx ; increment digit count
cmp ax,0 ; quotient > 0?
jnz divide ; yes: divide again
; Display the number
mov dx,di ; Start string
mov ah,9h ; write to screen
int 21h ; call MS-DOS
jmp quit
quit:
mov AH,4ch
int 21h