cortex M4 Thumb Assembly function address

2019-09-09 11:34发布

I'm currently trying to understand ARM assembly for Cortex-M cores.

I know that functions that are in Thumb mode (which is the only mode the core supports) are called by their address and the LSB of the address is high to indicate that the destination is Thumb code.

In assembly I write following in front of the function "Reset_Handler" to tell the assembler that the data at the label is executable/a function:

  .type  Reset_Handler, %function
  Reset_Handler: 
  MOV R0, R0 # just do something

If I now load the address of the label

  LDR R0, =Reset_Handler

the LSB of R0 is set.

Do I have to put this .type directive in front of every label I want to use as a function with indirect calls?

Or is there a way to let the assembler automatically decide the correct address?

If I want to copy the code of the function to RAM and load the address of this function, I have to clear the LSB myself to get the "real" address where the data is located, am I right?

Is the .type directive necessary in this case? I know that it is necessary as soon as I want to put the address in the vector table as this requires the LSB to be set.

1条回答
成全新的幸福
2楼-- · 2019-09-09 11:52

Well there are two types of addresses (labels). One type is an address you want to call, the address of the entry point of a function and for thumb the bx or pop needs the lsbit set. The other is the address for a data item, the beginning of a string, the beginning of an array, whatever, and that does not want the lsbit corrupt as that would cause an alignment fault. So unless you want to have to add code to OR one onto an address before using bx you really want the assembler to have a solution either when you define the label or when you use the label.

So for gnu assembler you need .thumb_func before the label (and probably other ways to do it), for your assembler it appears you need what your assembler requires.

查看更多
登录 后发表回答