<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Assembly on the PPC</title>
	<atom:link href="http://timestocome.com/wordpress2/assembly-on-the-ppc/feed" rel="self" type="application/rss+xml" />
	<link>http://timestocome.com/wordpress2</link>
	<description>Life in the flyover</description>
	<lastBuildDate>Mon, 24 Nov 2008 13:54:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: ljmacphee</title>
		<link>http://timestocome.com/wordpress2/assembly-on-the-ppc/comment-page-1#comment-10</link>
		<dc:creator>ljmacphee</dc:creator>
		<pubDate>Mon, 21 Apr 2008 12:21:35 +0000</pubDate>
		<guid isPermaLink="false">http://timestocome.org/timestocome/wordpress2/?page_id=164#comment-10</guid>
		<description>On Mon, Apr 21, 2008 at 2:21 AM, anonymous wrote:

    I know the posting is old, but I found a minor problem that might plague beginners. I believe helloworld.s should read something like this... ideally without the branches:

      1 .data                       ; section declaration - variables only
      2 
      3 msg:
      4   .ascii    &quot;Hello, world!\n\0&quot;
      5   len = . - msg         ; length of our dear string
      6 
      7 .text                       ; section declaration - begin code
      8 
      9   .globl _main
     10 _main:
     11 
     12 # write our string to stdout
     13 
     14   li      r0, 4         ; syscall number (sys_write)
     15   li      r3, 1         ; first argument: file descriptor (stdout)
     16             ; second argument: pointer to message to write
     17   lis     r4, ha16(msg); load top 16 bits of &amp;msg
     18   addi    r4, r4,lo16(msg)   ; load bottom 16 bits
     19   li      r5, len       ; third argument: message length
     20   sc            ; call kernel
     21 
     22 ; exit with error code for success of sys_write
     23   b       _err
     24   b       _success
     25 
     26 _exit: ; assume r3 has been preloaded
     27   li      r0, 1 ; syscall number (sys_exit)
     28   sc            ; call kernel
     29 
     30 _success: 
     31   li      r3, 0 
     32   b       _exit ; exit(0)
     33 
     34 _err:     
     35   li      r3, 1 
     36   b       _exit ; exit(1)
     37


    On OS X / Darwin (not sure if this applies to all PowerPC environments), &quot;sc&quot; calls don&#039;t resume to the next immediate instruction unless there&#039;s an error. By convention, the instruction immediately after a &quot;sc&quot; (srr + 0) is reserved for error handling. Upon success, the kernel will jump to (srr + 4), over the instruction immediately after the &quot;sc&quot;.

    If you comment out line 23 above, On OS X, the exit code is 1, since it enters _exit right away and r3 already had a value set on line 15.

    In your original example, r0 was still 4... so a duplicate &quot;li r0, 1&quot; would have done the trick and given the intended exit code, as it wasn&#039;t actually exiting cleanly... it was calling sys_write again.

    p</description>
		<content:encoded><![CDATA[<p>On Mon, Apr 21, 2008 at 2:21 AM, anonymous wrote:</p>
<p>    I know the posting is old, but I found a minor problem that might plague beginners. I believe helloworld.s should read something like this&#8230; ideally without the branches:</p>
<p>      1 .data                       ; section declaration &#8211; variables only<br />
      2<br />
      3 msg:<br />
      4   .ascii    &#8220;Hello, world!\n\0&#8243;<br />
      5   len = . &#8211; msg         ; length of our dear string<br />
      6<br />
      7 .text                       ; section declaration &#8211; begin code<br />
      8<br />
      9   .globl _main<br />
     10 _main:<br />
     11<br />
     12 # write our string to stdout<br />
     13<br />
     14   li      r0, 4         ; syscall number (sys_write)<br />
     15   li      r3, 1         ; first argument: file descriptor (stdout)<br />
     16             ; second argument: pointer to message to write<br />
     17   lis     r4, ha16(msg); load top 16 bits of &#038;msg<br />
     18   addi    r4, r4,lo16(msg)   ; load bottom 16 bits<br />
     19   li      r5, len       ; third argument: message length<br />
     20   sc            ; call kernel<br />
     21<br />
     22 ; exit with error code for success of sys_write<br />
     23   b       _err<br />
     24   b       _success<br />
     25<br />
     26 _exit: ; assume r3 has been preloaded<br />
     27   li      r0, 1 ; syscall number (sys_exit)<br />
     28   sc            ; call kernel<br />
     29<br />
     30 _success:<br />
     31   li      r3, 0<br />
     32   b       _exit ; exit(0)<br />
     33<br />
     34 _err:<br />
     35   li      r3, 1<br />
     36   b       _exit ; exit(1)<br />
     37</p>
<p>    On OS X / Darwin (not sure if this applies to all PowerPC environments), &#8220;sc&#8221; calls don&#8217;t resume to the next immediate instruction unless there&#8217;s an error. By convention, the instruction immediately after a &#8220;sc&#8221; (srr + 0) is reserved for error handling. Upon success, the kernel will jump to (srr + 4), over the instruction immediately after the &#8220;sc&#8221;.</p>
<p>    If you comment out line 23 above, On OS X, the exit code is 1, since it enters _exit right away and r3 already had a value set on line 15.</p>
<p>    In your original example, r0 was still 4&#8230; so a duplicate &#8220;li r0, 1&#8243; would have done the trick and given the intended exit code, as it wasn&#8217;t actually exiting cleanly&#8230; it was calling sys_write again.</p>
<p>    p</p>
]]></content:encoded>
	</item>
</channel>
</rss>
