String 将字符串从BSS变量复制到程序集中的BSS变量
假设我必须使用存储在.BSS节中创建的变量中的字符串String 将字符串从BSS变量复制到程序集中的BSS变量,string,assembly,copy,nasm,String,Assembly,Copy,Nasm,假设我必须使用存储在.BSS节中创建的变量中的字符串 var1 resw 5 ; this is "abcde" (UNICODE) var2 resw 5 ; here I will copy the first one 我该如何处理NASM? 我试过这样的方法: mov ebx, var2 ; Here we will copy the string mov dx, 5 ; Length of the string mov esi, dword var1 ; The
var1 resw 5 ; this is "abcde" (UNICODE)
var2 resw 5 ; here I will copy the first one
我该如何处理NASM?
我试过这样的方法:
mov ebx, var2 ; Here we will copy the string
mov dx, 5 ; Length of the string
mov esi, dword var1 ; The variable to be copied
.Copy:
lodsw
mov [ebx], word ax ; Copy the character into the address from EBX
inc ebx ; Increment the EBX register for the next character to copy
dec dx ; Decrement DX
cmp dx, 0 ; If DX is 0 we reached the end
jg .Copy ; Otherwise copy the next one
所以,第一个问题是字符串不是作为UNICODE复制的,而是作为ASCII复制的,我不知道为什么。第二,我知道有些寄存器可能不推荐使用。最后,我想知道是否有更快的方法来实现这一点(也许有专门为这种字符串操作创建的指令)。我说的是8086处理器。
incebx;为下一个要复制的字符递增EBX寄存器
一个单词有2个字节,但您只需向前移动1个字节。将
inc ebx
替换为add ebx,2
Michael已经回答了演示代码的明显问题
但还有另一层理解。如何将字符串从一个缓冲区复制到另一个缓冲区并不重要—按字节、字或双字复制。它将始终创建字符串的精确副本
因此,如何复制字符串是一个优化问题。使用rep movsd是已知最快的方法
以下是一个例子:
; ecx contains the length of the string in bytes
; esi - the address of the source, aligned on dword
; edi - the address of the destination aligned on dword
push ecx
shr ecx, 2
rep movsd
pop ecx
and ecx, 3
rep movsb
inc
/dec
/cmp
?为什么不rep stos
?因为我不知道。谢谢