<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>technochakra.com &#187; assembly</title>
	<atom:link href="http://www.technochakra.com/tag/assembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.technochakra.com</link>
	<description>Wheels Of Technology</description>
	<lastBuildDate>Thu, 04 Feb 2010 11:48:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Debugging &#8211; Modifying Code At Runtime</title>
		<link>http://www.technochakra.com/debugging-modifying-code-at-runtime/</link>
		<comments>http://www.technochakra.com/debugging-modifying-code-at-runtime/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 12:15:44 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=343</guid>
		<description><![CDATA[Introduction The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not. Sometimes it is good to be able to tweak the logic of your code during a debugging session. This article explains how to make minor adjustments to code without [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not.  Sometimes it is good to be able to tweak the logic of your code during a debugging session.  This article explains how to make minor adjustments to code without having to rebuild your application.</p>
<p>The article requires tweaking the assembly  and dealing with opcodes.  If you are not very familar with debugging in assembly, you may want to read <a href="http://www.technochakra.com/assembly-and-the-art-of-debugging/" target="_blank">this article</a> first.  The tips shared here are also useful while debugging libraries where you either do not have the source code or the build environment is unavailable.</p>
<p>Debuggers work on the common principle of launching the executable as a child program and requesting special permissions from the operating system to access and alter the state of the program.  This allows it to show and/or modify the memory, registers, code, etc in a debugging session.  If a debugger attaches itself to another program already loaded in memory, it requests special permission from the operating system to take control of the program for the purpose of debugging.  It will now seem very obvious why the same program in memory cannot be attached by two debuggers at the same time.  The operating system caters to the request on a first come first basis and refuses permissions to the second debugger.</p>
<p>The debugger&#8217;s access to a program&#8217;s memory and registers not only allows users to view the state of the program but also set software breakpoints.  Software breakpoints are set by altering and restoring the instructions in memory and has already been covered indepth in an <a href="http://www.technochakra.com/software-breakpoints/" target="_blank">earlier article</a>.</p>
<h3>How to modify code at runtime</h3>
<p>Altering instructions without compilation of code during debugging works on a similar logic.  If one uses the debugger to alter instructions of the program which it controls, then one can essentially change the logic compiled into the executable that is currently loaded in memory.  It is worth noting that altering a program in memory is temporary.  When the program is restarted and reloaded in memory, all changes are lost and no real harm has been done.  As this requires directly tweaking the opcodes in memory, one needs to know how to deal with assembly and this cannot be accomplished with knowledge of a high level language alone.<br />
Independent of the operating system and debugger, the steps needed to change the logic from within the debugger are as follows -</p>
<ol>
<li>Launch the program in a debugger.</li>
<li>Set a breakpoint in the code at the location which you want to alter execution.</li>
<li>Execute the program and drive the program so that your breakpoint is hit.</li>
<li>Request the debugger to display the disassembly of the code.</li>
<li>Get comfortable with the source-assembly mapping.</li>
<li>Identify the address of the assembly line you would like to alter.  Altering usually means changing an if statement to if NOT or &#8220;jump if zero&#8221; to &#8220;jump if not zero&#8221;.</li>
<li>Take the address and dump the memory contents at that address.  You will see opcodes corresponding to the assembly you just saw.</li>
<li>Modify the memory location with new opcodes (more on this later).</li>
<li>Cross your fingers so that your change won&#8217;t cause a crash <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Disable the breakpoint so that execution doesn&#8217;t stop too many times at this breakpoint as it makes debugging distracting.</li>
<li>Continue execution and now the program will respond to the new logic in the program.</li>
<li>To undo the effect of change, restart the program.</li>
</ol>
<h3>Example</h3>
<p>Let us look at the following simple snippet of code below.</p>
<p><code> bool flag = true;<br />
if( !flag )<br />
cout &lt;&lt; "flag is false" ;<br />
else<br />
cout &lt;&lt; "flag is true" ; </code></p>
<p>The code when compiled and run normally would print &#8220;flag is true&#8221; and this is obvious from the code above.  Say we at runtime would like to alter the logic by replacing if(!flag) with if(flag) so that the statement &#8220;flag is false&#8221; is printed instead.<br />
Let us see how to do this with the two most commonly available debuggers.</p>
<h4>GDB</h4>
<p>The steps below are in the same order as the generic steps explained earlier.</p>
<ul>
<li>gdb &lt;myprogram&gt;</li>
<li>break &lt;line_number_of_if_statement&gt;</li>
<li>run   Code will stop at the breakpoint set above.</li>
<li>disassemble.  The dump on my machine is as follows -</li>
</ul>
<blockquote><p>0&#215;00401170 &lt;main+32&gt;:   call   0x40f260 &lt;_alloca&gt;<br />
0&#215;00401175 &lt;main+37&gt;:   call   0&#215;410410 &lt;__main&gt;<br />
0x0040117a &lt;main+42&gt;:   movb   $0&#215;1,-0&#215;1(%ebp)<br />
0x0040117e &lt;main+46&gt;:   cmpb   $0&#215;0,-0&#215;1(%ebp)<br />
0&#215;00401182 &lt;main+50&gt;:   jne    0x40119a &lt;main+74&gt;<br />
0&#215;00401184 &lt;main+52&gt;:   movl   $0&#215;443000,0&#215;4(%esp)<br />
0x0040118c &lt;main+60&gt;:   movl   $0x4463a0,(%esp)<br />
0&#215;00401193 &lt;main+67&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0&#215;00401198 &lt;main+72&gt;:   jmp    0x4011ae &lt;main+94&gt;<br />
0x0040119a &lt;main+74&gt;:   movl   $0x44300e,0&#215;4(%esp)<br />
0x004011a2 &lt;main+82&gt;:   movl   $0x4463a0,(%esp)<br />
0x004011a9 &lt;main+89&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0x004011ae &lt;main+94&gt;:   mov    $0&#215;0,%eax</p></blockquote>
<ul>
<li>Note that main+67 and main+89 are calls to cout.  We have now identified the two important statements and the if check should be just above it.  It usually helps to corelate the assembly with the source code in this manner.</li>
<li>The if check is on lines main+42, main+46 and main+50 (if !(flag==0) ).  If we change jne to je, we can negate the if condition and thus alter the logic of the code.  The address of the opcode jne above is 0&#215;00401182.</li>
<li>x/6x 0&#215;00401182  will dump the opcodes and operands in hexadecimal.  In the dump below, 0&#215;75 is the opcode for jne and the operand is 0&#215;16.  For details, refer to the Intel instruction set.</li>
</ul>
<blockquote><p>0&#215;401182 &lt;main+50&gt;:     0&#215;75    0&#215;16    0xc7    0&#215;44    0&#215;24    0&#215;04</p></blockquote>
<ul>
<li>set *(char*)0&#215;00401182=0&#215;74 where 0&#215;74 is the opcode for je. Effectively &#8220;jump if not equal&#8221; has been changed to &#8220;jump if equal&#8221; thereby inverting the logic of the if statement.</li>
<li>cross your fingers.</li>
<li>disable  1.  Disable breakpoint set earlier.</li>
<li>continue</li>
</ul>
<p>You will observe that as we have modified the runtime code using gdb, the output of the program after the above steps would be &#8220;flag is false&#8221;.</p>
<h4>Visual Studio</h4>
<p><a href="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg"><img class="alignnone size-full wp-image-345" title="assembly_vs" src="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg" alt="Assembly Screen Shot in Visual Studio" width="566" height="111" /></a><br />
The above screen shot shows the debugger displaying the assembly after hitting the breakpoint at the if statement.  Do make sure that when you right click in the assembly, &#8220;Show Code Bytes&#8221; and &#8220;Show Address&#8221; are checked.<br />
From the screen shot above, the address of jne instruction is 0x003314B8 and its opcode is 0&#215;75.  If we modify the opcode at location 0x003314B8 and change it to 0&#215;74 (opcode for je), we effectively negate the if statement at runtime.</p>
<p>To do this, open the memory window (Debug-&gt;Windows-&gt;Memory-&gt;Memory 1) and enter 0x003314B8 in the Address field.  The first byte shows as 0&#215;75.  Put the cursor on 0&#215;75 and type 74 to change the opcode.  You can confirm this by seeing the effect in the disassembly window.</p>
<p>This modifies the code in memory and now when the program execution is continued, the output would be &#8220;flag is false&#8221;.</p>
<h4>Conclusion</h4>
<p>Another useful tip to keep in mind is that a lot of code can be disabled at runtime if all corresponding opcodes and operands are replaced with NOPs which has the opcode 0&#215;90 on an Intel machine.  Similarly useful results can also be achived by altering the operands instead of the opcodes.</p>
<p>The ability to change code at runtime is an effective tool for quick debugging though it requires a good understanding of the code at the assembly level.   This technique is meant for small tweaks because it is faster to recompile in case complex changes in assembly are needed.   Moreover, when the program finishes execution, the modification needs to be redone as this technique does not modify the image on the disk.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-2898169159040774";
/* 468x60, created 7/1/09 */
google_ad_slot = "5882807646";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/debugging-modifying-code-at-runtime/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Assembly And The Art Of Debugging</title>
		<link>http://www.technochakra.com/assembly-and-the-art-of-debugging/</link>
		<comments>http://www.technochakra.com/assembly-and-the-art-of-debugging/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 12:56:29 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=287</guid>
		<description><![CDATA[Debugging in assembly is not an optional skill to have.   Every developer encounters a situation where there is no other alternative other than cracking open the assembly code.  Here are a few reasons why a developer should get their hands dirty with this skill -]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">This article is an introduction to understanding Intel&#8217;s assembly language while debugging a computer program.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Debugging in assembly can be a daunting task and not every developer likes to make sense of the mnemonics that appear on the screen.  While debugging, one is normally used to viewing local variables, parameters  and syntax highlighted code in the most user friendly manner and that is the primary reason why many developers dislike debugging in assembly initially.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Why Debug In Assembly?</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">1.  You have a binary and its corresponding code but the build environment is not functional.  This could be because of missing tools, unavailable compilers and the lack of understanding of complex scripts and steps needed to recreate the binary.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">2.  You are dealing with a release build only bug and the debug symbols are unavailable or you are dealing with a third party library and don&#8217;t have access to the source code.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">3.  You like to have fun.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">What You Need?</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">1.  A debugger that can display assembly language (Visual Studio, WinDbg, gdb, XCode are capable debuggers).</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">2.  A ready reference to the Intel instruction set.  Google works just fine.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">3.  Patience and maybe a link to this article.  :-)</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The key to understanding debugging in assembly is to understand how functions are called, parameters are passed, local variables accessed.  The rest can be understood by referring the Intel Instruction set while debugging the assembly code.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The diagram below is roughly how a stack should look like once a function call has been made.  When one encounters assembly, mapping the code to this diagram should simply the debugging experience.  Note that the stack grows downwards.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Passing Parameters To A Function</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Parameters are the first to be pushed onto the stack.  The caller of the function pushes them from right to left.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Cleanup Of Parameters</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Parameter cleanup may happen in the function that was called (e.g. stdcall calling convention) or by the caller of the function (e.g. cdecl).  Various kinds of calling conventions of the x86 architecture are explained in detailed in this wiki article. http://en.wikipedia.org/wiki/X86_calling_conventions  Looking right after the function call gives a good idea of what convention was being used when compiling the code.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Take note that if cleanup happens in the function being called, variable number of arguments cannot be passed to it.  Whereas it becomes possible to do this if the parameter cleanup happens outside the function.  This essentially is the main difference between stdcall and cdelc calling conventions on a x86 architecture.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The Function Call</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Before control is passed to the function, the return address where the program is supposed to resume once the function has finished execution is pushed to the stack.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">To recap, parameters are pushed first, followed by the return address.  Later depending on the calling convention of the function, you may see cleanup hhappening right after the function call or within the function itself.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">In the diagram above, as the stack grows downwards, the parameters are at the top followed by the return address.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Example</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Take a look at the snippet and the equivalent assembly below :</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The Function</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The function usually starts with a prolog and ends with an epilog</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The prolog is either a simple ENTER instruction or more commonly, it saves the ebp register and copies the stack pointer value in it so as to use it as frame pointer.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">push        ebp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">mov         ebp,esp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The epilog just reverses what the prolog had done.  It is implented by a LEAVE instruction or is implemented as follows</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">mov         esp,ebp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">pop         ebp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The ret instruction returns from the function to the return address stored in the stack.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">A compiler switch allows one to omit the frame pointer (fpo &#8211; frame pointer omission) which effectively removes the prolog and epilog and uses the ebp register for other optimizations.  It is easy to demarcate function boundaries where the frame pointer is not omitted but one should be aware of the absense of these entry and exit points.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Allocation Of Local Variables</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Local variables are allocated on the stack.  The total size of the local variables is computed at compile time and at runtime those many bytes are reserved on the stack.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">004113A0  push        ebp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">004113A1  mov         ebp,esp</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">004113A3  sub         esp,0E8h</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Note that in the example above, 232 (0xE8) bytes are being reserved for local variables.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The above code will also help in understanding why local variable allocation is much faster than requesting memory from heap.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">Local Variables And Parameters In Assembly</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The most important part in assembly is to be able to identify access to locals and parameters.  The frame pointer that is set in the prolog, acts as the reference pointer using which all variables can be accessed.  If you add to the frame pointer (Remember : P for Plus and P for Parameters), you will access parameters.  If you subtract from the frame pointer, you will be able to access local variables.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">mov         byte ptr [ebp-20h],3</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">mov         byte ptr [ebp+20h],5</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">The first line above access a local variable whereas the second accesses a parameter.</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">In the case of frame pointer omission, everything is calculated with respect to the stack pointer.  Therefore [esp + 20h] might refer to a local or a parameter depending on where the stack pointer currently points to.  And if say a register is pushed on the stack, the same variable will now be referred using [esp + 24h].  Debugging functions that have optimized out the frame pointer is not that easy as the changes made to the stack pointer need to be constantly tracked.</div>
<p>This article is an introduction to understanding Intel&#8217;s assembly language while debugging a computer program.</p>
<p>Debugging in assembly can be a daunting task and not every developer likes to make sense of the mnemonics that appear on the screen.</p>
<p>While debugging, one is normally used to viewing local variables, parameters  and syntax highlighted code in the most user friendly manner.  This is the primary reason why many developers dislike debugging in assembly initially.</p>
<h3>Why Debug In Assembly?</h3>
<p><span style="font-weight: normal; font-size: 13px;">Debugging in assembly is not an optional skill to have.   Every developer encounters a situation where there is no other alternative other than cracking open the assembly code.  Here are a few reasons why a developer should get their hands dirty with this skill -</span></p>
<ol>
<li> You have a binary and its corresponding code but the build environment is not functional.  This could be because of missing tools, unavailable compilers or the lack of understanding of complex scripts and steps needed to recreate the binary.</li>
<li> You are dealing with a release build only bug and the debug symbols are unavailable or you are dealing with a third party library and don&#8217;t have access to its source code.</li>
<li> You like to have fun <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ol>
<h3><strong>What You Need?</strong></h3>
<ol>
<li>A debugger that can display assembly language (Visual Studio, WinDbg, gdb, XCode are capable debuggers).</li>
<li>A ready reference to the Intel instruction set.  Google works just fine.</li>
<li>Patience and maybe a link to this article.  :-)</li>
</ol>
<p>The key to debugging in assembly is to understand how functions are called, parameters are passed, local variables accessed.  The code flow and logic can be understood by looking up the Intel instruction set .</p>
<p>The diagram below (click for larger image)  is roughly how a stack should look like when a function is called.  Mapping the assembly code to this diagram should simplify the debugging experience.  Note that the stack grows from top to bottom.</p>
<p><a style="text-decoration: none;" href="http://www.technochakra.com/wp-content/uploads/assembly_stack.jpg"><img class="alignnone size-medium wp-image-317" title="assembly_stack" src="http://www.technochakra.com/wp-content/uploads/assembly_stack-300x170.jpg" alt="assembly_stack" width="300" height="170" /></a></p>
<h3>Passing Parameters To A Function</h3>
<p>Parameters are the first to be pushed onto the stack.  The caller of the function pushes them from right to left.</p>
<h3>Cleanup Of Parameters</h3>
<p>Parameter cleanup may happen in the function that was called (e.g. stdcall calling convention) or by the caller of the function (e.g. cdecl).  Various kinds of calling conventions of the x86 architecture are explained in detail in<a href="http://en.wikipedia.org/wiki/X86_calling_conventions" target="_blank"> this wiki article</a>.   Stack cleanup code right after the function call gives a good idea of  the calling convention being used.</p>
<p>Take note that if cleanup happens in the function being called, variable number of arguments cannot be passed to it.  In contrast, variable number of arguments can be implemented if the parameter cleanup happens outside the function.  This essentially is the main difference between stdcall and cdelc calling conventions on a x86 architecture.</p>
<h3>The Function Call</h3>
<p>Before control is passed to the function, the return address (where the caller is supposed to resume after the function completes execution) is pushed on the stack.</p>
<p>To recap, parameters are pushed first, followed by the return address.  Later depending on the calling convention of the function, you may see cleanup happening right after the function call or within the function itself.</p>
<p>In the stack diagram above, as the stack grows downwards, the parameters are at the top followed by the return address.</p>
<h3>Example</h3>
<p>Take a look at the snippet and the equivalent assembly below :</p>
<blockquote><p>[1]<span style="white-space:pre"> </span><strong>int a = 4;</strong><br />
[2]<span style="white-space:pre"> </span>mov         dword ptr [a],4<br />
[3]<span style="white-space:pre"> </span><br />
[4]<span style="white-space:pre"> </span><strong>char c = 0;</strong><br />
[5]<span style="white-space:pre"> </span>mov         byte ptr [c],0<br />
[6]<br />
[7]<span style="white-space:pre"> </span><strong>c = f(a, 22);</strong><br />
[8]<span style="white-space:pre"> </span>push        16h<br />
[9]  <span style="white-space:pre"> </span>mov         eax,dword ptr [a]<br />
[10]  push        eax<br />
[11]<span style="white-space:pre"> </span>call        f (4111D1h)<br />
[12]<span style="white-space:pre"> </span>add         esp,8 <span style="white-space:pre"> </span><br />
[13]<span style="white-space:pre"> </span>mov         byte ptr [c],al</p></blockquote>
<ul>
<li>In line [8], the constant value 22 (16 hex) is being pushed on the stack.</li>
<li>In line [9-10] the variable &#8216;a&#8217; is pushed.</li>
<li>Line [11] is the function call to f() which implicitly pushes the return address.</li>
<li>The return value of the function is passed in a register and copied in variable &#8216;c&#8217; (line[13]).</li>
<li>In line [12] one can see that 8 bytes are being discarded from the stack as f() uses the cdecl calling convention and cleanup needs to happen in the caller function.</li>
</ul>
<h3>The Function</h3>
<p>The function usually starts with a prolog and ends with an epilog.</p>
<p>The prolog is either a simple ENTER instruction or more commonly, it saves the ebp register and copies the stack pointer value in it so as to use it as frame pointer.</p>
<blockquote><p>push        ebp<br />
mov         ebp,esp</p></blockquote>
<p>The epilog just reverses what the prolog had done.  It is implented by a LEAVE instruction or is implemented as follows</p>
<blockquote><p>mov         esp,ebp<br />
pop         ebp</p></blockquote>
<p>The ret instruction passes control from the function to the return address stored in the stack.</p>
<p>Compilers provide a switch that allows one to omit the frame pointer (fpo or frame pointer omission) which effectively removes the prolog and epilog and uses the ebp register for other optimizations.  It is easy to demarcate function boundaries where the frame pointer is present but one should be aware that the entry and exit points may be absent.</p>
<h3>Allocation Of Local Variables</h3>
<p>Local variables are allocated on the stack.  The total size of the local variables is computed at compile time and at runtime those many bytes are reserved on the stack.</p>
<blockquote><p>004113A0  push        ebp<br />
004113A1  mov         ebp,esp<br />
004113A3 <strong> sub          esp,0E8h</strong></p></blockquote>
<p>Note that in the example above, 232 (0xE8) bytes are being reserved for local variables.</p>
<p>The above code will also help in understanding why local variable allocation is much faster than requesting memory from heap.   Allocating local variables requires moving the stack pointer whereas memory heap management is much more complex.</p>
<p>As the return address and local variable area are very close to each other, buffer overflows can be caused if data is written past the the local variable area which can then overwrite the the return address.   When this return address if forced to point to shell code (or to some other code placed intentionally by a hacker), such a buffer overflow is termed as an exploit.</p>
<h3>Local Variables And Parameters In Assembly</h3>
<p>The most important part in assembly is to be able to identify access to locals and parameters.  The frame pointer that is set in the prolog, acts as the reference pointer using which all variables can be accessed.  If you add to the frame pointer (Remember : P for Plus and P for Parameters), you will be able to access parameters.  If you subtract from the frame pointer, you will be able to access local variables.</p>
<blockquote><p>mov         byte ptr [ebp-20h],3<br />
mov         byte ptr [ebp+20h],5</p></blockquote>
<p>The first line above accesses a local variable whereas the second accesses a parameter.</p>
<p>In the case of frame pointer omission, everything is calculated with respect to the stack pointer.  Therefore [esp + 20h] might refer to a local or a parameter depending on where the stack pointer currently points to.  And if say a register is pushed on the stack, the same variable will now be referred using [esp + 24h].  Debugging functions that have optimized out the frame pointer is not that easy as the changes made to the stack pointer need to be constantly tracked.</p>
<h3>Conclusion</h3>
<p>Debugging in assembly is not only fun but a useful tool to debug difficult problems.  Different debuggers provide different interfaces to interact with the assembly code but under the hood, all programs work alike.   Understanding this is the key to debugging in assembly with ease.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/assembly-and-the-art-of-debugging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GDB &#8211; Debugging In Assembly</title>
		<link>http://www.technochakra.com/gdb-debugging-in-assembly/</link>
		<comments>http://www.technochakra.com/gdb-debugging-in-assembly/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 17:55:13 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=112</guid>
		<description><![CDATA[If you haven&#8217;t debugged in assembly yet, you haven&#8217;t debugged enough   Debugging assembly in gdb can be tricky at times. A normal debugging session involving assembly language consists of the following steps - Launch the program in gdb and set the required breakpoint(s). When the breakpoint is hit, use the disassemble command to view [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t debugged in assembly yet, you haven&#8217;t debugged enough <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   Debugging assembly in gdb can be tricky at times.</p>
<p>A normal debugging session involving assembly language consists of the following steps -</p>
<ol>
<li>Launch the program in gdb and set the required breakpoint(s).</li>
<li>When the breakpoint is hit, use the <em>disassemble</em> command to view the assembly language of the current frame.</li>
<li>Use the <em>frame</em> command and figure out the program counter and thus your location in the assembly that gdb produced in the last step.</li>
<li>Use the <em>si</em> and <em>ni</em> commands to step in and step over the instructions in assembly language.</li>
</ol>
<p>The problem is that this may require a bit of scrolling to do if the disassembled function is very long.  The display command can be used to solve this problem.</p>
<p>The display command in gdb allows a user to configure the variables that should show up each time the program is suspended.</p>
<blockquote><p><span style="text-decoration: underline;">(gdb)</span> <strong>help display</strong><br />
Print value of expression EXP each time the program stops.<br />
/FMT may be used before EXP as in the &#8220;print&#8221; command.<br />
/FMT &#8220;i&#8221; or &#8220;s&#8221; or including a size-letter is allowed,<br />
as in the &#8220;x&#8221; command, and then EXP is used to get the address to examine<br />
and examining is done as in the &#8220;x&#8221; command.</p>
<p>With no argument, display all currently requested auto-display expressions.<br />
Use &#8220;undisplay&#8221; to cancel display requests previously made.</p></blockquote>
<p>One can use the display command to view the next instruction that is about to be executed.  The pointer to the instruction yet to be executed is stored in the computer&#8217;s program counter (also known as the instruction pointer).   The following command will show the next 3 instructions that are pointed to by the program counter.</p>
<blockquote><p>display /3i $pc</p></blockquote>
<p>The /3i above causes three instructions to be printed and $pc references the program counter register.  A sample output is shown below.</p>
<blockquote><p>1: x/3i $pc<br />
0&#215;401175 &lt;main+37&gt;:     call   0x4103f0 &lt;__main&gt;<br />
0x40117a &lt;main+42&gt;:     lea    0xffffffe8(%ebp),%eax<br />
0x40117d &lt;main+45&gt;:     mov    %eax,(%esp)<br />
<span style="text-decoration: underline;">(gdb)</span></p></blockquote>
<p>
<DIV ALIGN=CENTER><br />
<iframe src="http://rcm.amazon.com/e/cm?t=technochakra-20&#038;o=1&#038;p=6&#038;l=st1&#038;mode=books&#038;search=debugging%20%20assembly&#038;fc1=000000&#038;lt1=&#038;lc1=3366FF&#038;bg1=FFFFFF&#038;f=ifr" marginwidth="0" marginheight="0" width="120" height="150" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe><br />
</DIV></p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/gdb-debugging-in-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
