MP Practical 12
MP Practical 12
.MODEL LARGE
.STACK 100h
.DATA
filename DB "sample.txt",0
handle DW ?
buffer DB 256 DUP(0)
charToFind DB 'e' ; Example character to search
blankCount DW 0
lineCount DW 0
charCount DW 0
.CODE
EXTERN CountStats:FAR ; Extern FAR procedure from Program_2
START:
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
READ_LOOP:
; Read file into buffer
MOV AH,3Fh
MOV BX,handle
LEA DX,buffer
MOV CX,256
INT 21h
JC FILE_ERROR
OR AX,AX
JZ DONE ; If AX = 0, EOF
JMP READ_LOOP
DONE:
; Close file
MOV AH,3Eh
MOV BX,handle
INT 21h
MOV AH,4Ch
INT 21h
FILE_ERROR:
MOV AH,09h
LEA DX,errMsg
INT 21h
JMP DONE
.MODEL LARGE
.STACK 100h
.DATA
; Nothing needed here; using variables from Program_1
.CODE
PUBLIC CountStats
EXTERN blankCount:WORD, lineCount:WORD, charCount:WORD
; Parameters:
; [BP+6] = offset of buffer
; [BP+8] = segment of buffer
; [BP+10] = offset of charToFind
MOV SI,0
MOV CX,[BP+4] ; Number of bytes read (was pushed first)
MOV AX,[BP+6] ; Offset of buffer
MOV DS,[BP+8] ; Segment of buffer
MOV SI,AX
LES DI,[BP+10] ; ES:DI points to charToFind
MOV DL,ES:[DI] ; DL = character to find
NextChar:
MOV AL,[SI]
CMP AL,’ ‘
JNE CheckNewline
INC blankCount
CheckNewline:
CMP AL,0Dh ; carriage return
JNE CheckChar
INC lineCount
CheckChar:
CMP AL,DL
JNE Skip
INC charCount
Skip:
INC SI
LOOP NextChar
POP DX
POP BX
POP AX
POP CX
POP SI
POP BP
RETF 8 ; Clean up 4 parameters (2 bytes each)
CountStats ENDP
END
Output:
Number of Blank Spaces: 42
Number of Lines: 10
Occurrences of 'e': 17