zoukankan      html  css  js  c++  java
  • rep stos dword ptr es:[edi] 是做什么的?

    笔者在winDBG中反汇编一个小程序的main函数, 看到了如下的一段代码:

    0:000> uf .
    monitor!main [c:\users\myalias\documents\visual studio 2005\projects\mytest\mytest\main.c @ 32]:
       32 0042f780 55              push    ebp
       32 0042f781 8bec          mov     ebp,esp
       32 0042f783 81eccc000000    sub     esp,0CCh
       32 0042f789 53               push    ebx
       32 0042f78a 56               push    esi
       32 0042f78b 57               push    edi
       32 0042f78c 8dbd34ffffff        lea     edi,[ebp-0CCh]
       32 0042f792 b933000000      mov     ecx,33h
       32 0042f797 b8cccccccc        mov     eax,0CCCCCCCCh
       32 0042f79c f3ab                     rep stos dword ptr es:[edi]

    有点不明白高亮语句的作用, 在stackoverflow的一篇帖子中找到了答案.

    ; This puts the address of the stack frame bottom (lowest address) into edi...
             lea     edi,[ebp-0C0h]
    ; ...and then fill the stack frame with the uninitialised data value (ecx = number of 
    ; dwords, eax = value to store)
            mov     ecx,30h
            mov     eax,0CCCCCCCCh
            rep stos dword ptr es:[edi]

    即, rep指令的目的是重复其上面的指令. ECX的值是重复的次数.

    所以, 我列出的代码的作用是将栈上从ebp-0xcc开始的位置向高地址方向的内存赋值0xCCCCCCCC,次数重复0x33(51)次. 注意0xCCCCCCCC代表着未被初始化.

    把STOS和REP的功能弄清楚, 上面的答案就不难理解了.

    =================

    STOS指令的作用是将eax中的值拷贝到ES:EDI指向的地址. 如果设置了direction flag, 那么edi会在该指令执行后减小, 如果没有设置direction flag, 那么edi的值会增加, 这是为了下一次的存储做准备.

    REP可以是任何字符传指令(CMPS, LODS, MOVS, SCAS, STOS)的前缀. REP能够引发其后的字符串指令被重复, 只要ecx的值不为0, 重复就会继续. 每一次字符串指令执行后, ecx的值都会减小.

    参考资料

    ===================

    Can anyone help me interpret this simple disassembly from WinDbg?

    http://stackoverflow.com/questions/4024492/can-anyone-help-me-interpret-this-simple-disassembly-from-windbg

    STOS

    http://www.cs.ubbcluj.ro/~dadi/ac/doc/ng1cf0a.html

    REP

    http://www.cs.ubbcluj.ro/~dadi/ac/doc/ng15a5f.html

  • 相关阅读:
    hdu-3001 三进制状态压缩+dp
    最长公共子序列(LCS)
    矩阵最优路线DP
    CF-721C DAG图拓扑排序+费用DP
    拓扑排序
    BFS+二进制状态压缩 hdu-1429
    DAG最长路问题 hdu-1224
    并查集-解决区间和纠错问题 hdu-3038
    hdu 4972 根据每轮篮球赛分差求结果
    hdu 1116 欧拉回路+并查集
  • 原文地址:https://www.cnblogs.com/awpatp/p/2623628.html
Copyright © 2011-2022 走看看