I am getting to know asembler and its commands well, so I thought Id start
by writing a program to read characters from stdin, then compare them with a
statement compiled into the .com file. How would I go about doing this? Just
to clarify things a bit, what i want to do, is use the cmp statement to
compare the characters retrieved from stdin, and the string stored in the
.com file. Can someone please tell me how to go about doing this as at the
moment I get an error stating: Illegal memory reference from Tasm, when I
try to compile it. FYI: the line of code is:
cmp Inputstring,StoredString
I have tried putting the offsets of both of these into registers ax,dx
respectively then using the cmp [ax],[dx] statement, but that produces the
same error.
I will happily mail you my source code on demand if this mail is too vague.
One last thing, could you please E-mail me any response you may have, I am
hopeless at checking newsgroups (email address at bottom) Sorry to be a
pain.
Any help much appreciated.
Thank you for your time.
Matthew Joyce
Ma...@acjoyce.demon.co.uk
The cmp instruction can only compare:
a register with another register
a register with a piece of memory the same size of it.
Assembly is a low-level language, so it hasn't any idea that you have a
complete string there.
The easiest way to do this is to compare the strings byte by byte.
Here's a quick example assuming you have null-terminated strings:
mov si, offset InputString ; remove the word "offset" if you use NASM
mov di, offset StoredString
loop:
mov al, [si] ; Get the first byte from InputString
inc si
cmp al, [di]
inc di
jne TheyArentTheSame
cmp al, 0
jne loop
TheyAreTheSame:
;; Hurray.
TheArentTheSame:
;; [Insert something Eeyore would say here]
>
> I have tried putting the offsets of both of these into registers ax,dx
> respectively then using the cmp [ax],[dx] statement, but that produces the
> same error.
This isn't doing what you want. If this was valid assembly, it would be
comparing pieces of the string, not the whole string. The reason it
gives you an error here is because 1) you can't do a cmp [memory],
[memory], and 2) it doesn't know the size of the memory reference you
are making, and 3) you can't normally use ax as an indexing register.
(using xlat is not normal in my book)
--------
The [??] indicates my beginning attempts at self-discipline; see
www.eiffel.com/discipline
>Anyone,
>
>I am getting to know asembler and its commands well, so I thought Id start
>by writing a program to read characters from stdin, then compare them with a
>statement compiled into the .com file. How would I go about doing this? Just
>to clarify things a bit, what i want to do, is use the cmp statement to
>compare the characters retrieved from stdin, and the string stored in the
>.com file. Can someone please tell me how to go about doing this as at the
>moment I get an error stating: Illegal memory reference from Tasm, when I
>try to compile it. FYI: the line of code is:
>
>cmp Inputstring,StoredString
>
>I have tried putting the offsets of both of these into registers ax,dx
>respectively then using the cmp [ax],[dx] statement, but that produces the
>same error.
True. The reason for this is the encoding of the instructions. you can
always (well almost) use two registers with an instruction, but only
one memory operand! Another problem is (I assume) that you want to
compare the whole string with one instruction. Assembler (or the cpu)
is not able to do so! You have to compare byte by byte (or character
by character if you prefere), using either the CMPSx instruction
(CMPSB, CMPSW, CMPSD) with a REP prefix, or load one byte after the
other to a register and then compare this one with the next character
of the string. If you got the specs, see CMPS and REP for information,
otherwise mail me your source, and I will try to help...
>I will happily mail you my source code on demand if this mail is too vague.
>One last thing, could you please E-mail me any response you may have, I am
done.
>hopeless at checking newsgroups (email address at bottom) Sorry to be a
>pain.
Roman Gruber
------------> gru...@kapsch.net
"Every finished, working program is outdated"
> I have tried putting the offsets of both of these into
> registers ax,dx
> respectively then using the cmp [ax],[dx] statement, but
> that produces the same error.
What you have to do is compare each byte to the corresponding byte you read in.
mov dx,malloc_seg ;get ready set ES...
mov es,dx ;set ES addressability
mov bx,0 ;point to sentence
begin_compare:
mov di,bx ;point to keyword (cmd line parm)
mov cx,0 ;clear for usage *
mov cl,parm_len ;length of keyword *
dec cl ;reduce for (carriage return) *
mov si,offset arguments
repe cmpsb ;compare characters
;repeat until CX = 0 or a ...
;...nonmatch is found
jz found_match
inc bx ;inc BX to check next character..
;..in sentence
dec dx ;DEC DX (sentence counter)
jz eof_found ;done?
jmp begin_compare
eof_found:
cmp found_flag,0 ;did we find any matches
jz no_match ;nope, send msg - terminate
jmp how_many_matches ;done with compare, check how ...
no_match: ;...many matches we have
I snagged the routine out of an old book .. Assembly language primer for the
IBM PC&XT. I believe it's a Waite book.
Nick
--
|Fidonet: Nick Grenfell 1:114/440
|Internet: Nick.G...@biohazard.ghostrdr.wierius.com
|
| Standard disclaimer: The views of this user are strictly his own.
Evin C Robertson <ec...@andrew.cmu.edu> wrote in article
<8oMfRKi00...@andrew.cmu.edu>...
> Excerpts from netnews.alt.lang.asm: 6-Nov-97 Assembly Problem (cmp
> state.. by Mathew Joyce:
> > I am getting to know asembler and its commands well, so I thought Id
start
> > by writing a program to read characters from stdin, then compare them
with a
> > statement compiled into the .com file. How would I go about doing this?
Just
> > to clarify things a bit, what i want to do, is use the cmp statement to
> > compare the characters retrieved from stdin, and the string stored in
the
> > .com file. Can someone please tell me how to go about doing this as at
the
> > moment I get an error stating: Illegal memory reference from Tasm, when
I
> > try to compile it. FYI: the line of code is:
> >
> > cmp Inputstring,StoredString
>
> The cmp instruction can only compare:
>
> a register with another register
> a register with a piece of memory the same size of it.
>
> Assembly is a low-level language, so it hasn't any idea that you have a
> complete string there.
>
> The easiest way to do this is to compare the strings byte by byte.
> Here's a quick example assuming you have null-terminated strings:
>
> mov si, offset InputString ; remove the word "offset" if you use
NASM
> mov di, offset StoredString
>
> loop:
>
> mov al, [si] ; Get the first byte from InputString
> inc si
> cmp al, [di]
> inc di
> jne TheyArentTheSame
> cmp al, 0
> jne loop
>
> TheyAreTheSame:
>
> ;; Hurray.
>
> TheArentTheSame:
>
> ;; [Insert something Eeyore would say here]
>
>
> >
> > I have tried putting the offsets of both of these into registers ax,dx
> > respectively then using the cmp [ax],[dx] statement, but that produces
the
> > same error.
>
--------------59B1787CD98
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Nick Grenfell wrote:
>
> > cmp Inputstring,StoredString
>
> > I have tried putting the offsets of both of these into
> > registers ax,dx
> > respectively then using the cmp [ax],[dx] statement, but
> > that produces the same error.
>
*************
Hi ! I dont know if I got you right?
Well I'm just a beginner so I dont know if this would help your problem
on comparing a stored string to an inputed string.
I made the program parameter driven so its gonna look something like
this.......
c>compare mystring
then later prints out a message whether the string is matched or not
matched to the string "Bio Hazard"
--
********************************************
jun
http://pages.prodigy.net/jjun/jun.htm
--------------59B1787CD98
Content-Type: text/plain; charset=us-ascii; name="compare.asm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="compare.asm"
;** Assemble as COM **
.model tiny
.code
org 100h
main:
mov si,80h ;get parameter
xor cx,cx
mov cl,byte ptr[si] ;store in cx
add si,2
dec cx
cmp cx,10 ;first check lenth
jne n_match
cld
lea di,jstr ;compare strings if equal to Bio Hazard
repe cmpsb
jnz n_match
mov ah,09h
lea dx,mstr
int 21h ;display matched string
ret
n_match:
mov ah,09h
lea dx,umstr
int 21h ;display not matched string
ret
jstr db 'Bio Hazard'
mstr db 'Input Matched$'
umstr db 'Input Not Matched$'
end main
--------------59B1787CD98--