Ensamblaje: bucle sobre cuerdas usando rep

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

porque 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,la 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?

preguntado el 10 de marzo de 12 a las 10:03

2 Respuestas

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. volver a 1.

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

respondido 10 mar '12, 19:03

repe also check if esi=edi, no? In our case, It exit from the loop because esi<>edi, and not because ecx=0. - Adán Sh

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

If the direction flag DF is clear in the EFLAGS register, ESI and EDI are incremented después de la comparación salidas. Entonces el dec instructions are compensating for this. I think. The rep string instructions are a bit of a hangover from the 8086 'CISC' days.

respondido 10 mar '12, 11:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.