<?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>Orange Juice Liberation Front &#187; assembler</title>
	<atom:link href="http://orangejuiceliberationfront.com/tag/assembler/feed/" rel="self" type="application/rss+xml" />
	<link>http://orangejuiceliberationfront.com</link>
	<description>Uli&#039;s citrussy fresh thoughts on technical stuff</description>
	<lastBuildDate>Mon, 13 May 2013 20:59:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Funny thing about C parameter evaluation order&#8230;</title>
		<link>http://orangejuiceliberationfront.com/funny-thing-about-c-parameter-evaluation-order/</link>
		<comments>http://orangejuiceliberationfront.com/funny-thing-about-c-parameter-evaluation-order/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 10:37:05 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[dangerous]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[param]]></category>
		<category><![CDATA[params]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[warning]]></category>
		<category><![CDATA[wrong order]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=57</guid>
		<description><![CDATA[I just explained this to a friend today, and thought this might make an interesting blog posting: #include &#60;stdio.h&#62;int main( int argc, const char * argv[] ) { char theText[2] = { 'A', 'B' }; char* myString = theText; printf( "%c, %c\n", *(++myString), *myString ); return 0; } The above code is platform-dependent in C. &#8230;  <a href="http://orangejuiceliberationfront.com/funny-thing-about-c-parameter-evaluation-order/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p>I just explained this to a friend today, and thought this might make an interesting blog posting:</p>
<pre>#include &lt;stdio.h&gt;<p>int main( int argc, const char * argv[] )
{
	char	theText[2] = { 'A', 'B' };
	char*	myString = theText;
	printf( "%c, %c\n", *(++myString), *myString );</p><p><span style="white-space: pre;">	</span>return 0;
}</p></pre>
<p>The above code is platform-dependent in C. Yes, you read correctly: <em>platform dependent</em>. And I&#8217;m not nitpicking that this may cause a problem if your compiler is old or that some compiler may not have <tt>printf()</tt> or the POSIX standard.</p>
<p>This code is platform-dependent, because the C standard says that there is no guarantee in which order the parameters of a function call get evaluated. So, if you run the above code, it could print <tt>B, B</tt> (which most of you probably expected because it corresponds to our left-to-right reading order) or it could print <tt>B, A</tt>.</p>
<p>If you want to test this and you own an Intel Mac, you can do the following thanks to Rosetta&#8217;s PowerPC emulation: Create a new &#8220;Standard Tool&#8221; project in Xcode and paste the above code into the <tt>main.c</tt> file. Switch to &#8220;Release&#8221; and change &#8220;Architectures&#8221; in the build settings for the release build configuration to be &#8220;ppc&#8221;. Build and Run. It&#8217;ll print <tt>B, B</tt>. Now change the architecture to &#8220;i386&#8243; and build and run again. It&#8217;ll print <tt>B, A</tt>.</p>
<p>So, why doesn&#8217;t C define an order? Why did anyone think such odd behaviour was a good idea? Well, to explain that, we&#8217;ll have to look at what your computer does under the hood to execute a function call. In general, there are two steps: First, the parameters are evaluated and stored in some standardized place where the called function can find them, and then the processor &#8220;jumps&#8221; to the first command in the new function and starts executing it.</p>
<p>Some CPUs have <em>registers</em> inside the CPU, which are little variables that can hold short values, and which can be accessed a lot quicker than actually going over to a RAM chip and fetching a value. There are different registers for different kinds of values. Many CPUs have separate registers for floating-point numbers and integers. And just like with RAM, it&#8217;s sometimes faster to access these registers in a certain order.</p>
<p>So, it may be faster to first evaluate all integer-value parameters, and then those that contain floating-point values. Depending on what physical CPU your computer has (or in the case of Rosetta, what characteristics the emulated CPU your code is being run on has), these performance characteristics may be different. Some CPUs may have so few registers that the parameters will always have to be passed in RAM. Others may put larger parameters in RAM and smaller ones in registers, others again may put the first couple parameters in registers (maybe even distributing a longer parameter across several registers), and the rest that don&#8217;t fit in RAM, etc.</p>
<p>So, to make sure C can be made to run that little bit faster on any of these CPUs, its designers decided not to enforce an order for execution of parameters. And that&#8217;s one of the dangers of writing code in C++ or Objective C: It may look like a high-level language, but underneath it is still a portable assembler, with platform-dependencies like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/funny-thing-about-c-parameter-evaluation-order/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Generating Machine Code at Runtime</title>
		<link>http://orangejuiceliberationfront.com/generating-machine-code-at-runtime/</link>
		<comments>http://orangejuiceliberationfront.com/generating-machine-code-at-runtime/#comments</comments>
		<pubDate>Tue, 01 May 2007 11:53:18 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Assembler]]></category>
		<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[dynamic code]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[i686]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[just in time compiler]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[machine language]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=104</guid>
		<description><![CDATA[Okay, so my next attempt at learning how my computer works and how to speak machine language is the following C code fragment: typedef int (*FuncPtr)(); // Create a function: char testFunc[] = { 0x90, // NOP (not really necessary...) 0xB8, 0x10, 0x00, 0x00, 0x00, // MOVL $16,%eax 0xC3 }; // RET // Make a &#8230;  <a href="http://orangejuiceliberationfront.com/generating-machine-code-at-runtime/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p>Okay, so my next attempt at learning how my computer works and how to speak machine language is the following C code fragment:</p>
<pre>typedef int (*FuncPtr)();

// Create a function:
char            testFunc[] = { 0x90,                         // NOP (not really necessary...)
                               0xB8, 0x10, 0x00, 0x00, 0x00, // MOVL $16,%eax
                               0xC3 };                       // RET

// Make a copy on the heap, OS doesn't like executing the stack:
FuncPtr         testFuncPtr = (FuncPtr) malloc(7);
memmove( (void*) testFuncPtr, testFunc, 7 );

printf("Before function.\n");
int result = (*testFuncPtr)();
printf("Result %d\n", result);</pre>
<p>Basically, this stores the raw opcodes of a function in an array of chars. The first byte of each line is usually the opcode, i.e. <tt>0x90</tt> is No-Op, <tt>0xB8</tt> is a MOVL into the <tt>eax</tt> register (with the next 4 bytes being the number to store, in this case 16), and <tt>0xC3</tt> is the return instruction (I had to look up the opcodes in Intel&#8217;s documentation).</p>
<p>One thing to watch out for here (at least on Mac OS X), is that you&#8217;ll get a bad access error if you try to execute testFunc directly. That&#8217;s because testFunc is on the stack, and the stack shouldn&#8217;t contain executable code (it&#8217;s a small safety measure). So, what we do is we simply malloc some memory on the heap, and stuff our code in there.</p>
<p>You may wonder why I&#8217;m using <tt>eax</tt> of all registers to store my number 16 in. Easy: Because the convention is that an int return value (and most other 4-byte return values) goes in <tt>eax</tt> when a function returns. So, what this does is it essentially returns 16. Which our printf() proves. Neat!</p>
<p>Intel&#8217;s documentation describes the opcodes in a very complicated way, so what I essentially do is I write some assembler code and enclose the instruction whose byte sequence I want to find out in instructions whose byte sequence I already know (I like to use six <tt>nop</tt>s, which are short and show up as <tt>0x90 90 90 90 90 90</tt>). Then I compile that, and then use a <a href="http://ridiculousfish.com/hexfiend/">hex editor</a> to search for the known instructions, and whatever is between them must be my new one. Here&#8217;s a small table of other operations you may find in the typical program and what byte sequences they turn to:</p>
<table border="1" cellspacing="0" cellpadding="8">
<tbody>
<tr>
<td valign="top">0&#215;50</td>
<td valign="top">pushl %eax</td>
</tr>
<tr>
<td valign="top">0&#215;53</td>
<td valign="top">pushl %ebx</td>
</tr>
<tr>
<td valign="top">0&#215;55</td>
<td valign="top">pushl %ebp</td>
</tr>
<tr>
<td valign="top">0&#215;89 E5</td>
<td valign="top">movl %esp, %ebp</td>
</tr>
<tr>
<td valign="top">0&#215;90</td>
<td valign="top">nop</td>
</tr>
<tr>
<td valign="top">0xB8 <em>NN NN NN NN</em></td>
<td valign="top">movl $<em>N</em>, %eax</td>
</tr>
<tr>
<td valign="top">0&#215;68 <em>NN NN NN NN</em></td>
<td valign="top">pushl $<em>N</em></td>
</tr>
<tr>
<td valign="top">0xE8 <em>NN NN NN NN</em></td>
<td valign="top">call <em>relativeOffsetNFromEndOfInstruction</em></td>
</tr>
<tr>
<td valign="top">0x8B 1C 24</td>
<td valign="top">movl (%esp), %ebx</td>
</tr>
<tr>
<td valign="top">0x8D 83 <em>NN NN NN NN</em></td>
<td valign="top">leal <em>relativeOffsetToData</em>(%ebx), %eax</td>
</tr>
<tr>
<td valign="top">0x8D 85 <em>NN NN NN NN</em></td>
<td valign="top">leal <em>relativeOffsetToData</em>(%ebp), %eax</td>
</tr>
<tr>
<td valign="top">0x5B</td>
<td valign="top">popl %ebx</td>
</tr>
<tr>
<td valign="top">0&#215;83 C4 <em>NN</em></td>
<td valign="top">addl $NN,%esp</td>
</tr>
<tr>
<td valign="top">0&#215;83 EC <em>NN</em></td>
<td valign="top">subl $NN,%esp</td>
</tr>
<tr>
<td valign="top">0x8B 00</td>
<td valign="top">movl (%eax), %eax</td>
</tr>
<tr>
<td valign="top">0&#215;89 45 <em>NN</em></td>
<td valign="top">movl %eax, NN(%ebp)</td>
</tr>
<tr>
<td valign="top">0xC9</td>
<td valign="top">leave</td>
</tr>
<tr>
<td valign="top">0xC3</td>
<td valign="top">ret</td>
</tr>
</tbody>
</table>
<p>The code fragment above is essentially what one would need to create a just-in-time compiler. For a real compiler, instead of executing this directly, we&#8217;d have to write it to a complete MachO file and link it with crt1.o.</p>
<p><strong>Update:</strong> on top of the instructions for <a href="http://orangejuiceliberationfront.com/intel-assembler-on-mac-os-x/">position-independent code</a> (PIC), I&#8217;ve also added some more useful in passing structs as parameters on the stack.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/generating-machine-code-at-runtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nice Intel assembler text&#8230;</title>
		<link>http://orangejuiceliberationfront.com/nice-intel-assembler-text/</link>
		<comments>http://orangejuiceliberationfront.com/nice-intel-assembler-text/#comments</comments>
		<pubDate>Sat, 04 Nov 2006 14:51:49 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Assembler]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[i686]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[machine language]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=223</guid>
		<description><![CDATA[I&#8217;ve recently been looking into assembler coding a little. I learned assembler theory back in High School in Mr. Trapp&#8217;s computer programming elective, and later learned a bit of 68000 assembler as well, but never got round to actually getting into it when the PPC arrived on the scene. So, when I recently heard at &#8230;  <a href="http://orangejuiceliberationfront.com/nice-intel-assembler-text/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[two of Intel's instruction set manuals]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/11/IMG_0372.jpg" border="0" alt="[two of Intel's instruction set manuals]" width="480" /></p>
<p>I&#8217;ve recently been looking into assembler coding a little. I learned assembler theory back in High School in Mr. Trapp&#8217;s computer programming elective, and later learned a bit of 68000 assembler as well, but never got round to actually getting into it when the PPC arrived on the scene. So, when I recently heard at work how one can get a whole bunch of Intel reference books for free, I thought this might be a good opportunity to learn x86 assembler. After all, I&#8217;m a parser and compiler geek, it&#8217;s kind of a gap in my skill set if I can&#8217;t do the backend.</p>
<p>Now, trouble is, while there are many tutorials for Linux and Windows, I couldn&#8217;t find a single one for Mac OS X. So, I started googling, assembling C code and bothering some developers I know and others on mailing lists with my questions, and I thought I&#8217;d share my first findings:</p>
<ul>
<li>I got a link to <a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html">Apple&#8217;s Mac OS X ABI docs</a>. This is really good, as it documents an important part on OS X in detail: How to align the stack (on 16 bytes, no matter what Intel&#8217;s docs tell you), and how to call your own functions.</li>
<li>Aforementioned 16-byte stack alignment is not always necessary, but when you call a function, you must give it a properly aligned stack. When you are <em>called</em>, however, the stack will have the return address on it, which is 4 bytes. So, after you push the base pointer on the stack (4 more bytes), you have to move the stack pointer by another 8 bytes at least to make it aligned on a 16-byte boundary again.</li>
<li>A nice way to learn assembler is by writing very simple C programs and using <tt>gcc -S my_simple_c_program.c</tt> to get it translated into assembler code. Note that by simple, I recommend you start out with stuff that doesn&#8217;t use any system functions, because those are dynamically linked and make for rather complex assembler.</li>
<li>To compile such a program, simply pass it to GCC again, as you would with a C source file. E.g. <tt>gcc my_simple_c_program.s -o my_simple_c_program</tt></li>
</ul>
<p>This might be a good point to mention my <a href="http://masters-of-the-void.com/book5.htm" target="_blank">Memory Management chapter</a> in the <a href="http://masters-of-the-void.com/" target="_blank">Masters of the Void C tutorial</a> again, which illustrates how memory works. As I learn more, I may post supplements to that that slowly teach you assembler. Well, I&#8217;m not promising anything, but I&#8217;d love to do that.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/nice-intel-assembler-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: orangejuiceliberationfront.com @ 2013-05-19 14:35:51 by W3 Total Cache -->