<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-7107396076752625506</atom:id><lastBuildDate>Tue, 09 Sep 2025 00:31:29 +0000</lastBuildDate><category>AIX</category><category>xlc</category><category>IBM</category><category>PowerPC</category><category>as</category><category>assembler</category><category>assembly</category><category>developerworks</category><category>tutorial</category><category>programming</category><category>function epilog</category><category>function prolog</category><category>malloc</category><category>stack-linkage</category><category>C</category><category>c++</category><category>dbx</category><category>registers</category><category>unix</category><category>.csect</category><category>.globl</category><category>.main</category><category>.set</category><category>3.1</category><category>allocator</category><category>bucket</category><category>compiler options</category><category>debugger</category><category>debugging</category><category>free</category><category>heap usage</category><category>identify memory leak</category><category>malloc debug</category><category>memory allocator c unix</category><category>performance</category><category>process_brk</category><category>report_allocations</category><category>svmon</category><category>vector application</category><title>root @ AIX</title><description>Views expressed in this blog are our own and in no way reflect those of our employers. Any similarity between our views and those of our employers is purely coincidental.</description><link>http://aixroot.blogspot.com/</link><managingEditor>noreply@blogger.com (Anonymous)</managingEditor><generator>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-3693092260665459870</guid><pubDate>Mon, 20 Dec 2010 14:02:00 +0000</pubDate><atom:updated>2010-12-20T06:12:08.014-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">as</category><category domain="http://www.blogger.com/atom/ns#">assembler</category><category domain="http://www.blogger.com/atom/ns#">assembly</category><category domain="http://www.blogger.com/atom/ns#">developerworks</category><category domain="http://www.blogger.com/atom/ns#">function epilog</category><category domain="http://www.blogger.com/atom/ns#">function prolog</category><category domain="http://www.blogger.com/atom/ns#">PowerPC</category><category domain="http://www.blogger.com/atom/ns#">registers</category><category domain="http://www.blogger.com/atom/ns#">stack-linkage</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>PowerPC Assembly Tutorial on AIX: Chapter 5, Saving non-volatile registers</title><description>&lt;div class=&quot;TextBodySingle&quot;&gt;We will now return to the problem we left at – why was the exit value 97 instead of 5? We now know that volatile registers don&#39;t retain their values across function calls, and when we called getchar(), the contents of r3 got overwritten. How do we overcome this? We can store the value of r3 in a non-volatile register before calling getchar. By convention, while using non-volatile registers, we use them in the order r31, r30 ... r13. Also, as we use each non-volatile register, we need to save them in the stack, and restore them when we return from the function. Hence the above program should be modified as follow:&lt;/div&gt;&lt;div class=&quot;TextBodySingle&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;ol&gt;&lt;li&gt;.set r0, 0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.set r1, 1&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.set r3, 3&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.set r31, 31&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.extern &lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.getchar&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Tell the assembler that .getchar is an external symbol&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.csect&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.globl&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp; &lt;/span&gt;.main&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;.main:&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# Function prolog begins&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mflr&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r0&lt;span style=&quot;mso-tab-count: 2;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Get the link register in r0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stw&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r31, -4(r1)&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Save caller&#39;s r31&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stw&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r0, 8(r1)&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Save the link register&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stwu&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r1, -64(r1)&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Store the stack pointer, and update. Create a frame of 64 bytes.&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# Function prolog ends&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;li&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r3, 5&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mr&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r31, r3&lt;span style=&quot;mso-tab-count: 1;&quot;&gt; &lt;/span&gt;# Copy contents of r3 into r31 for retention&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bl&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.getchar&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ori&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r0, r0, 0&lt;span style=&quot;mso-tab-count: 1;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;# No-op, required by compiler/loader after a branch to an external function&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mr&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r3, r31&lt;span style=&quot;mso-tab-count: 1;&quot;&gt; &lt;/span&gt;# Copy contents of r31 back to r3&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# Function epilog begins&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;addi&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r1, r1, 64&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Restore the stack pointer&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lwz&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r31, -4(r1)&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Restore caller&#39;s r31&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lwz&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r0, 8(r1)&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;# Read the saved link register&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mtlr&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;r0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# Function epilog ends&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt; &lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;blr&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;In line 13, we save the caller&#39;s (in this case __start&#39;s) r31 in the stack frame. In line 19, we copy the contents of r3 into r31, so that when getchar returns, although r3 would have been overwritten, we would still retain the value in r31. After getchar returns, we copy the contents of r31 to r3. We restore the caller&#39;s r31 in line 26 before returning from this function.&lt;/div&gt;</description><link>http://aixroot.blogspot.com/2010/12/powerpc-assembly-tutorial-on-aix.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-6422067190056881925</guid><pubDate>Tue, 30 Nov 2010 09:30:00 +0000</pubDate><atom:updated>2010-12-06T05:29:52.397-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">as</category><category domain="http://www.blogger.com/atom/ns#">assembler</category><category domain="http://www.blogger.com/atom/ns#">assembly</category><category domain="http://www.blogger.com/atom/ns#">developerworks</category><category domain="http://www.blogger.com/atom/ns#">function epilog</category><category domain="http://www.blogger.com/atom/ns#">function prolog</category><category domain="http://www.blogger.com/atom/ns#">PowerPC</category><category domain="http://www.blogger.com/atom/ns#">registers</category><category domain="http://www.blogger.com/atom/ns#">stack-linkage</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>PowerPC Assembly Tutorial on AIX: Chapter 4, Most frequently used registers</title><description>We will digress a little and introduce the most frequently used registers, before we deal with the question of why we got a return value of 97 instead of 5 in the previous program.&lt;br /&gt;
&lt;br /&gt;
In this section, we will not discuss all the registers of the PowerPC architectures, but only the most frequently used registers of the UISA (User Instruction Set Architecture) model. The UISA model defines the architecture to which user level programs should conform.&lt;br /&gt;
&lt;br /&gt;
In the UISA model, we have the following registers:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;32 General Purpose Registers (GPRs)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;32 Floating Point Registers (FPRs)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Condition Register (CR)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Floating Point Status and Control Register (FPSCR)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Exception Register (XER)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Link Register (LR)&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Counter Register (CTR)&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
There are 32 GPRs, named GPR0 – GPR31. These registers are 64-bit registers in 64-bit implementation, and 32-bit in 32-bit implementations. They can be used to manipulate integer data. GPR0 is used in function prologs. GPR1 is used as the stack pointer, and GPR2 is used as a pointer to the TOC, and these two registers should not be used for any other purpose.&lt;br /&gt;
&lt;br /&gt;
GPR0-GPR12 are volatile registers, that is, their values are not preserved across function calls. GPR13-GPR31 are non-volatile registers. If a function wishes to overwrite these non-volatile registers, it must first save the value in the stack, and restore the value before returning.&lt;br /&gt;
GPR12 is also used in special handling in the glink code (We&#39;ll see what the glink code is later). In 64-bit architectures, GPR13 contains the thread pointer.&lt;br /&gt;
&lt;br /&gt;
The link register is used to store the return address from a function call, and is generally automatically updated by the bl instruction. To return to the address contained in the LR, the blr instruction is used. The instruction &#39;mtlr&#39; (move to link register) can be used to modify the link register to an arbitrary value.&lt;br /&gt;
&lt;br /&gt;
The condition register (CR) is a 32-bit register divided into eight 4-bit fields, named CR0-CR7. The results of arithmetic and logical operations are stored in the condition register fields, and they can be used to perform conditional branches. CR0 is volatile, and CR1-CR7 are non-volatile. Hence, if any function attempts to change any of the condition registers CR1-CR7, it must save the state and restore it before returning to the caller.&lt;br /&gt;
&lt;br /&gt;
The Counter Register (CTR) is used to perform branches, and used in looping to hold the loop count value. The value of the counter register may be modified by the &#39;mtctr&#39; (move to counter register) instruction.</description><link>http://aixroot.blogspot.com/2010/11/most-frequently-used-registers.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-7412082992748948259</guid><pubDate>Fri, 19 Nov 2010 12:51:00 +0000</pubDate><atom:updated>2010-11-19T05:09:55.196-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">as</category><category domain="http://www.blogger.com/atom/ns#">assembler</category><category domain="http://www.blogger.com/atom/ns#">assembly</category><category domain="http://www.blogger.com/atom/ns#">developerworks</category><category domain="http://www.blogger.com/atom/ns#">function epilog</category><category domain="http://www.blogger.com/atom/ns#">function prolog</category><category domain="http://www.blogger.com/atom/ns#">PowerPC</category><category domain="http://www.blogger.com/atom/ns#">stack-linkage</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>PowerPC Assembly Tutorial on AIX: Chapter 3, Calling Other Functions</title><description>Suppose you want your program to wait for the user to press a key before exiting, you would call the getchar() function which is exported by libc. Calling getchar from our program is rather straight forward, and all we have to do is to include the following line in our program:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bl&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.getchar&lt;br /&gt;
&lt;br /&gt;
Note that in the above line, we have used .getchar instead of getchar. However, including this line alone in our program will not work, and in all probability, this program will just dump core. Do you know why?&lt;br /&gt;
&lt;br /&gt;
We had seen that when we issue the bl instruction, the link register gets overwritten with the address of the instruction following the current one. Hence, after the bl instruction, the link register will contain the address of the following instruction (which is a part of .main). After returning from getchar(), when we issue the instruction blr from .main, we would not return to __start, as we would have over-written the link register set by __start when it issued the bl instruction.&lt;br /&gt;
How do we solve the problem? We create a stack frame for main, and save the link register in the frame.&lt;br /&gt;
&lt;br /&gt;
Here is how the program would look like:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.set r0, 0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.set r1, 1&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.set r3, 3&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; .extern &amp;nbsp; &amp;nbsp; .getchar &amp;nbsp; # Tell the assembler that&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# .getchar is an external symbol&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; .csect&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.globl&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.main&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.main:&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; #### Function prolog begins ####&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; mflr &amp;nbsp; &amp;nbsp;r0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Get the link register in r0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stw &amp;nbsp; &amp;nbsp;r0, 8(r1) &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; # Save the link register&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stwu &amp;nbsp; &amp;nbsp;r1, -64(r1) &amp;nbsp; &amp;nbsp;# Store the stack pointer, and&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; # update. Create a frame of 64 bytes.&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;#### Function prolog ends&amp;nbsp;####&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;li&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3, 5&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bl&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.getchar&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ori &amp;nbsp; &amp;nbsp;r0, r0, 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# No-op, required by loader after a&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;# branch to an external function&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;#### Function epilog begins&amp;nbsp;####&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addi&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r1, r1, 64&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# Restore the stack pointer&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lwz &amp;nbsp; &amp;nbsp;r0, 8(r1) &amp;nbsp; &amp;nbsp;&amp;nbsp; # Read the saved link register&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mtlr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r0&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;#### Function epilog ends&amp;nbsp;####&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;blr&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
In the above program, line 5 tells the assembler that .getchar is an external symbol that is not present in the current file.&lt;br /&gt;
&lt;br /&gt;
Load and store operations cannot be performed directly on the link register, and hence we have to copy the contents of the link register to another general purpose register before storing it. The mflr (move from link register) instruction takes as an argument another register, and copies the contents of the link register to the specified register.&lt;br /&gt;
&lt;br /&gt;
In PowerPC, the convention is to use the general purpose register r1 as the stack pointer. In line 12, we save the value of the link register at an offset of 8 bytes from the stack pointer.&lt;br /&gt;
&lt;br /&gt;
In line 13, we use the special instruction stwu (or store word and update) to advance the stack pointer and save the old stack pointer. In this line, stwu stores the value of r1, at the address r1-64, and then stores the value r1-64 in r1. Hence this single instruction allows us to do the two tasks of decrementing the stack pointer, and storing the old stack pointer at one go.&lt;br /&gt;
&lt;br /&gt;
Having done this, we are ready to break into the main logic of the program. We use the bl instruction in line 18 to call getchar.&lt;br /&gt;
&lt;br /&gt;
There are several special instructions, which the assembler treats specially. The instruction in line 19 is treated as a no-op. A no-op is required by loader after a call to an external function is made. We shall see why it is required later. xlc will not compile the program without the no-op. &#39;as&#39; will not complain about it and compile the application.&lt;br /&gt;
&lt;br /&gt;
Having done our job, we now have to restore the old values of the stack pointer (r1) and the link register. In line 23, we restore the stack pointer to its old value, by adding the immediate value 64 to it. We then load the stored link register value in r0 at line 24. We then use the mtlr (move to link register) to copy the contents of r0 to the link register. We then finish it by calling blr.&lt;br /&gt;
&lt;br /&gt;
When we run this program, we see that it waits for us to enter something, and then returns.&lt;br /&gt;
&lt;br /&gt;
So far, so good, but when happens when I check the exit value returned by this program?&lt;br /&gt;
&lt;br /&gt;
$ ./a.out&lt;br /&gt;
97&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We are no longer getting 5!!!&lt;br /&gt;
&lt;br /&gt;
Just a note of caution, in this post, I have not followed the stack-linkage convention in its entirety, and have tried to simplify things and have only tried to capture the essence of stack-linkage. I will probably return to this topic in a later post.</description><link>http://aixroot.blogspot.com/2010/11/powerpc-assembly-tutorial-on-aix_19.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-6863963721526646256</guid><pubDate>Fri, 19 Nov 2010 12:25:00 +0000</pubDate><atom:updated>2010-11-19T05:10:01.269-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.csect</category><category domain="http://www.blogger.com/atom/ns#">.globl</category><category domain="http://www.blogger.com/atom/ns#">.main</category><category domain="http://www.blogger.com/atom/ns#">.set</category><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">as</category><category domain="http://www.blogger.com/atom/ns#">assembler</category><category domain="http://www.blogger.com/atom/ns#">assembly</category><category domain="http://www.blogger.com/atom/ns#">developerworks</category><category domain="http://www.blogger.com/atom/ns#">PowerPC</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>PowerPC Assembly Tutorial on AIX: Chapter 2, Adding Clarity</title><description>You would have noticed that the load instruction in line 5 is rather confusing as to which is the register, and which is the value being read. To make things clearer, we will use the .set assembler pseudo-op. C programmers can think of .set as a #define. The program will then look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#File. 1_2.s&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.set r3, 3&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.csect&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.globl .main&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.main:&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;li&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3, 5&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;blr&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
The above program has the same effect as 1_1.s</description><link>http://aixroot.blogspot.com/2010/11/powerpc-assembly-tutorial-on-aix.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-6098512487292411556</guid><pubDate>Thu, 18 Nov 2010 10:01:00 +0000</pubDate><atom:updated>2010-11-19T05:10:08.316-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">as</category><category domain="http://www.blogger.com/atom/ns#">assembler</category><category domain="http://www.blogger.com/atom/ns#">assembly</category><category domain="http://www.blogger.com/atom/ns#">developerworks</category><category domain="http://www.blogger.com/atom/ns#">PowerPC</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>PowerPC Assembly Tutorial on AIX: Chapter 1, The first steps</title><description>Its been quite some time since I last posted. In the next few posts, I&#39;ll try to present an introduction to PowerPC assembly on AIX.&lt;br /&gt;
The motivation for this comes from my personal experience trying to program in assembly on AIX. I found plenty of documentation on the instruction set, assembler directives etc. However, what I couldn&#39;t find was a step-by-step tutorial on how to write basic assembly programs. True, there were some developerworks articles, but the code presented in those articles hardly ever worked.&lt;br /&gt;
My endeavour is to present a primer into PowerPC assembly programming on AIX. Most of my programs will be sub-optimal, and simplistic. The goal is not to write perfect programs - rather to get someone started on PowerPC assembly programming on AIX, so that he can go on from here and take advantage of the large amount of material available on the web on PowerPC programming.&lt;br /&gt;
&lt;br /&gt;
The first program, usually written in any programming language, is the hello world program. However, writing a hello world program, in assembly is certainly not the easiest first. We will start with a much simpler program. A program that does nothing, or, almost nothing. The program just exits with an exit value.&lt;br /&gt;
&lt;br /&gt;
The default extension of an assembly program is .s. &lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#File. 1_1.s&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.csect&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.globl .main&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.main:&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;li 3, 5&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;blr&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;In this tutorial, we will use the xlc compiler to compile our first assembly program.  &lt;code&gt; $ cc 1_1.s &lt;/code&gt;  And now, onto running this program:  &lt;code&gt; $ ./a.out $ echo $? 5 &lt;/code&gt;  The first line in this program is a comment. Comments start with a #. A Comment can be placed anywhere in a line. Any text after the # in a line is ignored by the assembler.  The second line in the program tells the assembler that this is a csect, or a relocatable module. We will learn more about csects in section &lt;todo: fill=&quot;&quot; it=&quot;&quot; up=&quot;&quot;&gt;.&lt;br /&gt;
The third line tells the assembler that .main is a global symbol, and other objects can link to it.&lt;br /&gt;
&lt;br /&gt;
Line 4 is a label named .main. The assembler recognizes that this is a label by the colon following the label name. Line 3 and line 4 work together to signify that .main is a global symbol, and its address is specified by the label ine line 4.&lt;br /&gt;
&lt;br /&gt;
In PowerPC, the convention for a function to return a value is to store it in register 3. Line 5 loads the value 5 into the register 3. &#39;li&#39; is a load instruction, and loads an immediate value into a register.&lt;br /&gt;
&lt;br /&gt;
In AIX, whenever a binary is run, the function __start is automatically executed. __start then calls the symbol .main. &lt;br /&gt;
&lt;br /&gt;
In PowerPC, whenever one function calls another, it does so by executing the instruction bl, or branch and link. bl stores the address of the next instruction to be executed in the link register, and then branches to the specified address.&lt;br /&gt;
Therefore, when the callee function returns, it should start executing the instruction whose address is specified in the link register. While returning, the callee simply executes blr (branch to link register) instruction, which automatically loads the contents of the contents of the link register into the program counter and starts executing it.&lt;br /&gt;
&lt;br /&gt;
More posts on PowerPC assembly to come in the following weeks.&lt;/todo:&gt;</description><link>http://aixroot.blogspot.com/2010/11/powerpc-assembly-tutorial-on-aix-first.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-563910455707516039</guid><pubDate>Mon, 11 May 2009 07:22:00 +0000</pubDate><atom:updated>2009-05-11T01:26:26.377-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">c++</category><category domain="http://www.blogger.com/atom/ns#">compiler options</category><category domain="http://www.blogger.com/atom/ns#">debugging</category><category domain="http://www.blogger.com/atom/ns#">IBM</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">unix</category><category domain="http://www.blogger.com/atom/ns#">xlc</category><title>xlc Compiler options to be used with debugger</title><description>While doing source code debugging, one generally compiles with -g option, and assumes that all compiler optimizations have been turned off. However, as far as the xlc compiler is concerned, this might not necessarily be true. With -g the compiler puts in line-number information and turns off some optimizations, but not all optimizations. To tell the compiler to turn off ALL optimizations, the -qnoopt option should be employed.</description><link>http://aixroot.blogspot.com/2009/05/xlc-compiler-options-to-be-used-with.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-9145469110785183416</guid><pubDate>Sat, 26 Jul 2008 15:53:00 +0000</pubDate><atom:updated>2008-07-26T09:23:39.333-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">dbx</category><category domain="http://www.blogger.com/atom/ns#">free</category><category domain="http://www.blogger.com/atom/ns#">heap usage</category><category domain="http://www.blogger.com/atom/ns#">IBM</category><category domain="http://www.blogger.com/atom/ns#">malloc</category><category domain="http://www.blogger.com/atom/ns#">process_brk</category><category domain="http://www.blogger.com/atom/ns#">svmon</category><title>Heap usage</title><description>I haven&#39;t posted in a while, due to lack of time and too much work, and I think this post has been rather overdue.&lt;br /&gt;One of the things aspects of AIX&#39;s malloc (or for that matter any other operating system) is that if you free the memory you have allocated, it won&#39;t reflect in the svmon output. This is because malloc subsystem caches the memory, to be used for further malloc.&lt;br /&gt;An easy way to see how much memory your application is using (the memory malloced by it, + the memory in the free pool maintained by the malloc subsystem) is to use the variable process_brk which is exported by libc.&lt;br /&gt;The way I usually go about it is to use the dbx subcommand&lt;br /&gt;(dbx) p &amp;process_brk&lt;br /&gt;This gives me the address to dump, which I dump using a command similar to the one below&lt;br /&gt;(dbx) 0x12345678/3X&lt;br /&gt;This will give an output of three words..&lt;br /&gt;12345678 12345678 1&lt;br /&gt;The first word signifies what was the brk value before the first malloc was done, and the second word tells you what was the brk value after the second allocation was done. The third word tells how many sbrk()s were done. Of course, this gives me a very good estimate of the total memory used by my program.&lt;br /&gt;Another benifit of this thing is to check for heap/stack collision. To check whether there has been a heap stack collision in my 32-bit app, what I normally do is to dump the stack-pointer, and check whether the stack_pointer falls within the process_brk minimum and maximum limits.&lt;br /&gt;Hope this helps.</description><link>http://aixroot.blogspot.com/2008/07/heap-usage.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-8067704854282713015</guid><pubDate>Tue, 08 Jul 2008 18:05:00 +0000</pubDate><atom:updated>2008-07-26T09:21:32.642-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">dbx</category><category domain="http://www.blogger.com/atom/ns#">debugger</category><category domain="http://www.blogger.com/atom/ns#">IBM</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>A few dbx quickies</title><description>Here are some of the dbx commands I frequently use to speed things up:&lt;br /&gt;&lt;br /&gt;1. addcmd : Whenever I hit a breakpoint, I need to display the stack. I use addcmd to automatically display the stack when a breakpoint is hit.&lt;br /&gt;&lt;br /&gt;2. set $repeat : I usually get tired of pressing &#39;s&#39; again and again while stepping through a program. I set this variable to automatically re-execute the last command when enter is pressed.&lt;br /&gt;&lt;br /&gt;3. set -o vi : same as ksh&#39;s set -o vi&lt;br /&gt;&lt;br /&gt;4. -E option : I often have to debug programs with MALLOC DEBUG exported. However, I don&#39;t want dbx itself to run with MALLOC DEBUG on. I invoke dbx as follows:&lt;br /&gt;dbx -E MALLOCTYPE=debug -E MALLOCDEBUG=report_allocations,catch_overflow ./a.out&lt;br /&gt;&lt;br /&gt;5. goto : Useful if I want to skip executing some code at times. Usually I do this if there is some user input to be read, or some sleep event, I just skip all that by using goto. Goto directly sets the instruction pointer.&lt;br /&gt;&lt;br /&gt;I hope you find these tips useful. I&#39;ll put some more tips at some later point of time.</description><link>http://aixroot.blogspot.com/2008/07/few-dbx-quickies.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-2946774568773513828</guid><pubDate>Sun, 22 Jun 2008 18:27:00 +0000</pubDate><atom:updated>2008-07-26T09:21:03.951-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">IBM</category><category domain="http://www.blogger.com/atom/ns#">identify memory leak</category><category domain="http://www.blogger.com/atom/ns#">malloc</category><category domain="http://www.blogger.com/atom/ns#">malloc debug</category><category domain="http://www.blogger.com/atom/ns#">memory allocator c unix</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">report_allocations</category><title>Identifying memory leaks on AIX</title><description>Memory leaks are often a pain, especially if the program runs for a long time. The pain is increased by the difficulty of pin-pointing the exact place where the memory leak occurs. There are many commercial tools available which do an excellent job of pin-pointing memory leaks, but most of them come at a cost. If you use AIX, though, you have a pretty much plain jane, but very flexible and usable option of the malloc debug tool called report_allocations.&lt;br /&gt;With every call to malloc, a log is added which stores the stack where it was allocated from (yes, it does some dbx like stack walk thingy), and with every call to free() the corresponding log is deleted. An atexit handler is registered which prints all the logged entries. Guess this clarifies how report_allocations work.&lt;br /&gt;To enable report_allocations one needs export the following environment variables&lt;br /&gt;&lt;br /&gt;export MALLOCTYPE=debug&lt;br /&gt;export MALLOCDEBUG=&quot;report_allocations,stack_depth:4&quot;&lt;br /&gt;&lt;br /&gt;stack_depth indicates how much of the stack information is to be stored, the default being 4 and the highest being 32.&lt;br /&gt;Malloc debug does not do any aggregation of allocations of a similar kind (that is allocations of the same size from the same stack trace), but prints all allocations one by one. This is sometimes a drawback, however, one can easily write a script around malloc-debug to aggregate the data. One such script is readily available in a developerworks article. The author of the script, however, warns that the script may break in some corner conditions. However, I guess we can fix those issues. Over to developerworks for more info:&lt;br /&gt;&lt;a href=&quot;http://www.ibm.com/developerworks/aix/library/au-mallocdebug.html&quot;&gt;http://www.ibm.com/developerworks/aix/library/au-mallocdebug.html&lt;/a&gt;</description><link>http://aixroot.blogspot.com/2008/06/identifying-memory-leaks-on-aix.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7107396076752625506.post-1448137872309063735</guid><pubDate>Mon, 16 Jun 2008 17:36:00 +0000</pubDate><atom:updated>2008-07-26T09:16:54.358-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">3.1</category><category domain="http://www.blogger.com/atom/ns#">AIX</category><category domain="http://www.blogger.com/atom/ns#">allocator</category><category domain="http://www.blogger.com/atom/ns#">bucket</category><category domain="http://www.blogger.com/atom/ns#">c++</category><category domain="http://www.blogger.com/atom/ns#">IBM</category><category domain="http://www.blogger.com/atom/ns#">malloc</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">unix</category><category domain="http://www.blogger.com/atom/ns#">vector application</category><title>The beginnings: Speeding up C++ STL Vector applications</title><description>Having started this blog quite some time ago, I didn&#39;t have any idea what I was going to post in it. I only knew that it would contain tips and tricks for AIX which one might find handy, some of which one might find elsewhere, some of which one may not find elsewhere. I still am not sure as to what sort of tips and tricks I&#39;m going to put, but I&#39;ll put them, thats for sure. Don&#39;t ask me about the frequency.&lt;br /&gt;&lt;br /&gt;This first post is for guys who use STL Vectors heavily in their C++ code. If you guys want an improvement in performance, you might want to use the 3.1 malloc allocation policy. All you have to do is to export one of the following environment variable before running your code:&lt;br /&gt;&lt;br /&gt;export MALLOCTYPE=3.1  #for 32-bit applications&lt;br /&gt;export MALLOCTYPE=3.1_64BIT #for 64-bit applications&lt;br /&gt;&lt;br /&gt;How does it help? Well, it appears, that a vector keeps growing itself as and when it needs. Hence a lot of calls to realloc(). What happens with the 3.1 allocation policy is that requests are rounded up to the next higher power of 2. That is if you request 1026 bytes, you get 2048 bytes. Hence when a realloc is done, it often returns without doing any work since the memory is already there.&lt;br /&gt;3.1 is also faster than the default allocation policy as the default allocation policy uses a tree based data structure, essentially forcing the average time of O(log(n)), 3.1 is a bucket-based allocator with time complexit O(1).&lt;br /&gt;The caveat to using 3.1 is that it is wasteful of memory, so its upto you to decide what you want to compromise on. Memory usage or speed.&lt;br /&gt;&lt;br /&gt;How we found this out? Well, we were running some benchmark tests, and on exporting MALLOCTYPE=3.1 we found the performance to shoot up by 40%. Of course, the benchmark we were running was memory bound.</description><link>http://aixroot.blogspot.com/2008/06/beginnings-speeding-up-c-stl-vector.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item></channel></rss>