Assembly 初学者手臂装配问题
如何将预定义的.byte的值正确加载到寄存器中?e、 g.常数定义为:Assembly 初学者手臂装配问题,assembly,arm,Assembly,Arm,如何将预定义的.byte的值正确加载到寄存器中?e、 g.常数定义为: 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
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
你在填充字节的地址吗?它需要均匀地填充地址(单词)。或者甚至可以根据您的使用dword填充,您可以使用
ldrb
从字节对齐指针将单个字节加载到寄存器中。我想这就是你想要的:
ldr r0, =val
ldrb r1, [r0]
您可能希望在循环中使用相同的字符,否则,一旦前进到非单词对齐地址的第一个字符(可能是How
中的o
),您将以相同的方式崩溃:
您正在处理字节;没有对齐问题。你也忘了增加你的计数器和比较错误的寄存器。这里有一个可行的解决方案:
; 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 r1, =val
ldr r0, =msg
ldrb r1, [r1]
mov r3, #0
loop: ldrb r2, [r0],#1
cmp r2, #0
beq done
cmp r2, r1
addeq r3,r3,#1
b loop
done:
swi 0x11
.data
msg: .asciz "How many 'a's are in this string?"
val: .byte 'a'
.end
我不清楚你所说的“填充字节地址”是什么意思。你必须使用align-alignment-padding->allignment。我不确定你的回答或评论是否有意义。当然,数据部分中
msg
和val
的对齐不是OP的问题。@Michael Burr,我的意思是相对于任何更严格对齐的指针。
; 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 r1, =val
ldr r0, =msg
ldrb r1, [r1]
mov r3, #0
loop: ldrb r2, [r0],#1
cmp r2, #0
beq done
cmp r2, r1
addeq r3,r3,#1
b loop
done:
swi 0x11
.data
msg: .asciz "How many 'a's are in this string?"
val: .byte 'a'
.end