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
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
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
That's the way repe works:
- ecx = 0 or Zeroflag = 0 --> out
- dec ecx
- do command
- increase (or decrease) esi and edi.
- volver a 1.
So esi and edi point to address + 1 (or - 1 in case of backwards direction).
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.