<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx</link><description>Hello ntdebuggers! We’ve seen a lot of interest in our Puzzlers , and we’ve also seen requests and interest in topics covering debugging fundamentals . So we’ve decided to combine the two topics and post a series of “Fundamentals Exercises”. These exercises</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067038</link><pubDate>Thu, 13 Nov 2008 22:01:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067038</guid><dc:creator>Niels Thrane</dc:creator><description>&lt;p&gt;I'm betting on the factorial function .&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067456</link><pubDate>Fri, 14 Nov 2008 00:03:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067456</guid><dc:creator>jean</dc:creator><description>&lt;p&gt;DoTheWork performs the factorial operation&lt;/p&gt;
&lt;p&gt;the stack says we're in DoTheWork(7) so the return value is 7! = 5040&lt;/p&gt;
&lt;p&gt;nice puzzlers, keep it up !&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067488</link><pubDate>Fri, 14 Nov 2008 00:15:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067488</guid><dc:creator>pingpong</dc:creator><description>&lt;p&gt;DoTheWork is the factorial function; as such it'll return 7!.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067568</link><pubDate>Fri, 14 Nov 2008 00:48:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067568</guid><dc:creator>Atul</dc:creator><description>&lt;p&gt;[ebp] -&amp;gt; Old Ebp == 0012ff88&lt;/p&gt;
&lt;p&gt;[ebp+4] -&amp;gt; return address == 004012b2 &lt;/p&gt;
&lt;p&gt;[ebp+8] -&amp;gt; Param 1 == 2&lt;/p&gt;
&lt;p&gt;In this case:&lt;/p&gt;
&lt;p&gt;2 * (2 - 1) = 2&lt;/p&gt;
&lt;p&gt;int DoTheWork(int n) {&lt;/p&gt;
&lt;p&gt;int i = n;&lt;/p&gt;
&lt;p&gt;do {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;n--;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;i = i * n;&lt;/p&gt;
&lt;p&gt;} while (n &amp;gt; 2);&lt;/p&gt;
&lt;p&gt;return (i);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;BTW, why not make this a fastcall :)? &lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067817</link><pubDate>Fri, 14 Nov 2008 02:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067817</guid><dc:creator>ibganev</dc:creator><description>&lt;p&gt;(1) At function exit, EAX = 0x13b0&lt;/p&gt;
&lt;p&gt;(2) The function computes the factorial of its argument.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067821</link><pubDate>Fri, 14 Nov 2008 02:09:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067821</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Looks like factorial, assuming the input is positive (doesn't handle 0!)... result will be 7! = 5040.&lt;/p&gt;
&lt;p&gt;&amp;lt;code&amp;gt;&lt;/p&gt;
&lt;p&gt;int DoTheWork(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	int r = x;&lt;/p&gt;
&lt;p&gt;	do {&lt;/p&gt;
&lt;p&gt;		r *= --x;&lt;/p&gt;
&lt;p&gt;	} while (x &amp;gt; 2);&lt;/p&gt;
&lt;p&gt;	return r;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;lt;/code&amp;gt;&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9067874</link><pubDate>Fri, 14 Nov 2008 02:39:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067874</guid><dc:creator>Benoit</dc:creator><description>&lt;p&gt;1) Return value should be 5040 (in decimal)&lt;/p&gt;
&lt;p&gt;2) Looks like the !n function called with n=7&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9068420</link><pubDate>Fri, 14 Nov 2008 07:11:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9068420</guid><dc:creator>doug</dc:creator><description>&lt;p&gt;Doh! didn't look at esp&lt;/p&gt;
&lt;p&gt;1.) 7!&lt;/p&gt;
&lt;p&gt;2.) Factorial&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9068669</link><pubDate>Fri, 14 Nov 2008 10:32:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9068669</guid><dc:creator>Aaron Ballman</dc:creator><description>&lt;p&gt;I'm going to refrain from posting my answer since I don't want to spoil the challenge for anyone. &amp;nbsp;But this was a really fun post (to me, at least), so thanks for it!&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9068677</link><pubDate>Fri, 14 Nov 2008 10:37:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9068677</guid><dc:creator>zproxy</dc:creator><description>&lt;p&gt;This might be completly wrong but off the bat I would translate it to something like this:&lt;/p&gt;
&lt;p&gt;void* DoTheWork(void* e, int p)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;do&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;p = ((--p) * p;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;while (p != 2);&lt;/p&gt;
&lt;p&gt;return e;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;How close did i got it? :=)&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9069077</link><pubDate>Fri, 14 Nov 2008 13:19:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9069077</guid><dc:creator>msuiche</dc:creator><description>&lt;p&gt;I guess this is this:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://en.wikipedia.org/wiki/Factorial"&gt;http://en.wikipedia.org/wiki/Factorial&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;int DoTheWork(int p1)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;int eax;&lt;/p&gt;
&lt;p&gt;int ecx;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ecx = p1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;eax = p1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;do&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ecx -= 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eax *= ecx;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} while (ecx &amp;gt; 2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return eax;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;0012feb4 &amp;nbsp;00000002&lt;/p&gt;
&lt;p&gt;Here is the argument you used.&lt;/p&gt;
&lt;p&gt;The function returns 2.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9069147</link><pubDate>Fri, 14 Nov 2008 13:58:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9069147</guid><dc:creator>George</dc:creator><description>&lt;p&gt;It calculates the number 7 factorial. The return value will be 7! = 5040.&lt;/p&gt;
&lt;p&gt;-George &lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9069193</link><pubDate>Fri, 14 Nov 2008 14:27:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9069193</guid><dc:creator>Rasmus B</dc:creator><description>&lt;p&gt;Great idea with an exercise!&lt;/p&gt;
&lt;p&gt;The function calculates the factorial of the argument. The result in the example is 7!.&lt;/p&gt;
&lt;p&gt;int fact(int arg1) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int retval = arg1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;do {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;retval *= --arg1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} while (arg1 &amp;gt; 2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return retval;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9069918</link><pubDate>Fri, 14 Nov 2008 16:12:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9069918</guid><dc:creator>didi1605</dc:creator><description>&lt;p&gt;I assume the result is 5040. About the function I am not quiet sure.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9069972</link><pubDate>Fri, 14 Nov 2008 16:17:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9069972</guid><dc:creator>didi1605</dc:creator><description>&lt;p&gt;I assume the result is 5040. About the math function I am not sure.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9070273</link><pubDate>Fri, 14 Nov 2008 16:36:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9070273</guid><dc:creator>dmitri</dc:creator><description>&lt;p&gt;&amp;lt;pre&amp;gt;&lt;/p&gt;
&lt;p&gt;#define DoTheWork factorial&lt;/p&gt;
&lt;p&gt;int DoTheWork(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	int y=x-1;&lt;/p&gt;
&lt;p&gt;	for(;y&amp;gt;=2;--y) x*=y;&lt;/p&gt;
&lt;p&gt;	return x;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;DoTheWork(7)=5040&lt;/p&gt;
&lt;p&gt;&amp;lt;/pre&amp;gt;&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9070624</link><pubDate>Fri, 14 Nov 2008 16:52:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9070624</guid><dc:creator>tastewar</dc:creator><description>&lt;p&gt;Looks like you're calculating 7! which would be 5040.&lt;/p&gt;</description></item><item><title>Answer</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9074817</link><pubDate>Fri, 14 Nov 2008 20:24:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9074817</guid><dc:creator>Moshe Levi</dc:creator><description>&lt;p&gt;The return value from this current execution is 5040 which is the factorial of 7.&lt;/p&gt;</description></item><item><title>1) 7! = 5040 2) factorial</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9075324</link><pubDate>Fri, 14 Nov 2008 21:12:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9075324</guid><dc:creator>m</dc:creator><description>&lt;p&gt;1. &amp;nbsp; &amp;nbsp; &amp;nbsp; When the function “DoTheWork” returns, what is the return value from that function?&lt;/p&gt;
&lt;p&gt;5040&lt;/p&gt;
&lt;p&gt;2. &amp;nbsp; &amp;nbsp; &amp;nbsp; Bonus: what is the mathematical operation that “DoTheWork” performs?&lt;/p&gt;
&lt;p&gt;factorial&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9078273</link><pubDate>Fri, 14 Nov 2008 23:48:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9078273</guid><dc:creator>nch</dc:creator><description>&lt;p&gt;Here the answer.&lt;/p&gt;
&lt;p&gt;int DoWork(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	int y = x;&lt;/p&gt;
&lt;p&gt;	do&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		y *= --x;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;while(x &amp;gt; 2);&lt;/p&gt;
&lt;p&gt;	return y;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;quot;DoWork&amp;quot; calculates factorial.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9078348</link><pubDate>Fri, 14 Nov 2008 23:50:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9078348</guid><dc:creator>nch</dc:creator><description>&lt;p&gt;Hello, here is the answer:&lt;/p&gt;
&lt;p&gt;int DoWork(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	int y = x;&lt;/p&gt;
&lt;p&gt;	do&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		y *= --x;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(x &amp;gt; 2);&lt;/p&gt;
&lt;p&gt;	return y;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Function calculates factorial for its argument.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9078759</link><pubDate>Sat, 15 Nov 2008 00:12:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9078759</guid><dc:creator>Alex Ionescu</dc:creator><description>&lt;p&gt;1) It returns 7!, ie 5040&lt;/p&gt;
&lt;p&gt;2) It computes the factorial of the input number&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9079878</link><pubDate>Sat, 15 Nov 2008 01:01:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9079878</guid><dc:creator>zmx</dc:creator><description>&lt;p&gt;I am new to assembly debugging, but let me try this.&lt;/p&gt;
&lt;p&gt;It seem this code is calculating the factorial of a number and then store the result in EAX. &lt;/p&gt;
&lt;p&gt;The code decrements ECX for each loop until it becomes 2. and each time the value in ECX will be multiplied into EAX.&lt;/p&gt;
&lt;p&gt;Here is the part that confuses me. &lt;/p&gt;
&lt;p&gt;The initial value of EAX is from ECX and ECX got its value from the memory location pointed by ebp+8. &lt;/p&gt;
&lt;p&gt;and ebp got its inital value from esp which is probably &amp;quot;0012fe9c&amp;quot;. So 0012fe9c+8 = 0012fea4.&lt;/p&gt;
&lt;p&gt;so [0012fea4] is &amp;quot;82059a87&amp;quot;. but this seems a very large number already.&lt;/p&gt;
&lt;p&gt;so the result in EAX will be the factorial of &amp;quot;82059a87&amp;quot;?&lt;/p&gt;
&lt;p&gt;I could be completely wrong here. Just a wild guess.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9082270</link><pubDate>Sat, 15 Nov 2008 03:34:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9082270</guid><dc:creator>David Moisan</dc:creator><description>&lt;p&gt;It's a factorial function. &amp;nbsp;The original value is 7 and the result (7!) is 0x13B0 or 5040 decimal.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9082884</link><pubDate>Sat, 15 Nov 2008 04:03:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9082884</guid><dc:creator>zmx</dc:creator><description>&lt;p&gt;this seems to be computing the factorial of a number, though I am still confused what the end result is, because I have a hard time finding the initial value of ECX...&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9082903</link><pubDate>Sat, 15 Nov 2008 04:04:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9082903</guid><dc:creator>Nate Jhaveri</dc:creator><description>&lt;p&gt;Looks like a factorial function, in this case it should return 7!, or 5040.&lt;/p&gt;
&lt;p&gt;The original code probably looks something like this:&lt;/p&gt;
&lt;p&gt;DoTheWork(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int y = x;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;do &lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;x--;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;y = y * x;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;} &lt;/p&gt;
&lt;p&gt; &amp;nbsp;while (x &amp;gt; 2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return y;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9082979</link><pubDate>Sat, 15 Nov 2008 04:07:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9082979</guid><dc:creator>calin_iaru</dc:creator><description>&lt;p&gt;This is factorial(7) - it will return 5040.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9085470</link><pubDate>Sat, 15 Nov 2008 06:40:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9085470</guid><dc:creator>sundr</dc:creator><description>&lt;p&gt;/// Calculates factorial&lt;/p&gt;
&lt;p&gt;int DoTheWork(int n) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int result = n;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;do {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;result *= --n;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;while(n&amp;gt;2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return result;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;// Just curious - was the code from puzzle compiled from C++ or hand-written in assembly?&lt;/p&gt;
&lt;p&gt;Could not get exactly the same assembly from my msvc 2008 - Release build omits frame creation, while Debug build saves too much regs and 0xCC-fills frame.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9095551</link><pubDate>Sat, 15 Nov 2008 14:38:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9095551</guid><dc:creator>Pharaon</dc:creator><description>&lt;p&gt;1) Looks like it is calculating factorial of the number passed as parameter&lt;/p&gt;
&lt;p&gt;2) Return value is 2&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9099516</link><pubDate>Sat, 15 Nov 2008 18:51:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9099516</guid><dc:creator>daniel j aguiar</dc:creator><description>&lt;p&gt;I recently started my studies on debugging. So I can be completely wrong.&lt;/p&gt;
&lt;p&gt;This function seems to be a factorial:&lt;/p&gt;
&lt;p&gt;Mainly when looking at 00401024 until 00401028 where the function, decrement ecx until it becomes 2, and at each DEC, it multiply EAX * ECX&lt;/p&gt;
&lt;p&gt;But I am really unable to find the final value because the ECX receive [ebp+8] at 40101f and [ebp+8] seems to be 82059a87.&lt;/p&gt;
&lt;p&gt;EBP = 12fe9c &lt;/p&gt;
&lt;p&gt;EBP+8 = 12FEA4&lt;/p&gt;
&lt;p&gt;12fea4 &amp;nbsp;= 82059a87 (????)&lt;/p&gt;
&lt;p&gt;So... I was unable to find factorial for 82059a87...&lt;/p&gt;
&lt;p&gt;Maybe I get something wrong...&lt;/p&gt;
&lt;p&gt;Thanks for the opportunity&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9102216</link><pubDate>Sun, 16 Nov 2008 00:04:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9102216</guid><dc:creator>edgar</dc:creator><description>&lt;p&gt;1. 5040&lt;/p&gt;
&lt;p&gt;2. factorial&lt;/p&gt;
&lt;p&gt;Please, next time a bit more complicated. ;)&lt;/p&gt;</description></item><item><title>My attempt...</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9102343</link><pubDate>Sun, 16 Nov 2008 02:17:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9102343</guid><dc:creator>Michael Becker</dc:creator><description>&lt;p&gt;&amp;lt;i&amp;gt;Here's hoping that this looks like it did in plain text...&amp;lt;/i&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;pre&amp;gt;&lt;/p&gt;
&lt;p&gt;My Answers:&lt;/p&gt;
&lt;p&gt;1. &amp;nbsp; &amp;nbsp; &amp;nbsp;5040 (7!)&lt;/p&gt;
&lt;p&gt;2. &amp;nbsp; &amp;nbsp; &amp;nbsp;Factorial N &amp;nbsp;(N!)&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Detailed explanation(s)&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Function: &amp;nbsp; demo2!DoTheWork: &amp;nbsp; &amp;nbsp;(In assembly)&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Save the Prior Frame Pointer to the stack&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0040101c 55 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;push &amp;nbsp; &amp;nbsp;ebp&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Set the Frame pointer to the current Stack pointer&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0040101d 8bec &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mov &amp;nbsp; &amp;nbsp; ebp,esp&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Right at this point, the stack looks like:&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EBP = ESP&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EPB - N -- Local variables, if any (here there aren't)&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EBP &amp;nbsp; &amp;nbsp; -- Old EBP&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EBP + 4 -- Return Address back to calling function&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EBP + 8 -- First function Arg&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Put Arg1 into ECX&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0040101f 8b4d08 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mov &amp;nbsp; &amp;nbsp; ecx,dword ptr [ebp+8]&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Copy ECX into EAX&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;00401022 8bc1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mov &amp;nbsp; &amp;nbsp; eax,ecx&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;LOOP: &amp;nbsp; ECX--&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;00401024 49 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dec &amp;nbsp; &amp;nbsp; ecx&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;EAX = EAX * ECX&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;00401025 0fafc1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;imul &amp;nbsp; &amp;nbsp;eax,ecx&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;If EXC is greater than 2, goto LOOP:&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;00401028 83f902 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cmp &amp;nbsp; &amp;nbsp; ecx,2&lt;/p&gt;
&lt;p&gt;0040102b 7ff7 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;jg &amp;nbsp; &amp;nbsp; &amp;nbsp;demo2!DoTheWork+0x8 (00401024)&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Else it wasn't, so replace the Old Frame Pointer&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0040102d 5d &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;pop &amp;nbsp; &amp;nbsp; ebp&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Return back to the calling function.&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Whatever is in EAX is effectively returned.&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0040102e c3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ret&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Function: &amp;nbsp; demo2!DoTheWork: (In C)&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;int DoTheWork(int Number)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int WorkingValue = Number;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int Factorial &amp;nbsp; &amp;nbsp;= WorkingValue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;do {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WorkingValue--;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Factorial *= WorkingValue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}while (WorkingValue &amp;gt; 2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return(Factorial);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// &amp;nbsp;Since we know what the function does, all we need to do is find the &lt;/p&gt;
&lt;p&gt;// &amp;nbsp;argument to it. &amp;nbsp;We can just look at the stack...&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;0012fe9c &amp;nbsp;00406717 demo2!main+0x27 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Return address for DoTheWork&lt;/p&gt;
&lt;p&gt;0012fea0 &amp;nbsp;00000007 &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;// Arg1 &amp;quot;7&amp;quot;&lt;/p&gt;
&lt;p&gt;0012fea4 &amp;nbsp;82059a87&lt;/p&gt;
&lt;p&gt;&amp;lt;/pre&amp;gt;&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9102985</link><pubDate>Sun, 16 Nov 2008 14:37:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9102985</guid><dc:creator>strik</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;a source code which produces almost directly the same code is as follows:&lt;/p&gt;
&lt;p&gt;int DoThework(int value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; int c = value;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; do {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; value = value * --c;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; } while (c &amp;gt; 2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return value;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Thus, this function multiplies value * value - 1 * value - 2 * ... * 2, and then ends.&lt;/p&gt;
&lt;p&gt;Thus, for positive values (value &amp;gt; 0), this is the faculty (value!) function.&lt;/p&gt;
&lt;p&gt;For value &amp;lt;= 0, the function calculates value * (value-1), as the loop will be executed exactly once.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9109272</link><pubDate>Mon, 17 Nov 2008 14:15:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9109272</guid><dc:creator>Lucian Bargaoanu</dc:creator><description>&lt;p&gt;I think the result is Factorial(2).&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9111029</link><pubDate>Mon, 17 Nov 2008 19:10:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9111029</guid><dc:creator>Madhur</dc:creator><description>&lt;p&gt;Well it seems to be calculating factorial of&lt;/p&gt;
&lt;p&gt;82059a87 .. Am I right :) ?&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9112128</link><pubDate>Mon, 17 Nov 2008 22:14:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9112128</guid><dc:creator>Puneet Saraswat</dc:creator><description>&lt;p&gt;Very good exercise.&lt;/p&gt;
&lt;p&gt;The return value will be 0x2. And the function is for calculating factorial of a number.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9125393</link><pubDate>Wed, 19 Nov 2008 21:38:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9125393</guid><dc:creator>kirchner</dc:creator><description>&lt;p&gt;I believe returned value will be 5040, and the code implements a factorial function.&lt;/p&gt;
&lt;p&gt;I recognized the function as factorial, but when I was about to calculate the return value I first thought that dword ptr [ebp+8] means&lt;/p&gt;
&lt;p&gt;0012fea4 &amp;nbsp;82059a87&lt;/p&gt;
&lt;p&gt;Then I realized that &lt;/p&gt;
&lt;p&gt;0040101c 55 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;push &amp;nbsp; &amp;nbsp;ebp&lt;/p&gt;
&lt;p&gt;has not yet been executed, and it will but another 4bytes on stack.&lt;/p&gt;
&lt;p&gt;Anyway, I'm not a low-level debugging expert, but I enjoy these a lot.&lt;/p&gt;
&lt;p&gt;Thanks for the exercise. Hope there will be a post with the full explanation later on.&lt;/p&gt;</description></item><item><title>re: Debug Fundamentals Exercise 1: Reverse engineer a function</title><link>http://blogs.msdn.com/ntdebugging/archive/2008/11/13/debug-fundamentals-exercise-1-reverse-engineer-a-function.aspx#9125928</link><pubDate>Wed, 19 Nov 2008 23:56:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9125928</guid><dc:creator>André Werlang</dc:creator><description>&lt;p&gt;1. eax holds 5040.&lt;/p&gt;
&lt;p&gt;2. Factorial!&lt;/p&gt;</description></item></channel></rss>