Discussion Forum

Call C function (that calls another C function) from assembly code

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Nguyen Anh Huy
Posted
8-Feb-2006 03:41 GMT
Toolset
C51
New! Call C function (that calls another C function) from assembly code
Hi all,
I want to call a C function (for example: func1() ) from my old assembly code. The problem is that if func1() calls another C function( func2() ), what will happen? How does the compiler pass parameters to the second function? If it also passes through R1-R7, does it destroy the parameters of func1()?
Thanks for reading this topic.
Read-Only
Author
Andy Neil
Posted
8-Feb-2006 06:51 GMT
Toolset
C51
New! It's irrelevant!
"The problem is that if func1() calls another C function( func2() )"

That is entirely irrelevant!

The C51 calling convention defines how to call a function. It is the same for every function - that's the whole point of a calling convention.

"If it also passes through R1-R7, does it destroy the parameters of func1()?"

It will ensure that it does not destroy anything that it'll need again!

Note that your assembler code must assume that the C51 function call will destroy all registers.

You have read the chapter in the Manual on interfacing C51 and Assembler, haven't you?!
Read-Only
Author
Reinhard Keil
Posted
8-Feb-2006 08:17 GMT
Toolset
C51
New! RE: It's irrelevant!
There is extensive information about this on this web side. For example:
http://www.keil.com/support/docs/2163.htm

Reinhard
Read-Only
Author
erik malund
Posted
8-Feb-2006 14:41 GMT
Toolset
C51
New! RE: It's irrelevant!
one thing not mentioned in the above link:

C is free to use (and not save) ANY register it see fit. This may/will change with different releases and different optimizations. Thus so called "testting" to find which registers are affecter is worthless as "testing' often is.

Thus you MUST code like this
lcall Cfunct ; ALL REGISTRES INVALID AFTER CALL

Erik
Read-Only
Author
Andy Neil
Posted
8-Feb-2006 15:16 GMT
Toolset
C51
New! Registers after call
"Thus you MUST code like this
lcall Cfunct ; ALL REGISTRES INVALID AFTER CALL"


If the function returns a value, it will use registers as defined here:
http://www.keil.com/support/man/docs/c51/c51_ap_funcret.htm
Read-Only
Author
erik malund
Posted
8-Feb-2006 15:34 GMT
Toolset
C51
New! RE: Registers after call
correct, but that register is still "destroyed"

I once had to fix somebodys code that called C from assembly and, the C called only modified was it r4? under one particular condition and, of course you had the "monthly intermittent".

So much about mixing C and assembler is done with assumptions and thus likely causes of "unexplained failure"

Erik
Read-Only
Author
Nguyen Anh Huy
Posted
9-Feb-2006 01:58 GMT
Toolset
C51
New! I have read the manual
To Andy Neil,
I have read the manual, of course. But in the manual's example, it only calls one C function, and it doesn't save any registers!!! So I confuse that if the first C function calls another C function in it, does it save the registers?
I also see that, in the Large Model Example, page 171, line 11:
     MOV R0, #?_funtion?BYTE+3
I think it must be:
      MOV DPTR, #_?function?BYTE+3
Does it right?
Read-Only
Author
Andy Neil
Posted
9-Feb-2006 07:05 GMT
Toolset
C51
New! Black box.
"I confuse that if the first C function calls another C function"

What happens inside the function is entirely irrelevant - See my original post.

You just need to see the function as a "black box" - it has a defined interface, and what actually goes on inside the box is of no concern.
Read-Only
Author
Andy Neil
Posted
9-Feb-2006 07:58 GMT
Toolset
C51
New! LARGE Model example
"I also see that, in the Large Model Example, page 171, line 11:
MOV R0, #?_funtion?BYTE+3
I think it must be:
MOV DPTR, #_?function?BYTE+3"


Hmmm... I think you're right.
Looks like a cut-and-paste error - they've copied the COMPACT example, and forgotten to change R0 to DPTR?
You could check this by compiling an example yourself...

also here:
http://www.keil.com/support/man/docs/c51/c51_ap_largemodel.htm

Next Thread | Thread List | Previous Thread Start a Thread | Settings