0

I read a book about Assembly, and it has the next code:

.DATA
    string1 db ’abcdfghi’,0
    strLen EQU $ - string1
    string2 db ’abcdefgh’,0
.CODE

.STARTUP
    mov AX,DS ; set up ES
    mov ES,AX ; to the data segment
    mov ECX,strLen
    mov ESI,string1
    mov EDI,string2
    cld ; forward direction
    repe cmpsb
leaves ESI pointing to g in string1 and EDI to f in string2. Therefore, adding
    dec ESI
    dec EDI
leaves ESI and EDI pointing to the last character that differs. Then we can use,  
ja str1Above

It is writen that we need:

 dec ESI
 dec EDI

becuase leaves ESI pointing to g in string1 and EDI to f in string2.

But why? When we arrive to 'f' in ESI, and 'e' in EDI, the repe condition is not fulfill, and for that we would exit the loop, where 'f' is in ESI, and 'e' is in EDI. Why It say that we continue to scan the strings one more time?

2 Answers 2

1

That's the way repe works:

  1. ecx = 0 or Zeroflag = 0 --> out
  2. dec ecx
  3. do command
  4. increase (or decrease) esi and edi.
  5. back to 1.

So esi and edi point to address + 1 (or - 1 in case of backwards direction).

2
  • repe also check if esi=edi, no? In our case, It exit from the loop because esi<>edi, and not because ecx=0.
    – Adam Sh
    Mar 10, 2012 at 11:26
  • Sure - missed that, thanks. The point was, that the exit condition is at the beginning. That's why you need to adjust the pointer.
    – pät
    Mar 10, 2012 at 19:38
0

If the direction flag DF is clear in the EFLAGS register, ESI and EDI are incremented after the comparison exits. So the dec instructions are compensating for this. I think. The rep string instructions are a bit of a hangover from the 8086 'CISC' days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.