程序常常需要将大块数据从一个位置复制到另一位置,这些数据可以是数组或字符串或其它任何类型对象,在汇编中用循环来复制,变址寻址很适合这种操作。注意,目标存放位置必须有足够空间来存放。
; 复制字符串
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:dword
.data
source byte "This is the source string",0
target byte SIZEOF source DUP(0),0
.code
main proc
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
L1:
mov al,source[esi] ; get a character from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1 ; repeat for entire string
invoke ExitProcess,0
main endp
end main