.model tiny

n       equ     80

.stack


.data

crlf    db      10,13,36                ;carriage return, line feed, terminator


string  db      n,?,n+1 dup (36)        ;max length n, return length, buffer
scratch db      n+1                     ;a scratch buffer for string work


prompt  db      'Gimme a string: $'     ;a prompt string


stringdone db   ?                       ;flag from getword, end of string


.code

main    proc

        mov     ax,@data
        mov     ds,ax                   ;set default data segment
        mov     es,ax                   ; and extra segment

        mov     ah,9                    ;show prompt
        mov     dx,offset prompt
        int     21h

        mov     ah,0Ah                  ;input string
        mov     dx,offset string
        int     21h


        mov     ah,9                    ;next line
        mov     dx,offset crlf
        int     21h



                                        ;this here little loop breaks up
                                        ;a tring and prints each word
                                        ;on a line


        mov     si,offset string+2      ;point to string to break up
m1:
        call    getword                 ;get a word to scratch buffer

        mov     ah,9                    ;print the word
        mov     dx,offset scratch
        int     21h

        mov     ah,9                    ;next line
        mov     dx,offset crlf
        int     21h

        cmp     stringdone,1            ;is that the end of string?
        jne     m1






xit:
        mov     ax,4C00h                ;to dos
        int     21h
main    endp


getword proc                            ;gets a word to scratch buffer
        mov     di,offset scratch

startword:
        lodsb                           ;get a char
        cmp     al,32                   ;a space?
        je      endword                 ;yeah, end word
        cmp     al,13                   ;a carriage return?
        je      endstring               ;yeah end string

        stosb                           ;store char
        jmp     startword               ;next char

endstring:
        mov     stringdone,1            ;set a flag to caller
                                        ; we done!
endword:
        mov     al,36                   ;terminate word
        stosb


        ret
getword endp


        end     main

