News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Are the string instructions very slow?

Started by Gunnar Vestergaard, December 12, 2009, 11:51:59 PM

Previous topic - Next topic

Gunnar Vestergaard

First of all I am aware of and understand that the LOOP instruction is very slow. A much faster alternative is DEC and Jcc.

But how about the LODSx and STOSx and MOVSx instructions? I mean in modern Intel cpu's? Are they fast or should they be avoided?

A good reason for using LODSx and STOSx and MOVSx instructions is that they provide small code size, if that is a concern in a certain part of the application.

hutch--

Gunnar,

It varies with hardware but generally avoid old string instruction if they are not used wioth the leading REP(E) instruction as they can be very slow. The other factor is they use more registers, you can often do more faster with less registers by loading the address into a register then incrementing it. INC DEC work OK but on a PIV they were slower than ADD SUB and even on a core series quad ADD and SUB are still slightly faster. The size difference is trivial and by using fewer registers you often save the stack preservation code to support it.

Ther are special cases for REP MOVS(b,w,d) and also with the STOS instruction when used with the REP(E) prefix. Thyey are still fast over about 500 bytes length because the hardware makes an exception for those cases.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

#2
sometimes, speed isn't critical and using the string functions is no problem
i think it is a matter of what is convenient, sometimes, in terms of register use, size, and speed
one of the things that surprised me was that on many processors, changing the direction flag sucks up a lot of clock cycles   :eek
if you do set the direction flag (STD), be sure and clear it when you are done (CLD)