2
votes

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
3

3 Answers

6
votes

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]
0
votes

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  
-1
votes

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