tags:

views:

82

answers:

4

I am using HCS08 and Code Warrior. I am calling a C function from assembly. How can I pass parameter to this C function?

+2  A: 

push it to the stack and then call the function.

karlphillip
You mean do something like this: pshAjsr cdc_putc where cdc_putc is C function void cdc_putc(char c) So, C will get the value of accumulator?
Punit
well it didnt solve the problem, I am getting "ILLEGAL BP" on debugger.
Punit
+4  A: 

What you want is the ABI, or Application Binary Interface for your platform. That will explain things like how to pass arguments to functions (registers, stack, a mix), which registers are caller saves and which are callee saves, special purposes for certain registers, and so on. By following a common ABI you are able to link libraries built by different compilers, mix high-level languages and so on.

For "big" platforms it's usually easy to find a document specifying the ABI. For others you may have to rely on disassembling a C function and looking at what it does. Pay attention to which registers it saves in its prologue and which ones it might smash. Also note how the prologue saves the stack pointer (or frame pointer) on entry because you will have to mimic that if you want debuggers to work.

It looks like the calling convention for HCS08 is documented by Freescale in an appnote.

Ben Jackson
+1 for both where to look for the calling convention, AND how to figure it out on one's own if documentation can not be found.
Sparky
A: 

Well, i found the solution.

In assembly file declare the variable as: XREF varaible1 and use it as memory location

In C file declare the variable as global. extern char variable1;

Punit
A: 

Write c version (may be just a stub only passing arguments) for your function and analyse the code. Do your function same way. Calling method for specific function (in most cases) is uniquely defined by its set of arguments.

The common way is that first some arguments if they are integers or pointers passed in registers and the others are pushed on stack. But also note that varargs are often passed in different way than non-varargs.

Specially for 6808 which have few registers all parameters are likely passed through stack.

Vovanium