It is used to repeat a string instruction a specific number of times, controlled by the value in the ECX register, similar to using a loop, but without the extra LOOP instruction.
To divide the string length by four, the SHR instruction is used to shift the length value right 2 bits (which is the same as dividing by four), which leaves the quotient value loaded into the ECX register.
Besides monitoring the value of the ECX register, there are REP instructions that also monitor the status
of the zero flag (ZF). The following table describes the other REP instructions that can be used.
REPE Repeat while equal
REPNE Repeat while not equal
REPNZ Repeat while not zero
REPZ Repeat while zero
The REP instruction repeats the string instruction immediately following it until the value in the ECX register is zero. That is why it is called a prefix.
the REP instruction does not check the status of the flags between repetitions; remember that it is only concerned about the count value in the ECX register.
.section .data
string1:
.asciz “This is a test of the conversion program!\n”
length:
.int 43
divisor:
.int 4
.section .bss
.lcomm buffer, 43
.section .text
.globl _start
_start:
nop
leal string1, %esi
leal buffer, %edi
movl length, %ecx
shrl $2, %ecx
cld
rep movsl
movl length, %ecx
andl $3, %ecx
rep movsb
movl $1, %eax
movl $0, %ebx
int $0x80
To divide the string length by four, the SHR instruction is used to shift the length value right 2 bits (which is the same as dividing by four), which leaves the quotient value loaded into the ECX register.
Besides monitoring the value of the ECX register, there are REP instructions that also monitor the status
of the zero flag (ZF). The following table describes the other REP instructions that can be used.
REPE Repeat while equal
REPNE Repeat while not equal
REPNZ Repeat while not zero
REPZ Repeat while zero