I am a newbie, while my teacher give me a big problem~
Find a string which is specified in a file. If it exists, print the first place where the string is in, or print there is no such a string.
For example: Find c;\1.txt MyGod!
It will find if there is a string "MyGod!" in the 1.txt, and you can modify this string.
Only use assembly language, and my teacher doesn't let me use win32 assembly language~
please help me~ thank you very much!!!
kahlinor
August 30th, 2006, 01:50 AM
Sounds like a simple compare operation.
What assembly language/OS are you allowed to use if not Windows assembly?
You'll also have to show your attempt to program this, it is your assignment after all.
-To do so, figure out how to open a memory mapped file in your OS, and get a pointer to the 1st character. If your OS doesn't have support for memory mapped files, allocate memory and load the file into memory.
-After that, look up cmpsb and rep.cmpsb instructions to help you allong.
shadowxgm
September 7th, 2006, 01:10 AM
Sounds like a simple compare operation.
What assembly language/OS are you allowed to use if not Windows assembly?
You'll also have to show your attempt to program this, it is your assignment after all.
-To do so, figure out how to open a memory mapped file in your OS, and get a pointer to the 1st character. If your OS doesn't have support for memory mapped files, allocate memory and load the file into memory.
-After that, look up cmpsb and rep.cmpsb instructions to help you allong.
data segment
tip db 'This program will search a string in C:\text.txt.If it exists,you can insert a new word in that position.$' ;
mes1 db 'please enter your string:$'
mes2 db 'not exist$'
mes3 db 'exist$'
mes4 db 'insert?(y/n)$'
mes5 db 'please enter a new word:$'
mes6 db 'ok!$'
posmes db 'H of the text.txt$' ;position message
errmes db 'error!$' ;error message
file db 'C:\text.txt',00
string db 10,?,10 dup(?)
text db 1024 dup(0)
word db 10,?,10 dup(?)
count db ?
handle dw ?
loc db ?
data ends
;***********************************************************
code segment
assume cs:code,ds:data,es:data
main proc far
mov ax,data
mov ds,ax
mov es,ax
;******output tip
mov ah,9
mov dx,offset tip
int 21h
;******
mov dl,0ah
mov ah,02h
int 21h
;******output mes1
mov ah,9
mov dx,offset mes1
int 21h
;******input string
mov dx,offset string
mov ah,10
int 21h
;******open file
mov dx,offset file
mov al,010
mov ah,3dh
int 21h
jc error
mov handle,ax
;******read file
mov ah,3fh
mov bx,handle
mov cx,1024
lea dx,text
int 21h
jc error
;******
mov di,offset text+2
mov bx,di ;file buffer's address->bx
mov al,text+1 ;file's length->al
sub al,string+1 ;file'length-string'length
jl notexist
inc al
mov count,al ;the repeat time
;******search
next:
mov si,offset string+2 ;string's add->si
mov cl,string+1 ;string'length->cl
mov ch,0
cmp count,0
jl notexist
rep cmpsb
je exist
inc bx
mov di,bx
dec count
je notexist
jmp next
;******notexist
notexist:
;******output mes2
mov dl,0ah
mov ah,02h
int 21h
mov ah,9
mov dx,offset mes2
int 21h
jmp over
;******exist
exist:
sub bx,offset text+1 ;the location
mov loc,bx ;save to loc
;******output mes3
mov dl,0ah
mov ah,02h
int 21h
mov ah,9
mov dx,offset mes3
inc 21h
;******output posmes
call binihex ;call binihex
mov ah,9
mov dx,offset posmes
int 21h
;******output mes4
mov dl,0ah
mov ah,02h
int 21h
mov ah,9
mov dx,offset mes4
int 21h
;******decide insert or not
mov ah,01
int 21h
cmp al,79
jz insert
cmp al,59
jz insert
jmp over
;******insert
insert:
mov dl,0ah
mov ah,02h
int 21h
;******output mes5
mov ah,9
mov dx,offset mes5
int 21h
;******input new word
mov dx,offset word
mov ah,10
int 21h
;******move point
mov ah,42h
mov al,0
mov bx,handle
mov cx,0
mov dx,loc
int 21h
jc error
;******write new word
mov ah,40h
mov bx,handle
mov cx,word+1
mov dx,offset word
int 21h
jc error
;******close file
mov ah,3eh
mov bx,handle
int 21h
jmp over
;******error
error:
mov ah,9
mov dx,offset errmes
int 21h
;******over
over:
mov ah,4ch
int 21h
main endp
;******binihex
binihex proc near
mov ch,4
rotate:
mov ch,4
rol bx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,3ah
jl printit
add al,7h
printit:
mov dl,al
mov ah,2
int 21h
dec ch
jnz rotate
ret
binihex endp
;******
code ends
end main
please help me~~
kahlinor
September 7th, 2006, 03:06 AM
This looks like an older version of MASM.
Let's examine your main search routine:
next:
mov si,offset string+2 ;string's add->si
mov cl,string+1 ;string'length->cl
mov ch,0
cmp count,0
jl notexist
rep cmpsb
je exist
inc bx
mov di,bx
dec count
je notexist
jmp next
mov si,offset string+2 ;string's add->si
si is address of source string to search, yet you are resetting it at the start of each loop. You should add 1 to si through each loop. Also, make sure si doesn't point to end of string.
This is also a good spot to reset di to the beginning of the string to search for.
--------------------------------------------------------------
mov cl,string+1 ;string'length->cl
I'm not too familiar with MASM syntax, are you sure cl has length of string? Seems to me you are loading cl with the second character value of whatever is in 'string'
Anyways, it should be the length of the destination string (word to search for).
This is a sample of a string search routine in 32-bit fasm syntax, using zero-terminated strings (I think you have to deal with '$' terminated strings).
mov esi, [searchin] ; load esi with address of string to search
sub esi, 1
searchword:
add esi, 1
cmp [esi], 0 ; end of string?
je notfound
mov ebx, esi ; preserve current starting position in ebx
mov edi, [searchfor] ; load edi with address of search word
mov ecx, [strlen] ; load ecx with length of search word
repe cmpsb ; compare while equal
je found
mov esi, ebx ; restore current starting position in esi
jmp searchword ; continue searching
found:
;ebx = address of match (not the index relative to start of string!)
notfound:
;...
shadowxgm
September 8th, 2006, 12:05 AM
thank you~
but my teacher just want me use the older version of MASM :-(
i am confused~
when i debug it, my screen is black.....and i have to restart my computer~~
thanks again!
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.