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.
Up vote 2 down vote favorite share g+ share fb share tw.
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 be loop done: swi 0x11 . Data .
Align msg: . Asciz "How many 'a's are in this string? " val: .
Byte 'a' . End assembly arm link|improve this question edited Feb 8 '11 at 4:47 asked Feb 8 '11 at 4:23user477830717 60% accept rate.
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.
– Michael Burr Mar 23 '11 at 22:34 @Michael Burr, I meant as opposed to a pointer with any more restrictive alignment. – Carl Norum Mar 23 '11 at 22:56.
Before constant line, add: . Align 4 ARM is a RISC architecture. It doesn't even try to perform unaligned memory accesses for you under the hood at a hidden performance cost (unlike x86, for instance).
Instead, the processor generates an exception in case of unaligned memory accesses.
I have . Align before the line. This is part of an assignment for a class and I can't change this line of code to .
Align 4. – user477830 Feb 8 '11 at 4:35 @user447830 If you're using gas assembler, the default alignment value for . Align is 4 so it makes no difference.
Are you sure you're having . Align right before constant:. Could you post more code?
– Mehrdad Afshari Feb 8 '11 at 4:40 So I tried . Align 4 regardless; however, I get the same error. – user477830 Feb 8 '11 at 4:41 One of the variables is a string, the other is a single character.
Neither of those types have any alignment requirement. The OP simply needs to use the correct instruction to access them. – Carl Norum Feb 8 '11 at 4:57 @Carl Sure.
FYI the OP posted the information you're talking about after my comment. It didn't exist when I originally provided the answer. The original question was just a couple instructions and I think this was quite a sensible answer.
– Mehrdad Afshari Feb 8 '11 at 11:22.
It need to be even address (word) padded. Or maybe even dword padded dependint on your.
I am not clear what you mean by 'padding the address of the byte. ' – user477830 Feb 8 '11 at 4:37 you must be to the align - alignment - padding -> allignment – Warren Stevens Feb 8 '11 at 4:52 I'm not sure your answer or comment make any sense. Certainly the alignment of msg and val in the data section is not the OP's problem.
– Carl Norum Feb 8 '11 at 5:03 no, but there is an element of string component inside – Warren Stevens Feb 11 '11 at 5:39.
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 be loop done: swi 0x11 . Data msg: .
Asciz "How many 'a's are in this string? " val: . Byte 'a' .end.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.