4

I want to compare the user input stored in the register si with another string.
By the way, I'm using FASM. Here's my code so far after the user input.
If I'm using the repe cmpsb command, I know that I have to use the extra segment, but I don't know how. And the repe cmpsb command doesn't work with this code.

.input_done:
   cmp si, 0
   je no_input
   jmp short .compare_input


.compare_input:
    mov cx, 20 ;For the repe cmpsb command.
    cld
    mov di, info ;The string I want to compare.
    mov es, di
    mov di, info
    repe cmpsb
    cmp cx, 0
    je showinfo
.showinfo:
    ... ;The output string if both string are the same.

info db "info", 0
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • 2
    You need to specify not only assembler, but also target platform. For example in linux this is plain wrong code, as it's using neither 32b or 64b pointers. Then if you would say this is DOS 16b, we would need to see how you defined the data and for your own sake of adding good question, you should try to produce [MCVE] yourself, for example your current code does set `di` twice, but it's not clear what is in `si` (either set it, or add clear comment). – Ped7g Nov 18 '17 at 14:09
  • And here's some article explaining `segment:offset` addressing in 16b real mode (for you, to get better idea, what that `es` and `ds` are for): http://thestarman.pcministry.com/asm/debug/Segments.html – Ped7g Nov 18 '17 at 14:15

1 Answers1

2
mov di, info ;The string I want to compare.
mov es, di

For a simple program it's probably true that both strings will be stored in the same memory segment. Just place the value from DS in ES.

...
push ds
pop  es
mov  di, info
...

And the repe cmpsb command doesn't work with this code.

You've set the counter CX to a fixed number of 20, where at least one of the strings ("info") has only 4 characters. No wonder that the comparison fails.


Since you want to compare for equality, your first step will be to see if both strings have the same lengths. If not, you already know the answer.
If they are the same length, then use that as the counter CX.

; String1 pointer in DS:SI, length in CX
; String2 pointer in ES:DI, length in DX

cmp  cx, dx
jne  NotEqual
repe cmpsb
jne  NotEqual
Equal:       ; .showinfo:
...          ; The output string if both string are the same.
; Don't fall through here!!!
NotEqual:
...
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • 2
    the end state of `repe` is handled incorrectly as well. (destroying it by `cmp cx,0` instead of `jcxz` or rather testing for `jne` state). – Ped7g Nov 18 '17 at 14:12