repeat a string operation rcx times OR repeat a string operation while a condition is true a mximum of rcx times
The various repeat instructions all involve using the rcx to control the count of the number of times to execute the following string instruction. The simplest variety, rep, repeats the string instruction exactly rcs times. The next variety, repe (or repz), is limited to rcx times but also continues with the loop only if certain values are equal. The values tested depend on the specific string instruction used. The last variety, repne (or repnz), is limited to rcx times but also continues with the loop only if certain values are not equal.
lea rdi, [dest] ; get the address of the destination array
lea rsi, [source] ; get the address of the source array
cld ; clear the direction flag to increment
mov ecx, 1000 ; count = 1000
rep movsq ; move 1000 quadwords from source to dest
; rdi and rsi increment by 8 each time
lea rdi, [source] ; get the address of the array
cld ; clear the direction flag to increment
mov ecx, -1 ; count is pretty big
xor al, al ; will scan for 0 byte
repne scasb ; scan forward at most a lot
; rdi increments by 1 each time
; repeats while the byte scanned is != al
mov eax, -2 ; we start at -1 end up 1 past the end
sub eax, ecx ; yields the length of a C string
; Consider a string of length 0.
; The first iteration of the repeat finds the 0 byte.
; This means ecx == -2 and subtracting -2 - -2 = 0
; Consider a string of length 1.
; The second iteration of the repeat finds the 0 byte.
; This means ecx == -3 and -2 - -3 = 1.