简体   繁体   中英

How I can use the same asm code for different cortex-m

I am newbie in asm. I want to use some asm function as external lib for cortex m0 and m4. But code of asm function contains next declaration:

.cpu cortex-m0

My questions at this stage - .cpu is it mandatory instruction?

For gnu assembler that line tells the assembler what instruction set to encode for, basically armv-6m (no fpu, etc) which is so far compatible across all cortex-ms so if you want portable source code with respect to that assembly language that is one good way to do it.

But you would also need to compile your high level code to armv6-m instructions as well.

.cpu is not an instruction it is a tool/assembly language specific directive.

-mcpu=cortex-m0 on the gnu assembler as well as gnu c compiler command line also indicates the cpu and thus instruction set. the.cpu directive in the asm file overrides the -mcu option on the gas command line.

I want to use one asm function (in separate file) to link it to cortex-m0 and cortex-m4 C project. And I would like to remove.cpu at all. May I do it?

You have to tell the assembler what instruction set to use, first off arm vs thumb and then within each there are variations so you have to be quite clear, you can either indicate armv4t or cortex-m0 to get something compatible across the cortex-ms, you must put.thumb in the code to specify thumb vs arm. As well as indicate a label is a function if you want to call the code from C (want to use it externally or internally in the vector table).

The safest thing to do is put the.cpu line in there

.cpu cortex-m0
.thumb
.globl myfun
.thumb_func
myfun:
   bx lr

being a complete minimal example of a function to call from C written in gnu assembler for arm for all cortex-ms thus far.

arm-whatever-whatever-as myfun.s -o myfun.o

and link that with the project. Can remove the.cpu line but then have to

arm-whatever-whatever-as myfun.s -mcpu=cortex-m0 -o myfun.o

but can't remove any of the other lines. The.thumb_func has an alternate directive that you can use, but you have to tell the assembler to mark the label as a function if you want to make a call from an external object.

.cpu cortex-m0
.thumb
.globl myfun
.type myfun, %function
myfun:
   bx lr

.thumb_func is specific to gas of course and thumb mode code and indicates the next label is a function where.type can be used in arm or thumb and like.globl doesn't have to be immediately before the label.

According to the Arm white paper titled "ARM®Cortex®-M for Beginners", you should use the .cpu cortex-m0 directive: since the Cortex-M0 instruction set is a subset of the Cortex-M4 one, this would be IMHO the only way to make sure your code will execute on both MCUs:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM