gpt4 book ai didi

linux - linux内核中的memcpy实现

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:56 31 4
gpt4 key购买 nike

我在实模式下看到 linux 内核中的 memcpy 实现:

GLOBAL(memcpy)
pushw %si
pushw %di
movw %ax, %di
movw %dx, %si
pushw %cx
shrw $2, %cx
rep; movsl
popw %cx
andw $3, %cx
rep; movsb
popw %di
popw %si
retl
ENDPROC(memcpy)

我理解rep 之前的第一部分; movsl,但是为什么第二部分在rep之后; movsl,它已经从 si -> di 复制了 32 个字节。为什么是第二部分,我看到只有一个理由再次应对 si 中的地址未按 4 字节对齐的结构。

谢谢。

最佳答案

以下是相关的代码行,带有注释以解释它们的作用:

shrw    $2, %cx   ; length /= sizeof(DWORD)
rep; movsl ; Copy the first length/sizeof(DWORD) DWORDs
popw %cx ; Restore the original length
andw $3, %cx ; length &= 3, i.e. length %= sizeof(DWORD)
rep; movsb ; Copy the remaining length % sizeof(DWORD) bytes (if any)

关于linux - linux内核中的memcpy实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705053/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com