Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assembly Problem (cmp statement)

580 views
Skip to first unread message

God

unread,
Nov 6, 1997, 3:00:00 AM11/6/97
to

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.

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


Evin C Robertson

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to

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.

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


Roman Gruber

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to

On Thu, 6 Nov 1997 23:39:37 -0000, "God" <G...@hell.com> wrote:

>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"

Nick Grenfell

unread,
Nov 14, 1997, 3:00:00 AM11/14/97
to

> 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.

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.


Flint

unread,
Nov 15, 1997, 3:00:00 AM11/15/97
to

Sorry, bug. This code won't work. The CMP AL,[DI] sets the zero flag. Then
you INC DI, which modifies the zero flag. THEN you jnz. Your intention is
to jmp if the compare set the zero flag, but you are actually jumping if
the INC set the zero flag. You need to watch out for these flags. Try
something like this:
loop:
mov al,[si] ;get character to match
inc si ;move both pointers to next char
inc di
cmp al,0 ;was this source terminator?
je okSoFar ;if so, check dest
cmp al,[di-1] ;else, chars the same?
je loop ;yes, keep comparing
jmp differentStrings ;no, different chars here
okSoFar:
cmp al,[di-1] ;[si] was 0, is [di] 0?
jne differentStringLengths ;if not, dest was longer string
sameStrings: ;else, same contents AND same length

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.
>

JJUN

unread,
Nov 28, 1997, 3:00:00 AM11/28/97
to

This is a multi-part message in MIME format.

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


0 new messages