0% found this document useful (0 votes)
16 views3 pages

MP Practical 12

This document contains assembly code for a program that reads a text file and counts the number of blank spaces, lines, and occurrences of a specified character ('e'). It utilizes DOS interrupt calls to open and read the file, and an external procedure to perform the counting. The results of the counts are displayed at the end of the execution.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

MP Practical 12

This document contains assembly code for a program that reads a text file and counts the number of blank spaces, lines, and occurrences of a specified character ('e'). It utilizes DOS interrupt calls to open and read the file, and an external procedure to perform the counting. The results of the counts are displayed at the end of the execution.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No 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

; Open the file


MOV AH,3Dh ; DOS function: open file
MOV AL,0 ; Read mode
LEA DX,filename
INT 21h
JC FILE_ERROR
MOV handle,AX ; Save file handle

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

PUSH AX ; Save number of bytes read


PUSH DS
PUSH OFFSET buffer
PUSH OFFSET charToFind
CALL CountStats ; Call far procedure in Program_2
ADD SP,8 ; Clean up stack

JMP READ_LOOP
DONE:
; Close file
MOV AH,3Eh
MOV BX,handle
INT 21h

; Display result (optional)

MOV AH,4Ch
INT 21h

FILE_ERROR:
MOV AH,09h
LEA DX,errMsg
INT 21h
JMP DONE

errMsg DB "File Error$"

PUBLIC START, blankCount, lineCount, charCount


END START

.MODEL LARGE
.STACK 100h

.DATA
; Nothing needed here; using variables from Program_1

.CODE
PUBLIC CountStats
EXTERN blankCount:WORD, lineCount:WORD, charCount:WORD

CountStats PROC FAR


PUSH BP
MOV BP,SP
PUSH SI
PUSH CX
PUSH AX
PUSH BX
PUSH DX

; 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

You might also like