You are on page 1of 6

001 TITLE (wCount.asm) 002 003 .model large 004 .stack 4096 005 .386 006 007 .

data 008 009 msg byte 0dh, 0ah, 0dh, 0ah, "Demonisch Basic Word Count", 0dh, 0ah, "Type s ome text (maximum 100 chars) then press enter", 0dh, 0ah, "to see how many words the 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, "$" 012 013 input byte 100 DUP ('$') 014 015 num word 0000h ;Used so data ends on a word boundary 016 017 .code 018 019 main PROC 020 021 mov ax, SEG msg 022 mov ds, ax 023 024 mov ah, 9 025 mov dx, OFFSET msg 026 int 21h 027 028 mov cx, 0000h ;CL counts how many char, CH counts reading the chars back 029 cld 030 031 lea di, input 032 mov ah, 1 033 034 getText: 035 036 int 21h 037 038 cmp al, 0dh ;Return 039 je wordC 040 041 mov [di], al 042 043 inc di 044 inc cl 045 046 cmp cl, 64h ;Keep looping as long as it is under 100 chars 047 jl getText 048 049 wordC: 050 051 cmp cl, 00h ;If no chars were entered 052 je proEnd 053 054 mov ax, 0000h ;Used to remember if first char was space 055 mov bx, 0000h ;Used to count how many words 056 mov dx, 0000h ;Used to remember the last index of a space char 057

058 lea di, input 059 060 count: 061 062 mov al, [di] 063 cmp al, 20h ;If a space is found 064 je hanS 065 066 count2: 067 068 inc di 069 inc ch 070 071 cmp ch, cl ;Keep looping while not all entered chars have been read 072 jl count 073 074 cmp ch, 01h ;Handle differently if only one char was entered 075 je hanCF 076 077 hanCT: 078 079 inc dl 080 081 cmp dl, ch ;If the last char was a space then don't count it 082 je results 083 084 addWord: 085 086 inc bl 087 088 jmp results 089 090 hanCF: ;Only one char was entered 091 092 cmp ah, 01h ;Was it a space 093 je results 094 095 jmp addWord 096 ;HANDLE SPACES 097 hanS: 098 099 cmp ch, 00h ;At the start dl will be 0, so must check first char myself 100 je hanSF 101 102 cmp ch, 01h ;DL at the start is 0, but if a word such as "A" has been entere d it would think it was a double space 103 je hanSS 104 105 mov dh, ch 106 dec dh 107 108 cmp dh, dl ;If last char was a space (prevent double space) 109 je hanSE 110 111 jmp hanSYes 112 113 hanSF: ;First char is a space 114 115 mov ah, 01h 116 jmp hanSE

117 118 hanSS: ;Second char is a space 119 120 cmp ah, 01h ;Was the first char a space 121 je hanSE 122 123 hanSYes: 124 125 inc bl 126 127 hanSE: 128 129 mov dl, ch ;DL stores the last index of a space char 130 jmp count2 131 ;END HANDLE SPACES 132 results: 133 134 mov ah, 9 135 mov dx, OFFSET msgN 136 int 21h 137 138 call disNum 139 140 proEnd: 141 142 mov ah,9 143 mov dx,OFFSET msgEnd 144 int 21h 145 146 mov ah, 1 ;Wait for any key, allows the user to read any output 147 int 21h ;before the programme ends and the window closes 148 149 mov ah, 4ch 150 int 21h 151 152 main ENDP 153 ;-------------------------------------------------------------------------------; 154 disNum PROC 155 156 .data 157 158 numOut word 0000h 159 160 .code 161 162 mov numOut, bx ;Store a copy before pushing, so the original after this meth od stays the same 163 164 push ax 165 push bx 166 push cx 167 push dx 168 push si 169 push di 170 171 mov bx, numOut ;Use this copy 172 173 cmp bx, 0000h ;If number is greater then or equal to 0 174 jge disNumF

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

mov ah, 6 mov dl, 2Dh ;Display the - sign int 21h neg bx ;The filter requires positive numbers, so turn bx into positive disNumF: mov cl, 00h cmp bx, 2710h ; 10000 jge getTTho cmp bx, 03E8h ; 1000 jge getThou cmp bx, 0064h ; 100 jge getHun cmp bx, 000Ah ; 10 jge getTen jmp disUnit getTTho: inc cl sub bx, 2710h cmp bx, 2710h jge getTTho ;While answer is greater then or equal to 10,000 keep looping disTTho: add cl, 30h mov ah, 6 mov dl, cl int 21h mov cl, 00h cmp bx, 3E8h jl disThou getThou: inc cl sub bx, 3E8h cmp bx, 3E8h jge getThou disThou: add cl, 30h mov ah, 6 mov dl, cl

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

int 21h mov dl, 2Ch ;Display thousand comma int 21h mov cl, 00h cmp bx, 64h jl disHun getHun: inc cl sub bx, 64h cmp bx, 64h jge getHun disHun: add cl, 30h mov ah, 6 mov dl, cl int 21h mov cl, 00h cmp bx, 0Ah jl disTen getTen: inc cl sub bx, 0Ah cmp bx, 0Ah jge getTen disTen: add cl, 30h mov ah, 6 mov dl, cl int 21h mov cl, 00h cmp bx, 01h jl disUnit disUnit: add bl, 30h mov ah, 6 mov dl, bl int 21h

295 pop di ;Restore previous values, most importantly bx in case the user wants to use that number again 296 pop si 297 pop dx 298 pop cx 299 pop bx 300 pop ax 301 302 ret 303 304 disNum ENDP 305 306 END main

You might also like