初级 ARM 汇编问题

发布于 2024-10-16 12:12:00 字数 842 浏览 0 评论 0原文

如何正确地将预定义的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灰色世界里的红玫瑰 2024-10-23 12:12:00

您可以使用 ldrb 将单个字节从字节对齐指针加载到寄存器中。我希望这就是您正在寻找的:

ldr  r0, =val
ldrb r1, [r0]

您可能希望在循环中具有相同的功能,否则一旦您前进到非字对齐地址处的第一个字符(可能是 o如何中):

loop:    ldrb r2, [r0]

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:

ldr  r0, =val
ldrb r1, [r0]

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 in How):

loop:    ldrb r2, [r0]
迷乱花海 2024-10-23 12:12:00

你正在使用字节;不存在对齐问题。您还忘记增加计数器并与错误的寄存器进行比较。这是一个可行的解决方案:

;   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  

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:

;   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  
风渺 2024-10-23 12:12:00

您是否填充了字节的地址?它需要偶数地址(字)填充。或者甚至可能根据您的情况进行双字填充

Are you padding the address of the byte? it need to be even address (word) padded. or maybe even dword padded dependint on your

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文