Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a word

Status
Not open for further replies.

chris921

Technical User
Mar 9, 2004
82
0
0
GB
I want to know just to go from here; I've got to return whether a paticular word is found or not in a text file.

I've got this so far, just having problems finishing it off, can anyone help?

By the way the word will be specified by the user.

Code:
JMP START		; Skip over the message

;The message and string terminator

welmsg:     	    DB	'Please enter a word to search for and then press enter$' 
correctmsg: 	    DB	'Your word was found successfully$'
incorrectmsg: 	    DB	'Your search returned no results$' 
filename:      	    DB	'asmtext.ASM',0 
Buffer:		    DB		146 DUP(?)	;data buffer

START:

	MOV AH,3DH		; Open file
	MOV AL,0		; Open for reading
 	MOV DX,filename
 	INT 21h			; Calls DOS
 	MOV BX,AX		; save file handle in BX
	MOV AH,3FH		; Function to read
 	MOV CX,146		; Number of bytes to read
 	MOV DX,Buffer
 	MOV ah,1h
	mov DH, al
	mov ah, 1h
	MOV AH,0AH
 	INT 21h
 	
 	
FINISH:
	MOV AH,3EH		; Close File
	INT 21H
	MOV AH, 4Ch		; MS-DOS function-number for successful exit
 	INT 21h			; go back to the operating system
 
I don't see any code for searching for the word? Why don't you try something out before you post here (attempt something, at least).
 
This is what I have now.

It will either print a Y for every word, or a N for everyword, depending whether I put REPNE or REPE towards the end.

Can anyone see anything I've done majorly wrong?

Code:
JMP START		; Skip over the message

;The message and string terminator

welmsg:     	    DB	'Please enter a word to search for and then press enter$'
correctmsg: 	    DB	'Your search was carried out successfully$'
incorrectmsg: 	    DB	'Your search returned no results$'
filename:   	    DB	'asmtext.ASM',0
Buffer:		    DB		324 DUP(?)	;data buffer
MESSAGE:       	    DB  'It was a dark and stormy night the rain came down in torrents and there where three men in a cave and one man said to another would you like to know a story. The man said yes so the story begins as follows. It was a dark and stormy night. $'

START:
	MOV AH,3DH	;Open file
	MOV AL,0	;Open for reading
	MOV DX,filename
	INT 21h		; Call DOS
	MOV BX,AX	; save file handle in BX
	MOV AH,3FH	; function to read
	MOV CX,324	; Number of bytes to read
	MOV DX,Buffer
	MOV AH,1h
	MOV DH, AL
	MOV AH, 1H
	MOV AH,0ah
	INT 21h

;search
	MOV AX, CS
	MOV DS, AX
      	MOV ES, AX
	LEA SI, Buffer
	LEA DI, message
		        

	        
; compare until equal:
	MOV DX,message
	MOV CX, 324
	CLD
	XOR AH,AH
	MOV AH,Buffer[2]
	REPNE SCASB
	JZ not_equal
	MOV AL, 'Y'	 
	MOV AH, 0Eh
	INT 10h
	JMP FINISH
	
not_equal:
	
	MOV AL, 'N'
	MOV AH, 0Eh
	INT 10h
	JMP FINISH

FINISH:
	MOV AH,3EH	;Close File
	MOV AH, 4Ch	; MS-DOS function-number for successful exit
	INT 21h		; go back to the operating system
 
By rights your assembler should have objected to a few things here.

mov dx, filename.
I think this should have been mov dx, offset filename. mov dx, filename should take a word at filename and put it in dx. Tasm would have objected that filename points to a byte (db!) not a word.

same again mov dx, buffer

At the point where you do scasb, al (which is what scasb uses) still contains the low byte of cs; I don't think you set ax correctly before scasb.

You're currently only searching for a byte, but I assume you know that.
Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top