flat assembler
Message board for the users of flat assembler.

Index > DOS > cmp strings

Author
Thread Post new topic Reply to topic
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
Hi,
I'm trying to compare two strings,in masm you would write somthing
like this:
Code:
   
   lds   si, [src]
   les   di, [dest]
   cld
   mov   cx, 100
   rep   cmpsb
   jne   @@mismatch
@@match:
     :
     :

@@mismatch:
   dec   si
   dec   di
     

but I tried to do the same with fasm but I couldn't and had alot of trouble can someone help me please...



Thanx.
Post 14 Jul 2008, 01:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
There appears to be nothing wrong with the code you posted there except that perhaps you want to use repe instead of rep. What is the specific problem you have? Can't compile? Compiles wrongly? Doesn't compare properly? Something else?
Post 14 Jul 2008, 03:51
View user's profile Send private message Visit poster's website Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
Now I changed it to this ad it worked out:

Code:
 
        mov     si,msg1
        mov     di,msg2
        mov     cx,6
        rep     cmpsb 
        jne     notequal
    

but I still couldn't use LES LDS... Sad
Post 17 Jul 2008, 23:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Could you post from the other code the part where you declare src and dest variables?
Post 18 Jul 2008, 00:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
Perhaps ES and DS are not properly restored after the cmpsb.
Post 18 Jul 2008, 00:30
View user's profile Send private message Visit poster's website Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
LocoDelAssembly wrote:
Could you post from the other code the part where you declare src and dest variables?

Sure,here's the whole code:
Code:
      org     100h
        jmp     start
       str1    db      'first string',0
  str2    db      'second string',0
 wrong   db      'mismatch$'
       okay    db      'match$'

start:

    lds   si, str1
      les   di, str2 
     cld 
        mov   cx, 12 
       rep   cmpsb 
        jne   @@mismatch 

@@match: 
      mov     dx,okay
     mov     ah,09
       int     21h
 int     20h


@@mismatch: 
     mov     dx,wrong
    mov     ah,09
       int     21h
 int     20h
    

fasm is giving me:invalid operand
Post 19 Jul 2008, 00:35
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Sure that MASM supports the instruction as you wrote it?

The problem is that LDS and family needs a far pointer stored in memory but you are passing an offset instead. Also note that normally you would use L*S only for run-time generated pointers, for global variables you would normally set the segment register by yourself.

Here a working program that shows an example of when L*S could be used.

Code:
org $100

  mov ah, $48
  mov bx, (msg_end - msg + 15)/16
  int $21
  jc error

  mov word [pointer_var], 0
  mov word [pointer_var+2], ax

  mov es, ax
  xor di, di
  mov si, msg
  mov cx, msg_end - msg

  rep movsb

  ; push [pointer_var] Not supported in earlier processors
  push word [pointer_var+2]
  push word [pointer_var]
  call some_proc_with_far_pointer_param

exit:
  xor  ax, ax
  int  $16

  int  $20


some_proc_with_far_pointer_param:
  push bp
  mov  bp, sp

  push ds
  lds  dx, [bp+4]
  mov  ah, $09
  int  $21
  pop  ds

  leave
  retn 4

error:
  mov ah, $09
  mov dx, error_msg
  int $21
  jmp exit


error_msg db "Error allocating memory!$"

msg db "Hello World! Very Happy$"
msg_end:

pointer_var dd ?    

Very stupid example of course but shows when such instructions are useful.

[edit]I forgot to tell, in the case of COM executables, at program start you are guarantied to have CS=DS=ES=SS[/edit]
Post 19 Jul 2008, 01:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
LDS and LES must point to a two word structure that contain the segment and offset of your strings. Also you forgot to restore DS after you do the cmpsb. but note that you don't need to use LDS or LES since the strings can be accessed from the same segment. Try this:
Code:
        org     100h
        jmp     start
        str1    db      'first string',0
        str2    db      'second string',0
        wrong   db      'mismatch$'
        okay    db      'match$'

start:
        mov   ax,cs
        mov   ds,ax
        mov   es,ax
        mov   si, str1
        mov   di, str2 
        cld 
        mov   cx, 12 
        rep   cmpsb 
        jne   @@mismatch 

@@match: 
        mov     dx,okay
        mov     ah,09
        int     21h
        int     20h


@@mismatch: 
        mov     dx,wrong
        mov     ah,09
        int     21h
        int     20h    
Post 19 Jul 2008, 02:12
View user's profile Send private message Visit poster's website Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
Thank you guys that was helpfull,but I have a couple of questions...
Quote:
I forgot to tell, in the case of COM executables, at program start you are guarantied to have CS=DS=ES=SS

why would I need to do that?
and does this apply to .exe and .bin files?

Thanx.
Post 19 Jul 2008, 02:39
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
You have to do nothing Razz I meant that DOS sets all the segment registers to the same value before giving control of the CPU to the COM program. This is not true for EXEs, but yet LDS and LES are not very helpful because you can address the global variables as revolution does.
Post 19 Jul 2008, 02:48
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Just for the record here an example that uses both ways to get the far pointer of a global variable
Code:
format MZ

entry main:start
stack $100

segment main
start:
  lds  dx, dword [cs:p_hello] ; Segment override because DS is undefined and CS points to the same segment where the string is stored
  mov  ah,9
  int  21h

  mov  ax, main   ; Redundant actually since the LDS above already set
  mov  ds, ax     ; DS to main segment
  mov  dx, hello2
  mov  ah,9
  int  21h

  xor  ax, ax
  int  $16

  mov  ax,4C00h
  int  21h

p_hello dw hello, main

hello db 'Hello world!', 13, 10, '$'
hello2 db 'Hello again!', 13, 10, '$'    
Post 19 Jul 2008, 03:09
View user's profile Send private message Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
Thank you LocoDelAssembly
Post 20 Jul 2008, 00:24
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
I was expecting some SSE4 implementation Very Happy
Post 20 Jul 2008, 08:21
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Post 05 Aug 2008, 02:50
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Matrix wrote:
http://board.flatassembler.net/topic.php?t=2633
Wink
cheers


Yeah, but why keep things simple, when you can make them complicated Smile

_________________
My updated idol Very Happy http://www.agner.org/optimize/
Post 05 Aug 2008, 06:40
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.