This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ldm/stm with not aligned 4byte

Hi experts!

I want to use ldr/str or ldm/stm to copy memory not aligned 4bytes.

I know their input address should be aligned by 4 bytes.

but is there any solution to use ldr/str or ldm/stm though src or dst isn't aliged 4byte by modifing following code?

void wordcopy(unsigned char *dst, unsigned char *src, int num) {
loop:
   LDR r3,[r1];
   STR r3,[r0];
   SUBS r2,r2,#1;
   BNE loop;
   BX lr;
}

  • LDM/STM only support aligned addresses.

    LDR/STR can be used with unaligned addresses, as long as the target addresses are marked as Normal and that alignment checking isn't enabled.

    You didn't mention which processor/architecture you are targeting. For A class, the translation tables that define whether an address is Normal or Device.  For R and M class, it's the MPU (if present).

    The other option is to test in the passed in pointers.  Do a number of byte load/stores until you get an aligned address, then do LDM/STM, and finish with more byte load/stores if needed.

    EDIT: Typo

  • Hello,

    I hope the __rt_memcpy function of the following link will help you.

    http://research.microsoft.com/en-us/um/redmond/projects/invisible/src/crt/md/arm/_memfunc.s.htm

    Best Regards,

    Yasuhiko Koumoto.

  • To enhance Martin's reply slightly:

    In the very beginning of your routine, make a check on the lowest 2 bits on both source and destination.

    If the bits match on both source and destination, use a 32-bit copy with a ramp-up.

    If bit 0 on source differs from bit 0 on destination, you may benefit from byte-copy; you also have the option to use shifts, but the code will be large.

    If bit 1 on source differs from bit 1 on destination, you may benefit from halfword-copy, but the penalty for copying misaligned halfwords is not so bad, so you may want to do a full 32-bit copy there.

    Hint: Use the EOR instruction followed by a LSLS#30 to check if the low two bits match.

  • Thanks Martin, jensbauer, yasuhikokoumoto for your kind response!

    It's very helpful!