<?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>Necessary to save used registers in assembly ?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/14316/necessary-to-save-used-registers-in-assembly</link><description> I am using assembly language routines 
called from C code. 
Within the assembly routines, is it necessary to save the registers used? 
 
Ex: 
MY_ASM_ROUTINE: 
 PUSH ACC ; necessary ? 
 CLR A ; overwrite ACC 
 POP ACC 
 RET 
 
main(){ 
 MY_ASM_ROUTINE</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/121624?ContentTypeID=1</link><pubDate>Fri, 27 Jul 2001 13:08:30 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:47d1ff2a-930b-4239-9600-01e5849f3b4e</guid><dc:creator>Sergey Dymchishin</dc:creator><description>&lt;p&gt;&amp;gt;1. There is no difference between a C function and an assembly function.&lt;br /&gt;
&lt;br /&gt;
&amp;gt;5. The regfile is used to perform &lt;br /&gt;
&amp;gt;global register optimizations. &lt;br /&gt;
&amp;gt;Basically, a bipmap of the registers &lt;br /&gt;
&amp;gt;used in each function is built up and &lt;br /&gt;
&amp;gt;used by the C compiler to optimize &amp;gt;saving and restoring registers.&lt;br /&gt;
&lt;br /&gt;
I think you forget about $REGUSE directive.&lt;br /&gt;
&lt;br /&gt;
Sentence (1.) is not true. &lt;br /&gt;
Main difference between C and Assembler functions is that C compiler don&amp;#39;t know anything about using registers in assembler function.&lt;br /&gt;
&lt;br /&gt;
So, to force compiler apply &amp;#39;global optimisation&amp;#39; under assembler functions user MUST explicitly declare used registers with $REGUSE directive.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
--- file SWAPBYTE.C ---&lt;br /&gt;
&lt;pre&gt;
#pragma  SRC(SWAPBYTE.a51)


#pragma asm
   $REGUSE _SwapByte(A, R7 )
#pragma endasm

uchar SwapByte(unsigned char Byte)
{
  ACC = Byte;
#pragma asm
  swap a
#pragma endasm
  return( ACC );
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
P.S.&lt;br /&gt;
Without $REGUSE compiler generate code for all callers of SwapByte() with reloading registers R0,R1,...,R6,DPTR,B.&lt;br /&gt;
&lt;br /&gt;
Adding lines:&lt;br /&gt;
&lt;pre&gt;
#pragma asm
   $REGUSE _SwapByte(A, R7 )
#pragma endasm
&lt;/pre&gt;
in my current project reduce code size &lt;br /&gt;
&lt;b&gt;from 44639 to 44489 bytes&lt;b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/b&gt;&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/85228?ContentTypeID=1</link><pubDate>Fri, 27 Jul 2001 12:20:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:57ad1ce9-8e9a-4deb-ac65-030759265d8f</guid><dc:creator>Jim Burke</dc:creator><description>&lt;p&gt;Thanks for the reply Jon.&lt;br /&gt;
&lt;br /&gt;
I can therefore assume that the compiler&lt;br /&gt;
will restore registers after a call,&lt;br /&gt;
even if it is optimizing performance by&lt;br /&gt;
using registers. I do not need to&lt;br /&gt;
save used registers in an assembly&lt;br /&gt;
program ( an ISR is an exception ) since&lt;br /&gt;
the caller will assume its registers may have been overwritten.&lt;br /&gt;
&lt;br /&gt;
Thanks again for your reply.&lt;br /&gt;
It eliminated my doubt.&lt;br /&gt;
&lt;br /&gt;
Jim&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/85066?ContentTypeID=1</link><pubDate>Fri, 27 Jul 2001 12:03:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b002fe89-83fc-4cfe-ac3e-ebb6d6ed60c7</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;Here are a few things to consider:&lt;br /&gt;
&lt;br /&gt;
1. There is no difference between a C function and an assembly function.&lt;br /&gt;
&lt;br /&gt;
2. A called assembler function may use all registers A, B, R0-R7, and DPTR without saving and restoring them.&lt;br /&gt;
&lt;br /&gt;
3. It is ALWAYS the caller&amp;#39;s responsibility to reload register contents after a function call.&lt;br /&gt;
&lt;br /&gt;
4. If an assembler function (called from C) takes arguments and/or returns values, it must use the same conventions as the C compiler.  Refer to the manual.&lt;br /&gt;
&lt;br /&gt;
5. The regfile is used to perform global register optimizations.  Basically, a bipmap of the registers used in each function is built up and used by the C compiler to optimize saving and restoring registers.&lt;br /&gt;
&lt;br /&gt;
Hope this helps.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/84132?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2001 13:18:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:62b3098f-afae-46c6-be83-3d6bfe74719f</guid><dc:creator>Marcello Puri</dc:creator><description>&lt;p&gt;Hi Jim,&lt;br /&gt;
&lt;br /&gt;
I found these documents&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/939.htm"&gt;http://www.keil.com/support/docs/939.htm&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/904.htm"&gt;http://www.keil.com/support/docs/904.htm&lt;/a&gt;&lt;br /&gt;
They tell about REGFILE directives, but both apply to C51 v5.5 and I don&amp;#39;t know if they can hel you.&lt;br /&gt;
&lt;br /&gt;
I understand that REGFILE directives are&lt;br /&gt;
to be used in:&lt;br /&gt;
1. Assembly code (to specify which register are used in each function)&lt;br /&gt;
2. In C compiling parameters (to tell the compiler to optimize register usage following information retrieved in 1.)&lt;br /&gt;
3. In BL51 compiling parameters (why ??)&lt;br /&gt;
&lt;br /&gt;
I think that if you don&amp;#39;t use REGFILE directives C51 compiler assumes that *all* register are corrupted in the Assembly routine, so it &amp;quot;disables&amp;quot; the &amp;#39;Register variables optimization&amp;#39;.&lt;br /&gt;
&lt;br /&gt;
Try to analyze the Assembly file created by C51, so you can understand how it uses register to store variables &lt;br /&gt;
first and after Assembly-routine callings. (If C51 v4.10 doesn&amp;#39;t allow to create SRC file from C modules use a debugger or a disassembler)&lt;br /&gt;
&lt;br /&gt;
Tomorrow (in Italy is already 8:00 PM), I hope to make some test.&lt;br /&gt;
&lt;br /&gt;
Marcello&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/53800?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2001 09:00:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:06406ce1-a24e-4b9f-a8f1-78231770a1c7</guid><dc:creator>Jim Burke</dc:creator><description>&lt;p&gt;Hi Marcello,&lt;br /&gt;
&lt;br /&gt;
I agree, REGFILE seems worth investigating. I&amp;#39;m using v.4.10 of the compiler. It mentions REGFILE, and the&lt;br /&gt;
.reg files, but I can not find more information on their contents or an example of a .reg file.&lt;br /&gt;
&lt;br /&gt;
I&amp;#39;ve been using the compiler on one project for 4 years, as part of a team of a half-dozen programmers. I reviewed&lt;br /&gt;
all the assembly routines and found that most did save their used registers.&lt;br /&gt;
&lt;br /&gt;
A new project I&amp;#39;m looking at does not save it&amp;#39;s used registers, with the exception of the ISR&amp;#39;s. The code has been working for 10 years however. I&amp;#39;m just wondering if it is by luck, or if possibly a small change might then lead to registers in the C rotines to be overwritten when they call assembly routines.&lt;br /&gt;
&lt;br /&gt;
In the back of the manual rev 07.94, in the appendix H: &amp;#39;Writing Optimized Code&amp;#39;, under the heading &amp;#39;Local Variables&amp;#39;, it states:&lt;br /&gt;
&amp;quot;As part of the optimization process, the compiler attempts to maintain local variables in registers.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Thus, a routine that used optimization&lt;br /&gt;
and called assembly routines that did not save their used registers, would have the potential to corrupt the callers registers.&lt;br /&gt;
&lt;br /&gt;
The compliler also defaults to the highest level of optimization, level 6.&lt;br /&gt;
This includes level 4 and Register Variable optimization.&lt;br /&gt;
&lt;br /&gt;
I&amp;#39;ll either set the optimization level below 4, or add assembly code to save used registers.&lt;br /&gt;
&lt;br /&gt;
Jim&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Necessary to save used registers in assembly ?</title><link>https://community.arm.com/thread/37752?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2001 07:31:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:396763a9-dcbb-429b-9543-960929ef5cce</guid><dc:creator>Marcello Puri</dc:creator><description>&lt;p&gt;I&amp;#39;m a new user of Keil products, and i&amp;#39;ve the same problem.&lt;br /&gt;
But at page 135 of &amp;#39;C51 Compiler User&amp;#39;s Guide (01.97)&amp;#39; you can read:&lt;br /&gt;
&amp;quot;Assembler functions can change all register contents in the current selected register bank as well as the content of th register ACC, B, DPTR, PSW. [..]&amp;quot;.&lt;br /&gt;
IMHO this behaviour seems to be not optimized. &lt;br /&gt;
The best solution seems to be REGFILE directives of C51 and BL51. But I don&amp;#39;t know how they work. I will investigate. &lt;br /&gt;
&lt;br /&gt;
Marcello&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>