【问题标题】:Can someone explain this code有人可以解释这段代码吗
【发布时间】:2015-03-26 21:28:07
【问题描述】:

有此代码用于反转字符串

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
source  BYTE  "This is the source string",0
target  BYTE  SIZEOF source DUP('#')
 .code
 main PROC
; Point ESI to the last character in the source string:
    mov  esi,OFFSET target - 2

; Point EDI to the beginning of the target string:
; We do not copy the null terminator byte.

mov  edi,OFFSET target
mov  ecx,SIZEOF source-1        ; loop counter

L1: mov  al,[esi]                   ; get a character from source
    mov  [edi],al                   ; store it in the target
    dec  esi                        ; move to next character
    inc  edi
    loop L1                         ; repeat for entire string

    mov BYTE PTR [edi],0            ; add a null byte to the target

    invoke ExitProcess,0
main endp
end main

有人可以向我解释这一切意味着什么吗?我看到寄存器移动,似乎当 ECX 等于 0 时循环结束。这是为什么呢?愿意解释每一段代码吗?

编辑 1:我看到 ecx 是在“mov ecx, SIZEOF source-1”中定义的,每次都带走 1。

【问题讨论】:

    标签: string assembly reverse


    【解决方案1】:

    如您所见,hereloop 指令递减 ECX,如果不为 0 则跳转,如果为 0 则继续。

    edi 用作指向字符串末尾的指针。 ecx 设置为字符串的长度

    这条线是偷偷摸摸的:mov esi,OFFSET target - 2

    循环相当于:

    a = 0;
    b = source.length - 1;
    for (int i = source.length; i >= 0; i++) {
       target[a] = source[b];
       a++;
       b--;
    }
    

    【讨论】:

      【解决方案2】:

      LOOP 使用 ECX 作为循环计数器,它会减少它,然后如果它不为零则跳转到标签。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-04
        • 2015-09-14
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        相关资源
        最近更新 更多