初级 ARM 汇编问题
如何正确地将预定义的 .byte 值加载到寄存器中?例如,将常量定义为:
constant: .byte 'a'
我正在尝试:
ldr r0, =constant
ldr r1, [r0]
但是,模拟器在第二行之后停止并给出错误“访问未对齐的内存位置,错误地址”只要第二行不存在,其余代码就可以正常运行包括。
完整代码:
; r0 is a pointer to msg1
; r1 used to store the value of val
; r2 used to compare a character in msg1
; r3 counter for the number of comparisons
.text
.global _start
_start:
ldr r0, =msg
ldr r1, =val
ldr r1, [r1]
mov r3, #0
loop: ldr r2, [r0]
cmp r2, #0
beq done
cmp r0, r1
add r0, r0, #4
bne loop
add r2, r2, #1
b loop
done:
swi 0x11
.data
.align
msg: .asciz "How many 'a's are in this string?"
val: .byte 'a'
.end
How do you properly load the value of a predefined .byte into a register? e.g. With a constant defined as:
constant: .byte 'a'
I am trying:
ldr r0, =constant
ldr r1, [r0]
However, the simulator halts after the second line and gives the error "Access to unaligned memory location, bad address" The rest of the code otherwise runs fine as long as the second line is not included.
Full Code:
; r0 is a pointer to msg1
; r1 used to store the value of val
; r2 used to compare a character in msg1
; r3 counter for the number of comparisons
.text
.global _start
_start:
ldr r0, =msg
ldr r1, =val
ldr r1, [r1]
mov r3, #0
loop: ldr r2, [r0]
cmp r2, #0
beq done
cmp r0, r1
add r0, r0, #4
bne loop
add r2, r2, #1
b loop
done:
swi 0x11
.data
.align
msg: .asciz "How many 'a's are in this string?"
val: .byte 'a'
.end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 ldrb 将单个字节从字节对齐指针加载到寄存器中。我希望这就是您正在寻找的:
您可能希望在循环中具有相同的功能,否则一旦您前进到非字对齐地址处的第一个字符(可能是
o
在如何
中):You can use
ldrb
to load a single byte into a register from a byte-aligned pointer. I expect that's what you're looking for:You probably want the same in your loop or else you'll crash in the same way once you advance to the first character at a non-word-aligned address (probably the
o
inHow
):你正在使用字节;不存在对齐问题。您还忘记增加计数器并与错误的寄存器进行比较。这是一个可行的解决方案:
You're working with bytes; there are NO alignment issues. You're also forgetting to increment your counter and comparing with the wrong register. Here's a working solution:
您是否填充了字节的地址?它需要偶数地址(字)填充。或者甚至可能根据您的情况进行双字填充
Are you padding the address of the byte? it need to be even address (word) padded. or maybe even dword padded dependint on your