Query regarding ARM assembly language

Hi I am working on ARM (ARM926EJ).

I have one integer pointer. I want to load some value in r5. I have used the following code.

int RegValue = 10; int *ptr; ptr = &RegValue; __asm { LDR r5 , [ptr] } Suppose ptr points to address 0x5FFED94 and *ptr = 10. Now after executing LDR statement I am getting value 0x5FFED94 in r5. But I need value at 0x5FFED94 address to be loaded in r5.i.e r5 must contain 10. Can you tell me if there is some problem in the code. Or if you can suggest me some alternative.

Thanks in advance.

Reply to
sachinahuja82
Loading thread data ...

So far ok. But which compiler/assembler ?

ptr is no register. This might be the fault.

BTW: Are you sure your compiler can handle this, if you change registers in inline-assembly ?

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
 Click to see the full signature
Reply to
42Bastian Schick

Why?

That means ptr is in r5.

Why? How can it matter which register is used?

The ARM compiler treats operands as C expressions that can be allocated to any register - there is no difference between LDR x, [ptr] and LDR r5,[ptr]. ARM inline assembler is very high level, the easiest way to think about it is that it is C with the syntax of assembler.

Inline assembler is only useful for cases where you need instructions that cannot be generated by a compiler (and even in that case intrinsics are a better option). Think about the actual problem you want to solve - why do you use inline assembler rather than C?

Wilco

Reply to
Wilco Dijkstra

Some version of the ARM compiler, most likely recent.

It's correct syntax.

Not a problem for a register allocator.

Wilco

Reply to
Wilco Dijkstra

You can't just do LDR R5,#10 ?

Reply to
Everett M. Greene

Well he could do MOV r5,#10 or even simpler: int r5 = 10; But I suspect the problem is that he doesn't actually use r5, so the compiler will optimise the instruction away. Even if it doesn't, it may not use r5 anyway...

Wilco

Reply to
Wilco Dijkstra

So how should one write above code correct to get *ptr not ptr into r5 ? (I admit, I am to lazy to look up the inline assembly syntax of my RVDS :D)

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
 Click to see the full signature
Reply to
42Bastian Schick

so just use volatile asm... i think he needs to learn assembly

Reply to
MisterE

The above code is sufficient to do the equivalent of r5 = *ptr, but the compiler won't use R5 nor LDR. It's not the syntax that is the problem, it is the user expectation that is wrong. For example:

int f(void) { int RegValue = 10; int *ptr; int r5; // declare variables used in inline assembler ptr = &RegValue; __asm { LDR r5, [ptr] // equivalent to r5 = *ptr } return r5; // variable r5 is allocated to register R0 }

generates:

PUSH {r3,lr} MOV r0,#0xa STR r0,[sp,#0] POP {r3,pc}

No LDR... As I've said before most people have trouble understanding how inline assembler works... Inline assembler is meant to seamlessly integrate with C - if you want WYSIWYG, then use the real assembler.

Wilco

Reply to
Wilco Dijkstra

RVDS inline assembler at least.

Thanks for clarifying this.

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
 Click to see the full signature
Reply to
42Bastian Schick

If you really want to use the ARM register (R1, R2, R3...) you must use the Embedded Assembler in RealView Compiler. Example:

__asm void my_strcpy(const char *src, const char *dst) { loop LDRB r3, [r0], #1 STRB r3, [r1], #1 CMP r3, #0 BNE loop MOV pc, lr }

void main() { const char *a = "Hello world!"; char b[20]; my_strcpy (a, b); printf("Original string: '%s'\n", a); printf("Copied string: '%s'\n", b); }

More information on Chapter 4 - Inline and Embedded Assembler of RealView Compiler and Libraries

formatting link

Bye

Reply to
Cesar Almeida

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.