Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason to use MOVS/CMPS/STOS/SCAS without a repeat prefix?

Tags:

x86

assembly

The x86 architecture allows string instructions to be used with or without a repeat prefix. But they don't seem to do anything interesting without the repeat prefix. MOVS without REP, for example, can be replaced with a simple MOV.

Is there a good reason to use MOVS (and STOS, SCAS, CMPS) without REP? Or is this just a useless idiosyncrasy of the x86 instruction set?

like image 708
Alex D Avatar asked Aug 31 '25 22:08

Alex D


1 Answers

Yes. Not necessarily on purpose, yet you can use them for some sort of optimization.

It is, for example, faster (provided that rsi, rsi point to the right location) to use movs than

mov rax,[whatever1]
mov [whatever2],rax

For the rest, I'm not sure at the moment, but the execution times could be looked up I guess. Incrementing/decrementing rsi, rdi would be a side effect here actually.

Also, printing a C style string in low level mode (no formatting or special chars; direct video memory access) is like:

    ; ...
_load_char:
    lodsb
    or al,al
    jz _end_of_string
    stosw
    jmp _load_char
_end_of_string:
    ; ...

Here you need to examine each single character you load and determine if the end of the string was reached or not so you cannot use rep. Though one might think of repz movsb that won't work here, since one of two consecutive bytes in the video mem is the attribute byte for the particular character. In this case it is an intended feature.

like image 181
Powerslave Avatar answered Sep 03 '25 20:09

Powerslave