【问题标题】:Copy string from BSS variable to BSS variable in Assembly将字符串从 BSS 变量复制到程序集中的 BSS 变量
【发布时间】:2013-09-03 08:04:03
【问题描述】:

假设我必须将字符串存储在 .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 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 处理器。

【问题讨论】:

  • inc/dec/cmp?为什么不rep stos
  • 因为我不知道。谢谢

标签: string assembly copy nasm


【解决方案1】:

inc ebx ; Increment the EBX register for the next character to copy

一个字是 2 个字节,但您只是向前迈进了 ebx 1 个字节。将inc ebx 替换为add ebx,2

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2014-02-03
      相关资源
      最近更新 更多