How to call C functions with arguments in assemble program for S12ZVL96 MCU

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to call C functions with arguments in assemble program for S12ZVL96 MCU

跳至解决方案
1,830 次查看
ZhaiHui
Contributor II

Hello,

We want to call C functions with one argument in our assembly program, and we used register d0 to pass the value to the argument, but after we added other software parts, we found it became not working, and by debugging we found d2 is used, and after we changed the register to d2, it worked again. Could anyone tell me what is the correct way?

ld d0,#0

jsr CFunciton

0 项奖励
1 解答
1,766 次查看
ZhaiHui
Contributor II

Hello Edward,

Thank you very much for the answer. We have function prototype like following and not changed.

void MCUInterface_Init(const McuInitModeType i_mode)

And we have declaration as following in assembly file.

XREF.L MCUInterface_Init

By the way, I searched the folder of Codewarrior installation, and get MCU_S12Z_Assembler.pdf and MCU_S12Z_Compiler.pdf. In the MCU_S12Z_Assembler.pdf, there is "Chapter 12 Mixed C and Assembler Applications", and "12.1 Parameter Passing Scheme 

Check the backend chapter in the compiler manual for the details of parameter passing."

But I did not find contents about parameter passing in MCU_S12Z_Compiler.pdf, do you know where can I find such contents? Thanks.

在原帖中查看解决方案

0 项奖励
5 回复数
1,811 次查看
kef2
Senior Contributor IV

It happened because you changed function prototype, or perhaps you didn't have function prototype at all and compiler thought it should be different for some reason. It could be something like void foo(char), which became void foo(short). D0 and D1 are 8 bits wide registers, D2-D5 - 16 bits wide registers. Look for compiler documentation, just look for *.pdf in your Codewarrior installation. The look in compiler docs for something like calling convention, it should explain when and what registers are used.

Edward

0 项奖励
1,767 次查看
ZhaiHui
Contributor II

Hello Edward,

Thank you very much for the answer. We have function prototype like following and not changed.

void MCUInterface_Init(const McuInitModeType i_mode)

And we have declaration as following in assembly file.

XREF.L MCUInterface_Init

By the way, I searched the folder of Codewarrior installation, and get MCU_S12Z_Assembler.pdf and MCU_S12Z_Compiler.pdf. In the MCU_S12Z_Assembler.pdf, there is "Chapter 12 Mixed C and Assembler Applications", and "12.1 Parameter Passing Scheme 

Check the backend chapter in the compiler manual for the details of parameter passing."

But I did not find contents about parameter passing in MCU_S12Z_Compiler.pdf, do you know where can I find such contents? Thanks.

0 项奖励
1,760 次查看
kef2
Senior Contributor IV

Hi, @ZhaiHui 

What is McuInitModeType? Is it enum type? enum size is unspeciied, it may be 8bits, it may 16 bits. At least compiler may try to decide what is more optimal. 

 

See chapter 18.4 calling convention.

 

Regards

Edward

 

0 项奖励
1,746 次查看
ZhaiHui
Contributor II

Hello Edward,

Thank you very much for the answer. Yes, the McuInitModeType is enum, so I think it depends on compiler. We will try to modify it to primitive type. Thank you again.

0 项奖励
1,825 次查看
StanoA
NXP TechSupport
NXP TechSupport

Hello ZhaiHui,

The simplest way is to build the C-Function, then compile to assembler and copy the generated code into main assembler code. The advantage of this attempt is very simple procedure and you don’t need to save all possible affected registers used by compiled C-Function if it will be called from the asm code.

The next more complicated attempt is to define such exact position of the C-Function in *.prm file. Then if you want use this function you can use the “ld C_Addr” and “jmp ….” Command, but before you must save contents of all registers which could be used by compiled C-Function. In case you change this function you need to investigate which registers will be used by new C-Function and save possible other registers in asm code before use this function.

The other topic is the return from C-Function – you need to have saved the return address and use it when C-Function finishes.

So finally I suggest you to use the first option – write C-Function, compile it and copy the code into main asm code. It is more fluent asm code, more reliable and you don’t need pay attention to save the contents of many registers used by called C-Function.

I hope it can help you to solve your task.

Best Regards,

Stano.

0 项奖励