<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/19488/call-c-function-that-calls-another-c-function-from-assembly-code</link><description> 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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/97846?ContentTypeID=1</link><pubDate>Thu, 09 Feb 2006 01:58:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1749d993-a45c-42d9-b4cd-1f85fd860a91</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;I also see that, in the Large Model Example, page 171, line 11:&lt;br /&gt;
     MOV R0, #?_funtion?BYTE+3&lt;br /&gt;
I think it must be:&lt;br /&gt;
      MOV DPTR, #_?function?BYTE+3&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Hmmm... I think you&amp;#39;re right.&lt;br /&gt;
Looks like a cut-and-paste error - they&amp;#39;ve copied the COMPACT example, and forgotten to change R0 to DPTR?&lt;br /&gt;
You could check this by compiling an example yourself...&lt;br /&gt;
&lt;br /&gt;
also here:&lt;br /&gt;
&lt;a href="http://www.keil.com/support/man/docs/c51/c51_ap_largemodel.htm"&gt;http://www.keil.com/support/man/docs/c51/c51_ap_largemodel.htm&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/97845?ContentTypeID=1</link><pubDate>Thu, 09 Feb 2006 01:05:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e1bc3c32-077a-4dfb-8a85-77406b729de8</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;I confuse that if the first C function calls another C function&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
What happens inside the function is entirely irrelevant - See my original post.&lt;br /&gt;
&lt;br /&gt;
You just need to see the function as a &amp;quot;black box&amp;quot; - it has a defined interface, and what actually goes on inside the box is of no concern.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/73823?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 19:58:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b67a7e5c-94b0-45b9-bfaa-caaeff233a0e</guid><dc:creator>Nguyen Anh Huy</dc:creator><description>&lt;p&gt;To Andy Neil,&lt;br /&gt;
I have read the manual, of course. But in the manual&amp;#39;s example, it only calls one C function, and it doesn&amp;#39;t save any registers!!! So I confuse that if the first C function calls another C function in it, does it save the registers?&lt;br /&gt;
I also see that, in the Large Model Example, page 171, line 11:&lt;br /&gt;
&lt;pre&gt;
     MOV R0, #?_funtion?BYTE+3
&lt;/pre&gt;
I think it must be:&lt;br /&gt;
&lt;pre&gt;
      MOV DPTR, #_?function?BYTE+3
&lt;/pre&gt;
Does it right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/123134?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 09:34:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:635ff444-092a-48ec-a74d-6bdfef897af2</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;correct, but that register is still &amp;quot;destroyed&amp;quot;&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;monthly intermittent&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
So much about mixing C and assembler is done with assumptions and thus likely causes of &amp;quot;unexplained failure&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Erik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/111933?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 09:16:19 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f1663921-f009-4718-bcd0-114830d361a8</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;Thus you MUST code like this&lt;br /&gt;
lcall Cfunct ; ALL REGISTRES INVALID AFTER CALL&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
If the function returns a value, it will use registers as defined here:&lt;br /&gt;
&lt;a href="http://www.keil.com/support/man/docs/c51/c51_ap_funcret.htm"&gt;http://www.keil.com/support/man/docs/c51/c51_ap_funcret.htm&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/97842?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 08:41:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c9d1a00e-64d3-4e27-9438-7480ffea8169</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;one thing not mentioned in the above link:&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;testting&amp;quot; to find which registers are affecter is worthless as &amp;quot;testing&amp;#39; often is.&lt;br /&gt;
&lt;br /&gt;
Thus you MUST code like this&lt;br /&gt;
 lcall Cfunct ; ALL REGISTRES INVALID AFTER CALL&lt;br /&gt;
&lt;br /&gt;
Erik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/87583?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 02:17:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2005f275-d46b-469c-ac6d-a1cfdb23f804</guid><dc:creator>Keil Software Support Intl.</dc:creator><description>&lt;p&gt;There is extensive information about this on this web side.  For example:&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/2163.htm"&gt;http://www.keil.com/support/docs/2163.htm&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Reinhard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call C function (that calls another C function) from assembly code</title><link>https://community.arm.com/thread/45526?ContentTypeID=1</link><pubDate>Wed, 08 Feb 2006 00:51:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:40a000a7-e02d-409a-988b-12716b4cc2d8</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;The problem is that if func1() calls another C function( func2() )&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
That is entirely irrelevant!&lt;br /&gt;
&lt;br /&gt;
The C51 calling convention defines how to call a function. It is the same for &lt;i&gt;&lt;b&gt;every&lt;/b&gt;&lt;/i&gt; function - that&amp;#39;s the whole point of a &lt;i&gt;calling convention&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;If it also passes through R1-R7, does it destroy the parameters of func1()?&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
It will ensure that it does not destroy anything that it&amp;#39;ll need again!&lt;br /&gt;
&lt;br /&gt;
Note that your assembler code must assume that the C51 function call will destroy all registers.&lt;br /&gt;
&lt;br /&gt;
You have read the chapter in the &lt;b&gt;Manual&lt;/b&gt; on interfacing C51 and Assembler, haven&amp;#39;t you?!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>