jak*_*ist 1 string assembly reverse
这个代码用于反转字符串
.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
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这一切意味着什么吗?我看着寄存器移动了,当ECX等于0时,循环似乎结束了.为什么这样?注意解释每段代码?
编辑1:我看到ecx定义在"mov ecx,SIZEOF source-1",每次取走1.
正如你在这里可以看到的那样,loop指令递减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--;
}
Run Code Online (Sandbox Code Playgroud)