<?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; mac os x</title>
	<atom:link href="http://orangejuiceliberationfront.com/tag/mac-os-x/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>How to become a games programmer on the Mac</title>
		<link>http://orangejuiceliberationfront.com/how-to-become-a-games-programmer-on-the-mac/</link>
		<comments>http://orangejuiceliberationfront.com/how-to-become-a-games-programmer-on-the-mac/#comments</comments>
		<pubDate>Sun, 16 Dec 2007 13:08:26 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[advice]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[macintosh]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=114</guid>
		<description><![CDATA[From the &#8220;when you don&#8217;t have time to blog, drag out an old e-mail and post your side of it&#8221;-department: &#160; How do I find beginners&#8217; tutorials for Mac game programming? In general, for Mac programming you&#8217;ll always find great resources on Scott Stevenson&#8217;s CocoaDevCentral and CocoaBlogs sites. There&#8217;s also the two Mac programming Wikis &#8230;  <a href="http://orangejuiceliberationfront.com/how-to-become-a-games-programmer-on-the-mac/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p><em>From the &#8220;when you don&#8217;t have time to blog, drag out an old e-mail and post your side of it&#8221;-department:</em></p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[A simple computer game called Sterntaler]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/11/Screen-shot-2010-11-28-at-19.03.06.png" border="0" alt="[A simple computer game called Sterntaler]" width="480" /><br/>&nbsp;</p>
<h3>How do I find beginners&#8217; tutorials for Mac game programming?</h3>
<p>In general, for Mac programming you&#8217;ll always find great resources on Scott Stevenson&#8217;s <a href="http://cocoadevcentral.com" target="_blank">CocoaDevCentral</a> and <a href="http://cocoablogs.com" target="_blank">CocoaBlogs</a> sites. There&#8217;s also the two Mac programming Wikis <a href="http://cocoadev.com" target="_blank">CocoaDev</a> and <a href="http://carbondev.com" target="_blank">CarbonDev</a>.</p>
<p>For games programming, <a href="http://idevgames.com" target="_blank">iDevGames</a> is supposedly very popular. If you&#8217;re looking to do high-end, high-performance graphics work (i.e. not just the occasional animation), you&#8217;ll probably have to drop down to OpenGL, and for that the ultimate tutorial would be <a href="http://nehe.gamedev.net" target="_blank">The NeHe OpenGL Tutorials</a>, which also comes with info on how to do them on a Mac.</p>
<h3>Would you recommend Cocoa or Carbon for games?</h3>
<p>It depends. If you&#8217;re doing a lot of standard UI, Cocoa is definitely the better choice. If you&#8217;re doing a lot of custom UI, you won&#8217;t be using much of either UI library. Since Apple&#8217;s been frantically trying to saw off the branch on which Carbon sits in recent years, the decision has probably been made for you.</p>
<p>In the end, it again depends on your kind of game. If you&#8217;re doing a board game or another kind of game that only needs 2D graphics (and this includes pre-rendered 3D views, and thus also many isometric game graphics as long as they can&#8217;t be freely rotated), Cocoa&#8217;s NSViews will work.</p>
<p>Carbon has a leg up there with HIViews, because they are actually supported to be drawn overlapping. In Cocoa that works as of Leopard, though it&#8217;s a bunch more complicated. You may have to sort the view list occasionally, or switch on CoreAnimation layers (thus needlessly increasing system requirements to exclude lower-end graphics cards).</p>
<p>If your graphics are fairly simple (e.g. pre-rendered and similar size), you may also want to try using one huge view for all of them, and using the dirty rects (&#8220;update regions&#8221;) passed to you by the OS to selectively redraw only the parts that actually changed. This will give you less built-in behaviour, but will on the other hand forego some of the overhead all the built-in behaviour has.</p>
<p>Otherwise, you&#8217;ll probably be using OpenGL, which means you&#8217;ll probably just create a standard Cocoa app and stuff an NSOpenGLView in its main window, and that&#8217;ll be about all of Cocoa you&#8217;ll find yourself using. The remainder of the display code would probably be C OpenGL calls, maybe wrapped in C++ or Objective C classes for more manageability. If you&#8217;re wondering how you could use C++ in an NSOpenGLView, search the web for &#8220;Objective C++&#8221;, which is a way to use both ObjC and C++ in the same file.</p>
<h3>What about sound?</h3>
<p>For sound-output, Cocoa&#8217;s classes always were a bit rudimentary. Only as of Leopard, they finally allow volume control and specifying a device, making the sound loop and even specifying an output device and a channel mapping. Before that, the Carbon sound manager was close (though it was not quite easy to load sounds other than &#8216;snd &#8216; (.sfil) and AIFF), but most people had to punt and just open an audio-only QuickTime movie and play that, either using NSMovie or later QTMovie. However, for most apps these APIs should be enough to play background music and the occasional sound effect.</p>
<p>If you need Tiger compatibility or anything more complicated, like real ambient sounds, you&#8217;ll probably find yourself dropping down to CoreAudio, which is a horribly complicated API for sound playback (For a taste: One CoreAudio engineer called pausing playback an &#8220;advanced feature&#8221; at a presentation). There&#8217;s an AUFilePlayer that can be made to work if your input is a file and you don&#8217;t need to know when it&#8217;s finished playing, but otherwise you&#8217;ll be in a bit of pain using that.</p>
<p>Alternately, you can leverage open source libraries whose Mac versions were built on top of CoreAudio, like <a href="http://www.openal.org" target="_blank">OpenAL</a>, to retain your sanity.</p>
<p>Any actual game programmers wanna chime in with their suggestions?</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/how-to-become-a-games-programmer-on-the-mac/feed/</wfw:commentRss>
		<slash:comments>0</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>Intel assembler on Mac OS X</title>
		<link>http://orangejuiceliberationfront.com/intel-assembler-on-mac-os-x/</link>
		<comments>http://orangejuiceliberationfront.com/intel-assembler-on-mac-os-x/#comments</comments>
		<pubDate>Wed, 25 Apr 2007 13:58:37 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Assembler]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[core duo]]></category>
		<category><![CDATA[core solo]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[i686]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[mach]]></category>
		<category><![CDATA[machine language]]></category>
		<category><![CDATA[macho abi]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=225</guid>
		<description><![CDATA[I&#8217;ve always wanted to learn another assembler, and with one of my colleagues being a real assembler guru, and the Intel reference books on my bookshelf, and the Intel switch just behind us, I thought this would be a good opportunity to finally get going with x86 assembler. Now, assembler programming under Mac OS X &#8230;  <a href="http://orangejuiceliberationfront.com/intel-assembler-on-mac-os-x/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve always wanted to learn another assembler, and with one of my colleagues being a real assembler guru, and the Intel reference books on my bookshelf, and the Intel switch just behind us, I thought this would be a good opportunity to finally get going with x86 assembler.</p>
<p>Now, assembler programming under Mac OS X isn&#8217;t quite as well documented as one would wish. There&#8217;s no tutorial that I could find (lots of tutorials for Linux and Windows, but none for Mac OS X yet). This won&#8217;t be one either, but rather this is a blog posting of me sharing what I found out about assembler on OS X, and is probably only useful to someone who already knows <em>some</em> assembler, but just doesn&#8217;t know Intel on Mac OS X. My main approach is to <a href="http://orangejuiceliberationfront.com/nice-intel-assembler-text/">compile C source code into assembler source files using GCC</a>. Then I can look at that code and find out what assembler instructions correspond to what C command. If all of this turns out to be correct and I should happen to have loads of time on my hand, I may still go out there and turn this into a decent tutorial.</p>
<h2>The basics are pretty simple</h2>
<pre>	.text						# start of code indicator.
.globl _main					# make the main function visible to the outside.
_main:							# actually label this spot as the start of our main function.
	pushl	%ebp				# save the base pointer to the stack.
	movl	%esp, %ebp			# put the previous stack pointer into the base pointer.
	subl	$8, %esp			# Balance the stack onto a 16-byte boundary.
	movl	$0, %eax			# Stuff 0 into EAX, which is where result values go.
	leave						# leave cleans up base and stack pointers again.
	ret							# returns to whoever called us.</pre>
<p>Now, the underscore in front of &#8220;main&#8221; is a convention in C, so just accept it. When you enter the _main function, the return address (i.e. the instruction where the program will continue after the function has finished, aka &#8220;back pointer&#8221;) has already been pushed on the stack, taking up 4 bytes. We also save the base pointer (the point where our caller can find its parameters on the stack) to the stack, and set it to the current stack pointer (which is where our parameters are). That takes another 4 bytes, so we have 8 bytes now. Since the stack should be aligned on 16 bytes before you can make a call to another function, we subtract another 8 from the stack pointer, which pads out the stack (we could also just do two &#8220;pushl $0&#8243; for the same effect). If we used any local variables, we would use this opportunity to subtract more for them.</p>
<p>Now comes the actual body of our function. What we do is simply return 0. This is done by stuffing 0 in the <tt>eax</tt> register.</p>
<p>Finally, we have the tail end of our function, which calls <tt>leave</tt> (which cleans up by restoring our caller&#8217;s base pointer and stack pointer) and then call <tt>ret</tt>, which pops the return address off the stack and continues execution there.</p>
<h2>Calling a local function</h2>
<p>Calling a function is fairly simple, as long as it&#8217;s a local one right in the same file as ours. In that case, what you do is you first declare that function:</p>
<pre>	.text
.globl _doSomething				# Our doSomething function.
_doSomething:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	nop							# does nothing.
	leave
	ret
.globl _main
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$24, %esp			# 8 to align, 16 for our 4-byte parameter and padding.
	movl	$3, (%esp)			# write our parameter at the end of the stack (i.e. padding goes first).
	call	_doSomething		# call doSomething.
	movl	$0, %eax
	leave
	ret</pre>
<p>&#8220;nop&#8221; is a do-nothing instruction I just inserted here to show where doSomething&#8217;s code would go. That&#8217;s pretty easy. You just write the function, push the parameters on the stack and use <tt>call</tt> to jump to the function, and that will take care of pushing the return address and all that. The only tricky thing is passing the parameters. You have to pad first, and then push (or mov, in our case) the parameters in reverse order (i.e. #1 is at the bottom of the stack, #2 above it etc.). That&#8217;s because otherwise the function being called would have to skip the padding. Well, could be worse.</p>
<h2>Accessing parameters</h2>
<p>To acess any parameters, you address relative to the base pointer. The value immediately at the base pointer is generally your caller&#8217;s base pointer and the return address, so you need to add 4 + 4 = 8 bytes. Yes, since the stack starts at the end of memory and grows towards the beginning, and you subtract from the stack pointer to make it larger, you need to add to the stack pointer to find something on the stack. The same applies to our base pointer, of course:</p>
<pre>	movl	12(%ebp), %eax	# get parameter 2 at offset 4 + 4 + 4
	addl	8(%ebp), %eax	# get parameter 1 at offset 4 + 4</pre>
<p>Would store your second parameter in <tt>eax</tt> and then add the first parameter to it, leaving the result in <tt>eax</tt>, where it&#8217;s ready for use as a return value. Note the ##(foo) syntax, which adds the number ## to the pointer foo. This is register-relative addressing.</p>
<p>An added benefit of this is that you can actually pass more parameters to a function than it knows to handle, and it will just ignore the rest.</p>
<h2>Fetching data</h2>
<p>To access data (e.g. strings), it gets trickier. You declare data like the following:</p>
<pre>	.cstring
myHelloWorld:
	.ascii "Hello World!\0"
	.text
.globl _main
_main:
<span style="color: #888888;">. . .</span></pre>
<p>So, you add a <tt>.cstring</tt> section at the top of the function, and in that you declare a label and use the <tt>.ascii</tt> keyword to actually stash your string there. So far, so good, there&#8217;s only one problem:</p>
<p>All data manipulation is done using absolute addresses. But we don&#8217;t know at what position in memory our program will be loaded. Labels aren&#8217;t absolute addresses, they get compiled into relative offsets from the start of our code. So, how do we find out at which absolute address our string <tt>myHelloWorld</tt> is? Well, the trick MachO uses is that it knows that our program will be loaded as one huge chunk. So, we know that the distance between any of our instructions in the code will always stay at the same distance to our string.</p>
<p>So, if we could only get the address of <em>one</em> instruction in our code that has a label, we could calculate the absolute address of our string from that. Now, look above, at our function call code. Notice anything? Our return address is an absolute pointer to the next instruction after a function call. So, all we need to do to get our address is call a function. When you assemble C source code, they call this helper function <tt>___i686.get_pc_thunk.bx</tt>, which is quite a mouthful. Let&#8217;s just call it <tt>_nextInstructionAddress</tt>:</p>
<pre><span style="color: #888888;">. . .</span>
	call	_nextInstructionAddress
myAnchorPoint:
<span style="color: #888888;">. . .</span></pre>
<p>That&#8217;s what we call somewhere at the start of our code to find our own address. Note how I cleverly already added a label <tt>myAnchorPoint</tt>, which labels the instruction whose address we&#8217;ll get. Then we somewhere (e.g. at the bottom) define that function:</p>
<pre><span style="color: #888888;">. . .</span>
_nextInstructionAddress:
	movl	(%esp), %ebx
	ret</pre>
<p>We don&#8217;t even bother aligning the stack or changing and restoring the base pointer. This simply peeks at the last item on the stack (the return address) and stashes that in register <tt>ebx</tt>. Then it returns (and obviously doesn&#8217;t call <tt>leave</tt> because we pushed no base pointer that it could restore).</p>
<p>Once we have this address in <tt>ebx</tt>, we can do the following to get our string&#8217;s address into a register, and from there onto the stack:</p>
<pre><span style="color: #888888;">. . .</span>
	leal	myHelloWorld-myAnchorPoint(%ebx), %eax
	movl	%eax, (%esp)
<span style="color: #888888;">. . .</span></pre>
<p>LEA means &#8220;Load Effective Address&#8221;, i.e. take an address and stash it into a register. myHelloWorld-myAnchorPoint calculates the difference between our two labels, and thus tells us how far myHelloWorld is from myAnchorPoint. Since myHelloWorld is probably at the start of the program, e.g. at address 3 maybe, and myAnchorPoint further down, say at address 20, what we get is a negative value, e.g. -17. And <tt>xxx(%ebx)</tt> is how you tell the assembler that you want to add an offset to a register to get a memory address. <tt>ebx</tt> contains the address of myAnchorPoint, so what this does is subtract 17 from myAnchorPoint&#8217;s absolute address, giving us the absolute address of myHelloWorld! Whooo! And this mess is called &#8220;position-independent code&#8221;.</p>
<p>Now, our call to <tt>LEA<strong>L</strong></tt> loads a &#8220;<strong>L</strong>ong&#8221; (which is 32 bits, i.e. the size of a pointer on a 32-bit CPU) and stashes it into register <tt>eax</tt>. And the <tt>movl</tt> call moves that long from our register into the last item on the stack, ready for use as a parameter to a function.</p>
<h2>Calling a system function</h2>
<p>Now, it&#8217;d be really nice if we could <tt>printf()</tt> or something, right? Well, trouble is, we don&#8217;t know the address of <tt>printf()</tt>. But this time it&#8217;s actually easy. We add a new section at the bottom of our code:</p>
<pre><span style="color: #888888;">. . .</span>
	.section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
_printf_stub:
	.indirect_symbol _printf
	hlt ; hlt ; hlt ; hlt ; hlt
_getchar_stub:
	.indirect_symbol _getchar
	hlt ; hlt ; hlt ; hlt ; hlt</pre>
<p>This is a new section named <tt>__IMPORT,__jump_table</tt>. It has the <em>type</em> <tt>symbols_stubs</tt> and the attributes <tt>self_modifying_code</tt> and <tt>pure_instructions</tt>. 5 is the size of the stub, and intentionally is the same as the number of <tt>hlt</tt> statements below.</p>
<p>This section is special, because when our code is loaded, the <a href="http://orangejuiceliberationfront.com/building-a-loader/">loader</a> will look at it. It will see that there is an <tt>.indirect_symbol</tt> directive for a function named &#8220;printf&#8221;, and will look up that function. Then it will replace the five <tt>hlt</tt> instructions, each of which is one byte in size, with an instruction to jump to that address (hence the <tt>self_modifying_code</tt>). We also added a label for each indirect symbol, which we name the same as the symbol, just with &#8220;_stub&#8221; appended.</p>
<p>So, to call printf, all you have to do now is push the string on the stack and then</p>
<pre>	call	_printf_stub</pre>
<p>Which will jump to _printf_stub and immediately continue to <tt>printf</tt> itself. And just to show you that you can have several such imported symbols, I&#8217;ve also included a stub for <tt>getchar</tt>. Now note that the system usually doesn&#8217;t name these symbols &#8220;_foo_stub&#8221;, but rather &#8220;L_foo$stub&#8221; (yes, a label name can contain dollar signs. You can even put the label in quotes and have spaces in it&#8230;). Same difference.</p>
<p>Okay, so that&#8217;s how much I&#8217;ve guessed my way through it so far. Comments? Corrections? If you want</p>
<p>PS &#8211; Thanks to John Kohr, Alexandre Colucci, Jonas Maebe, Eric Albert and Jordan Krushen, all of which helped me figure this out one way or the other. Thanks, guys!</p>
<p><strong>Update:</strong> Added mention of how to actually access parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/intel-assembler-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Carbon for the Cocoa Guy: Handles</title>
		<link>http://orangejuiceliberationfront.com/carbon-for-the-cocoa-guy-handles/</link>
		<comments>http://orangejuiceliberationfront.com/carbon-for-the-cocoa-guy-handles/#comments</comments>
		<pubDate>Wed, 18 Oct 2006 10:46:34 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[carbon]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[handle]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[summary]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=62</guid>
		<description><![CDATA[One of the more confusing aspects of MacOS programming is the Handle. That doesn&#8217;t have to be so. I&#8217;ll quickly illustrate the history of the Handle and then everything should become clearer. I&#8217;ll also include a little memory-management-101 at the beginning. Memory Fragmentation When you use malloc() or similar APIs to allocate memory, you face &#8230;  <a href="http://orangejuiceliberationfront.com/carbon-for-the-cocoa-guy-handles/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p>One of the more confusing aspects of MacOS programming is the Handle. That doesn&#8217;t have to be so. I&#8217;ll quickly illustrate the history of the Handle and then everything should become clearer. I&#8217;ll also include a little memory-management-101 at the beginning.</p>
<h3>Memory Fragmentation</h3>
<p>When you use <tt>malloc()</tt> or similar APIs to allocate memory, you face the problem of fragmentation: Imagine you have the following (top) situation in memory:</p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[Illustration of three consecutive memory blocks, where the one in the middle gets deleted, fragmenting memory]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/10/memory_fragmentation.png" border="0" alt="[Illustration of three consecutive memory blocks, where the one in the middle gets deleted, fragmenting memory]" width="230" height="166" /></p>
<p> </p>
<p>You have three blocks of memory: The blue one, the green one and the red one, each 6 bytes in length. Now, you dispose of the green one by calling <tt>free()</tt> (bottom). Now there&#8217;s a six-byte hole between the two blocks. The total free memory (white blocks) is 108 bytes. Trouble is, since the computer can only allocate contiguous blocks of memory, the largest block you can allocate is 102 bytes.</p>
<p> </p>
<p>Now, we can&#8217;t just move the red block to the left to make more continuous free space available, because our program keeps track of each block by its position (its &#8220;address&#8221;), and moving it would change that address. The system would have to go through your program and change each occurrence of the moved block&#8217;s address, which is simply impossible since only your program knows which parts of its memory are used for what. Not to mention it would cause pauses in execution. So, if you needed 103 bytes, you couldn&#8217;t get them, even though we have 108 free bytes in total, more than we&#8217;d need.</p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[Illustration of three pointers pointing to the same memory block]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/10/memory_regular_pointer.png" border="0" alt="[Illustration of three pointers pointing to the same memory block]" width="229" height="94" /></p>
<p> </p>
<h3>Handles &#8211; a modern solution to fragmentation</h3>
<p>So, what Apple did is they created the Handle. A Handle is essentially a centralized way of storing pointers to memory blocks, so the system only has to change <em>one</em> centralized pointer when it needs to move a block to make more memory available.</p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[Illustration of three Handles pointing at a master pointer, which in turn points at the memory block]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/10/memory_master_pointer.png" border="0" alt="[Illustration of three Handles pointing at a master pointer, which in turn points at the memory block]" width="229" height="93" /></p>
<p> </p>
<p>Each pointer to the actual memory is owned by the system, and kept in a central table of pointers (the &#8220;master pointer block&#8221;). When you want to allocate memory, you ask the system to do it for you using the <tt>NewHandle()</tt> function. The system gives you a Handle, which is a <em>pointer</em> to the actual <em>master pointer</em> it owns. You only use this Handle to remember where your memory is. When you want to access the memory, you de-reference the Handle once to get at the pointer, and then use that pointer like any other.</p>
<p>The advantage of this is that all access to memory goes through those central master pointers. When the OS has to move a block of memory, all it has to do is change the master pointer, because all Handles point there. The OS also takes care to bunch up all master pointers at the start of memory, and to re-use old master pointers, so that they can&#8217;t fragment memory.</p>
<p>Since we don&#8217;t want the OS to move around our master pointer while we are using it, there is an <tt>HLock()</tt> function that lets you mark a Handle as immovable, and <tt>HUnlock()</tt> to make it movable again as soon as you are finished. To get rid of a Handle, there is the <tt>DisposeHandle()</tt> function (So, NewHandle/DisposeHandle can be seen as roughly equivalent to malloc() and free(), though they&#8217;re not exchangeable).</p>
<h3>History, or Carbon, intervenes</h3>
<p>By the time Apple switched to Mac OS X, pretty much everything was a Handle. There were ControlHandles used for buttons, MenuHandles used for menus &#8230; Since Mac OS X was very different under the hood, Apple needed to change the way menus and controls worked. Mac OS 9 had a single address space, so it was easy to just hand a menu Handle between applications.</p>
<p>But Mac OS X has protected memory. For security reasons, every application runs in its own segregated area of memory, with very limited access to memory in other applications. Moreover, the Window Server, responsible for drawing windows and the menu bar, is now a separate application. You can&#8217;t just pass it a MenuHandle, it has to be something different.</p>
<p>Since Apple didn&#8217;t want to break everyone&#8217;s code, they changed the name of <tt>ControlHandle</tt> to <tt>ControlRef</tt>, but kept the old name for compatibility, even though it isn&#8217;t a Handle anymore. Which means you have to be extra careful with <em>xxx</em>Handle data types.</p>
<h3>Handles &#8211; a solution still used today</h3>
<p>Unix offers a slightly improved version of the same approach: Each address a program uses isn&#8217;t a real address on the RAM chip, but rather a &#8220;virtual&#8221; address that gets translated to a physical address in RAM. There is a translation table for translating addresses from virtual to physical. When the OS needs to move a block of memory on the chip, it essentially just changes this translation table (I&#8217;m simplifying here). So, your code doesn&#8217;t even have to know its memory just moved, doesn&#8217;t have to mess with Handles. But that needs a faster, better CPU with a Memory Management Unit, which early Macs with their 680&#215;0 CPUs didn&#8217;t have (at least not until the Performas and Quadras).</p>
<p>Since Mac OS X is a descendant of BSD Unix, it inherited this, and Apple uses this new Unix-style mechanism in most new APIs. Also, in Mac OS X, a Handle is <em>never</em> moved. Since, under the hood, the master pointer&#8217;s memory block is allocated using malloc(), it&#8217;s not necessary anymore. So, if you write new code, use malloc()/free(). If you need to talk with APIs that still use Handles, use NewHandle()/DisposeHandle() and double-de-reference, but there&#8217;s no need to call HLock()/HUnlock() anymore. Though it doesn&#8217;t hurt either.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/carbon-for-the-cocoa-guy-handles/feed/</wfw:commentRss>
		<slash:comments>0</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 13:51:11 by W3 Total Cache -->