【问题标题】:Copy part of the memory to another location将部分内存复制到另一个位置
【发布时间】:2021-02-11 04:51:53
【问题描述】:

CopyMemory 函数必须将部分内存复制到另一个位置。

CopyMemory
; The CopyMemory function receives the following parameters:
; R0: Address of the first 32-bit word to copy
; R1: Address of the last 32-bit word to copy
; R2: Destination address

ADD R4, R1, #4

Loop
CMP R4, R1
BEQ EndLoop

LDR R3, [R0]
STR R3, [R2]
ADD R0, R0, #4
ADD R2, R2, #4

B Loop
EndLoop

EndCopyMemory
BX LR

我的代码有一个错误,但我看不到它是什么。我是不是做错了什么?

【问题讨论】:

  • 请评论每一行你认为它做了什么,这样我才能理解你的流程。我想您的 CMP 指令的操作数不正确。

标签: assembly memory arm cpu-registers


【解决方案1】:

我建议您学习如何在代码不工作时对其进行调试,这仍然是学习恕我直言的最佳方式。

例如,您可以使用在线汇编器/调试器/仿真器,例如 cpulator

复制/粘贴您要测试的代码, 组装它, 在逐步执行程序时,一边查看寄存器值、内存内容一边进入它。

            .global _start
_start:
            ldr r0,= first
            ldr r1,= last
            ldr r2,= dst
            
            // The CopyMemory function receives the following parameters:
            // R0: Address of the first 32-bit word to copy
            // R1: Address of the last 32-bit word to copy
            // R2: Destination address
            add r4, r1, #4

loop:
            cmp r4, r1
            beq endloop

            ldr r3, [r0]
            str r3, [r2]
            add r0, r0, #4
            add r2, r2, #4

            b loop      
endloop:
            b .
            .data                   
first:      .word 0x11111111
            .word 0x22222222
            .word 0x33333333
last:       .word 0x44444444
dst:        .word 0x00000000
            .word 0x00000000
            .word 0x00000000            
            .word 0x00000000
            .end

如果您是 Windows 用户并且对 Linux 一无所知,则另一种选择是使用 Windows 版本的qemu-system-armGDB,但在现阶段这对您来说可能更难做到。

可以分别从herehere下载。

如果您熟悉/使用 Linux 系统,那么按照 Peter Cordes 在评论中的建议使用 qemu-user 也是一个很好的解决方案。这可以在本机 Linux 系统上完成,或者通过在 Windows 上使用 VirtualBox 虚拟机,或通过使用 WSLWSL2 Windows 子系统之一来完成。

【讨论】:

  • qemu-user 在某种程度上可用作 GDB-remote,用于单步执行简单的 ARM 指令块。 How to single step ARM assembly in GDB on QEMU? / How to run a single line of assembly, then see [R1] and condition flags
  • @Peter Cordes 我同意。但是,当 Stefan Weil 的 qemu-system-arm 的port 在这种情况下与qemu-virttarget 机器和 Linaro 中提供的 GDB 一起工作时,它在 Windows 系统上并不容易(我猜)或为 Windows 构建的 ARM 工具链。我倾向于假设完全不熟悉 Linux 的人可能会问这种问题,- 我不考虑在 WSL 或 WSL2 子系统下运行 qemu-user,或使用 VirtualBox ,因为您仍然需要以某种方式了解 Linux。
  • 好的,但为什么会有人使用 Windows? :P (开个玩笑,但由于 OP 没有提到 Windows,所以我根本没有想到这一点。有趣且公平。)我猜 qemu-user 甚至可能在 cygwin 上工作,但也许不是;我不知道映射有多低级,也不知道它是否可以处理与来宾不同的结构的主机版本。
  • 啊啊,我知道...我不确定 Cygwin 下的 qemu-user(我曾经非常喜欢它),因为如果我没记错的话,它是一个 POSIX 仿真层。我对qemu-user 的理解是,被模拟程序调用的系统调用只是被主机系统上严格等效的系统调用捕获/替换,这更容易/更直接。对于在 AMD64 Windows PC 上安装 Cygwin 的成本(时间),从今天开始,我肯定更喜欢使用 WSLWSL2
  • ...但是味道和颜色,他们说,在旁观者的眼中...
猜你喜欢
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多