Ulaş Sezgin
Ulaş Sezgin

Reputation: 318

LDRB and LDR problem in the ARM M0+ assembly

ArraySize EQU 0X0A
        AREA array, DATA, READWRITE
        ALIGN
y_array SPACE ArraySize

        AREA sort, CODE, READONLY
        ENTRY
        THUMB
        ALIGN
        EXPORT __main
 __main PROC
        B __sort
  __ret
    
        ALIGN
        ENDP

 __sort PROC
        MOVS R0, #0
        LDR R1, =x_array
        LDR R3, =y_array
        CMP R5, #ArraySize
        BGE __ret
  __mem LDRB R2, [R1,R0]
        STRB R2, [R3,R0]
        ADDS R0, R0, #1
        B __mem
        ENDP

  x_array DCB 12,65,43,37,89,94,37,56,33,14
        END

I am newbie to assembly. The assembly code for ARM M0+ above is to copy array. I have some questions about that;

  1. When I use LDR R2, [R1,R0] instead of LDRB R2, [R1,R0] there occurs error i couldn't define. I think the reason about x_array definition is DCB but i am not sure about it.

  2. When I use ALIGN statement in the code and what is the clear purpose of the statement?

  3. Is there any RET instruction for ARM M0+ assembly?

And please warn me if there is any error you recognize in the code. Thanks!!

Upvotes: 0

Views: 1314

Answers (1)

Tom V
Tom V

Reputation: 5510

  1. The difference between LDRB and LDR is the size of the data. If you are loading a byte (8 bits) use LDRB. For a word (32 bits) use LDR.

  2. The purpose of ALIGN is to skip some bytes so that the address of what follows is a certain multiple, usually a multiple of 4 if not otherwise specified. Most data needs to be aligned to a multiple of the size of its members, eg: an array of 32-bit words need to be aligned to 4 bytes.

  3. For ARMv6M (Cortex-M0, M0+ and M1) the instruction to return from a function is bx lr meaning branch with exchange to address in link register. This is used in functions without anything on the stack or when the stack has already been popped. Normally it is more efficient to pop any saved registers from the stack at the same time as returning. This looks like pop {r4-r6,pc}. The exact register list will depend on what you pushed, but the key is you will have pushed some registers including lr link register, which gets popped into pc program counter.

Upvotes: 2

Related Questions