Words Count (1)

download Words Count (1)

of 6

Transcript of Words Count (1)

  • 7/30/2019 Words Count (1)

    1/6

    001 TITLE (wCount.asm)002003 .model large004 .stack 4096005 .386006007 .data008009 msg byte 0dh, 0ah, 0dh, 0ah, "Demonisch Basic Word Count", 0dh, 0ah, "Type some text (maximum 100 chars) then press enter", 0dh, 0ah, "to see how many wordsthe text contains", 0dh, 0ah, 0dh, 0ah, ":$"010 msgN byte 0dh, 0ah, 0dh, 0ah, "Number of Words: $"011 msgEnd byte 0dh, 0ah, 0dh, 0ah, "Press any key to continue", 0dh, 0ah, 0dh,0ah, "$"012013 input byte 100 DUP ('$')014015 num word 0000h ;Used so data ends on a word boundary016017 .code018019 main PROC020021 mov ax, SEG msg

    022 mov ds, ax023024 mov ah, 9025 mov dx, OFFSET msg026 int 21h027028 mov cx, 0000h ;CL counts how many char, CH counts reading the chars back029 cld030031 lea di, input032 mov ah, 1033034 getText:

    035036 int 21h037038 cmp al, 0dh ;Return039 je wordC040041 mov [di], al042043 inc di044 inc cl045046 cmp cl, 64h ;Keep looping as long as it is under 100 chars047 jl getText

    048049 wordC:050051 cmp cl, 00h ;If no chars were entered052 je proEnd053054 mov ax, 0000h ;Used to remember if first char was space055 mov bx, 0000h ;Used to count how many words056 mov dx, 0000h ;Used to remember the last index of a space char057

  • 7/30/2019 Words Count (1)

    2/6

    058 lea di, input059060 count:061062 mov al, [di]063 cmp al, 20h ;If a space is found064 je hanS065066 count2:067068 inc di069 inc ch070071 cmp ch, cl ;Keep looping while not all entered chars have been read072 jl count073074 cmp ch, 01h ;Handle differently if only one char was entered075 je hanCF076077 hanCT:078079 inc dl080081 cmp dl, ch ;If the last char was a space then don't count it

    082 je results083084 addWord:085086 inc bl087088 jmp results089090 hanCF: ;Only one char was entered091092 cmp ah, 01h ;Was it a space093 je results094

    095 jmp addWord096 ;HANDLE SPACES097 hanS:098099 cmp ch, 00h ;At the start dl will be 0, so must check first char myself100 je hanSF101102 cmp ch, 01h ;DL at the start is 0, but if a word such as "A" has been entered it would think it was a double space103 je hanSS104105 mov dh, ch106 dec dh

    107108 cmp dh, dl ;If last char was a space (prevent double space)109 je hanSE110111 jmp hanSYes112113 hanSF: ;First char is a space114115 mov ah, 01h116 jmp hanSE

  • 7/30/2019 Words Count (1)

    3/6

    117118 hanSS: ;Second char is a space119120 cmp ah, 01h ;Was the first char a space121 je hanSE122123 hanSYes:124125 inc bl126127 hanSE:128129 mov dl, ch ;DL stores the last index of a space char130 jmp count2131 ;END HANDLE SPACES132 results:133134 mov ah, 9135 mov dx, OFFSET msgN136 int 21h137138 call disNum139140 proEnd:

    141142 mov ah,9143 mov dx,OFFSET msgEnd144 int 21h145146 mov ah, 1 ;Wait for any key, allows the user to read any output147 int 21h ;before the programme ends and the window closes148149 mov ah, 4ch150 int 21h151152 main ENDP153 ;---------------------------------------------------------------------------

    -----;154 disNum PROC155156 .data157158 numOut word 0000h159160 .code161162 mov numOut, bx ;Store a copy before pushing, so the original after this method stays the same163164 push ax

    165 push bx166 push cx167 push dx168 push si169 push di170171 mov bx, numOut ;Use this copy172173 cmp bx, 0000h ;If number is greater then or equal to 0174 jge disNumF

  • 7/30/2019 Words Count (1)

    4/6

    175176 mov ah, 6177 mov dl, 2Dh ;Display the - sign178 int 21h179180 neg bx ;The filter requires positive numbers, so turn bx into positive181182 disNumF:183184 mov cl, 00h185186 cmp bx, 2710h ; 10000187 jge getTTho188189 cmp bx, 03E8h ; 1000190 jge getThou191192 cmp bx, 0064h ; 100193 jge getHun194195 cmp bx, 000Ah ; 10196 jge getTen197198 jmp disUnit

    199200 getTTho:201202 inc cl203 sub bx, 2710h204205 cmp bx, 2710h206 jge getTTho ;While answer is greater then or equal to 10,000 keep looping207208 disTTho:209210 add cl, 30h211

    212 mov ah, 6213 mov dl, cl214 int 21h215216 mov cl, 00h217218 cmp bx, 3E8h219 jl disThou220221 getThou:222223 inc cl224 sub bx, 3E8h

    225226 cmp bx, 3E8h227 jge getThou228229 disThou:230231 add cl, 30h232233 mov ah, 6234 mov dl, cl

  • 7/30/2019 Words Count (1)

    5/6

    235 int 21h236237 mov dl, 2Ch ;Display thousand comma238 int 21h239240 mov cl, 00h241242 cmp bx, 64h243 jl disHun244245 getHun:246247 inc cl248 sub bx, 64h249250 cmp bx, 64h251 jge getHun252253 disHun:254255 add cl, 30h256257 mov ah, 6258 mov dl, cl

    259 int 21h260261 mov cl, 00h262263 cmp bx, 0Ah264 jl disTen265266 getTen:267268 inc cl269 sub bx, 0Ah270271 cmp bx, 0Ah

    272 jge getTen273274 disTen:275276 add cl, 30h277278 mov ah, 6279 mov dl, cl280 int 21h281282 mov cl, 00h283284 cmp bx, 01h

    285 jl disUnit286287 disUnit:288289 add bl, 30h290291 mov ah, 6292 mov dl, bl293 int 21h294

  • 7/30/2019 Words Count (1)

    6/6

    295 pop di ;Restore previous values, most importantly bx in case the user wantsto use that number again296 pop si297 pop dx298 pop cx299 pop bx300 pop ax301302 ret303304 disNum ENDP305306 END main