Can somebody tell me how and when and why we use the STD instruction ?
Thanks in advance (It'll be kewl to post examples too)
Posted on 2006-02-09 03:53:47 by XCHG
after std command such commands as lods, movs, scas will work with decrement of esi/edi - this is usefull if you wish scan some block from the end to the start (ie, in backward).

you have to restore direction flag with cld before exiting your procedure or before call of system function or they will fail.
Posted on 2006-02-09 04:04:07 by Shoo
Hi shoo and thanks for the reply
Well, what i am trying to figure out is that why would somebody need to copy or compare strings in a backward manner ?

I have heard that you use STD for unions and where the destination memory block and the source memory block overlap.
Posted on 2006-02-09 08:03:40 by XCHG
One example where you would want to use STD is if you want to insert some data within some other block of data. In assembly, there are numerous ways you could do it but most would require copying the backend of the block's data to another memory area and copy it back later to its original block at the end of the inserted data (i.e. moving it twice).

Using the std, you can copy it backwards to where it should be in the block and simply insert the new data in the liberated space; the backend gets moved only once.

Raymond
Posted on 2006-02-09 22:25:56 by Raymond

Can somebody tell me how and when and why we use the STD instruction ?
Thanks in advance (It'll be kewl to post examples too)


I found this in some old 16 bit code.

; Store the path

scan:
      lea      si,
      xor      cx,cx
      mov      di,si
      mov      cl,
      mov      al,'\'              ; c:\work.1\
      add      di,cx
      std                          ; scan from right to left
      dec      di
      repne    scasb              ; zero flag set if "\"
      popf
      jnz      short no_path
      add      cx,1                ; value is total # chars in path
Posted on 2006-02-10 07:22:33 by skywalker
Well, what i am trying to figure out is that why would somebody need to copy or compare strings in a backward manner ?



.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
    szTitle db "Find Extention", 0
    szString db "File extention: [%s]", 0
    szMyFile db "c:\some\file\name.exe", 0

.data?
    szBuffer db 256 dup (?)

.code
start proc
    lea edi, szMyFile
    mov al, '.'
    repne scasb

    invoke wsprintf, addr szBuffer, addr szString, edi
    invoke MessageBox, NULL, addr szBuffer, addr szTitle, MB_OK
    invoke ExitProcess, NULL
start endp
end start


This example searches for the extention of a file.. if you wanted to say, just grab the name of the file, and not the extention, you could do this, dec edi after the scan, set the byte to 0 then scan back for the first \ character (using the same type of scan) and you have a pointer to the value of the file name (without extention).. for say if you want to read in a file named "file.asm" and have the default output "file.obj" or whatever.

Regards,
Bryant Keller
Posted on 2006-02-10 18:55:14 by Synfire
Oh now i get it.
Thank you guys for the replies and examples. appreciate it.
Posted on 2006-02-10 21:15:17 by XCHG