Electrical – ARM assembly – Addition of specific bytes from word-size values

armassemblymicrocontroller

I'm trying to code a way to add specific halfwords from word-sized numbers in the ARM assembly language. For example, lets say I want to add the first 4 digits of r0=0x3B029BA1 and r1=0x0B54A361 or the middle 4 digits of r0=0x3B029BA1 and r1=0x0B54A361. I can't seem to make this work. This is a shortened example of what I have so far.

LDR r0, =0x3B029BA1;
LDR r1, =0x0B54A361;
LDRH r10, [r1]; 

I'm pretty sure there will need to be an offset for the address in the 3rd line of the code, but I haven't gotten past a halfword just getting into the register 10. Every time I run this code, the correct register values end up in r0 and r1, but nothing at all is appearing in r10. So before I do anything else, I just want to understand why this is happening.

Best Answer

LDR and LDRH are instructions for loading a register from memory. You are using R1 as a pointer to memory location 0x0B54A361. R10 then gets loaded with the contents of this memory location (which could be anything, but probably 0 if that particular memory location is not being used).

To move (copy) data from one register to another you should use the MOV instruction. You can combine the move with other operations by using instructions such as AND or ADD, because the destination register can be different from the source registers. You may also be able to apply a shift or rotate with the same instruction.