;; count words in a string ;; .model small .586 .stack 100h .data msg1 db "Enter a string (max 40 chars): ",0 msg2 db "Number of words in string is: ",0 ;;leave space in buffer for one char past string for terminator buf db 40 db 0 buffer db 40+1 dup (?) .code extern Crlf:proc, Writestring:proc, Writeint:proc main proc mov ax,@data mov ds,ax call Crlf mov dx,offset msg1 call Writestring mov ah,0ah mov dx, offset buf int 21h call crlf ;; mark end of string mov cl,[buf+1] ;; get count of chars entered xor ch,ch mov bx,offset buffer add bx,cx ;; add count of chars entered mov [bx],ch ;; write a null byte at end of string ;; count words xor cx,cx ;; cx will have count mov bx,offset buffer lp1: mov al,[bx] inc bx ;; point at next byte cmp al,0 je exit ;; at end of string cmp al,20h ;; check if space je lp1 ;; loop while space inc cx ;; found a word lp2: mov al,[bx] ;; get next char inc bx cmp al,0 je exit cmp al,20h jne lp2 ;; loop while space jmp lp1 ;; look for other spaces exit: mov dx,offset msg2 call Writestring mov ax,cx mov bx,10 call writeint ;; print the integer call crlf Mov ax, 4c00h ;exit Int 21h Main endp end main