Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Hello World for PPC ASM?

Hello World for PPC ASM?
Thread Tools
Mac Elite
Join Date: Jan 2001
Location: Sad King Billy's Monument on Hyperion
Status: Offline
Reply With Quote
Oct 23, 2001, 11:46 PM
 
Can someone direct me toward the source code for a "Hello World"-type app written in Mach-O PPC ASM?
I abused my signature until she cried.
     
Mac Enthusiast
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 24, 2001, 01:11 AM
 
Hi,

You could write it by hand, or you could write it in C and let cc disassemble it. This is

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> #include &lt;stdio.h&gt;int main( void ){ printf(<font color = red>"Hello world!\n"</font>); return <font color = blue>1</font>;}</font>[/code]

Doing 'cc -S filename.c' results in filename.s with

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> .macro mul mul$<font color = blue>0</font> $<font color = blue>1</font>,$<font color = blue>2</font>,$<font color = blue>3.</font>endmacro.macro div div$<font color = blue>0</font> $<font color = blue>1</font>,$<font color = blue>2</font>,$<font color = blue>3.</font>endmacro.cstring .align 2LC0: .ascii <font color = red>"Hello world!\<font color = blue>012</font>\<font color = blue>000</font>"</font>.text .align <font color = blue>2.</font>globl _main_main: mflr r0 stmw r30,<font color = blue>-8</font>(r1) stw r0,<font color = blue>8</font>(r1) stwu r1,<font color = blue>-80</font>(r1) mr r30,r1 bcl <font color = blue>20</font>,<font color = blue>31</font>,L1$pbL1$pb: mflr r31 addis r3,r31,ha16(LC0-L1$pb) la r3,lo16(LC0-L1$pb)(r3) bl L_printf$stub li r3,<font color = blue>1</font> b L6L6: lwz r1,<font color = blue>0</font>(r1) lwz r0,<font color = blue>8</font>(r1) mtlr r0 lmw r30,<font color = blue>-8</font>(r1) blr.picsymbol_stubL_printf$stub: .indirect_symbol _printf mflr r0 bcl <font color = blue>20</font>,<font color = blue>31</font>,L2$pbL2$pb: mflr r11 addis r11,r11,ha16(L2$lz-L2$pb) mtlr r0 lwz r12,lo16(L2$lz-L2$pb)(r11) mtctr r12 addi r11,r11,lo16(L2$lz-L2$pb) bctr.lazy_symbol_pointerL2$lz: .indirect_symbol _printf .long dyld_stub_binding_helper </font>[/code]<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> </font>[/code]

Nice to hear someone interested in assembler!

-- Daniel
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Oct 26, 2001, 12:44 AM
 
The above advice is the absolute WORST way I know to learn assembly.

A C compiler will output tons of unnecessary code, just like it did
in the previous example. First off there was absolutely no need for printf. Find the system call for write in syscalls.h, push the necessary arguments on the stack, call the kernel via an interrupt and away you go.
(OSX might pass parameters in registers instead. I'm not sure.)

This reminds me of a fellow who decided he would let Microsoft's C compiler
do his ASM homework in college. Guess who failed.

Have a look at Linux Assembly and Lightsoft. The former is pretty x86 Linux specific, but BSD is covered and interfacing w/ the kernel should be similiar. The latter offers pretty good documentation on PPC asm.

You should know that writing asm for RISC processors is bad for your health. Also, an optimising compiler will output faster code than 99.9% of all RISC asm programmers.
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Oct 26, 2001, 04:30 AM
 
Originally posted by int69h:
<STRONG>The above advice is the absolute WORST way I know to learn assembly.

A C compiler will output tons of unnecessary code, just like it did
in the previous example. First off there was absolutely no need for printf. Find the system call for write in syscalls.h, push the necessary arguments on the stack, call the kernel via an interrupt and away you go.
(OSX might pass parameters in registers instead. I'm not sure.)

This reminds me of a fellow who decided he would let Microsoft's C compiler
do his ASM homework in college. Guess who failed.

Have a look at Linux Assembly and Lightsoft. The former is pretty x86 Linux specific, but BSD is covered and interfacing w/ the kernel should be similiar. The latter offers pretty good documentation on PPC asm.

You should know that writing asm for RISC processors is bad for your health. Also, an optimising compiler will output faster code than 99.9% of all RISC asm programmers.</STRONG>
For function calls we use a variant of the PowerOpen ABI. We do pass args on registers, the first arg is on r4. I can't recall how many registers are volatile across function calls, I think up to r8 or r11 are.

I really, really, really do not recommend you attempt to trigger a system call directly from your own asm. Use the system library. The actual system call mechanisms has *changed* between releases before. For those who have tried to build xnu, his is one of the reasons why some libraries have to be kept in lockstep with the kernel.

Louis
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Oct 26, 2001, 11:11 AM
 
Thanks for the info on the calling conventions Louis.


Let me state again that IMHO anyone writing an application of any significant size in PPC asm is either an idiot or a masochist. I still believe that calling the kernel directly for simple "Hello World" style programs is the way to go if one is interested in finding out how the system works below the libc level.

Any progress on softupdates? JFS has come to a standstill since my ADC Select membership ran out right before 10.1 came out. I haven't been able to find the cash to by a retail copy of 10.1, much less renew my membership.
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Oct 27, 2001, 04:07 PM
 
Originally posted by int69h:
<STRONG>Thanks for the info on the calling conventions Louis.


Let me state again that IMHO anyone writing an application of any significant size in PPC asm is either an idiot or a masochist. I still believe that calling the kernel directly for simple "Hello World" style programs is the way to go if one is interested in finding out how the system works below the libc level.

Any progress on softupdates? JFS has come to a standstill since my ADC Select membership ran out right before 10.1 came out. I haven't been able to find the cash to by a retail copy of 10.1, much less renew my membership.</STRONG>
I would never write significant things in asm, too time consuming. I will say that when I do bother to write it I tend to beat gcc by a wide margin. I hear gcc-3.1 has much improved code gen though, and am looking forward to it.

Softupdates is sitting still, since some things it depends on are in flux, and I am working on something else (which is pretty darn cool ;-) I will let you know more I can. I am starting to like our VFS a bit more, now that I am starting to understand its idiosynchrocies.

Louis
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
Mac Enthusiast
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 09:08 AM
 
The above advice is the absolute WORST way I know to learn assembly.
It may be a bad advice to learn assembly, but it's a good way to learn about how to call C-functions and OS X runtime conventions in general. You can also see how stupid gcc is, even with -O3.

Also, an optimising compiler will output faster code than 99.9% of all RISC asm programmers.
Which compiler would that be for OS X? (This is not meant ironically, I'd really like to know!) An optimizing compiler may know a lot about the CPU pipelines and I/O delays etc., but it doesn't know nothing about what I want to do. It's got my high-level source code and may optimize this, but it can never do a straight-forward implementation of what I intended. That's why I write some of my stuff in assembly. Of course, no more than a few hundred lines.

Another reason to write low-level code is when you need to mess around a lot with single bits and bit fields (see rlwimi or cntlzw).

-- Daniel
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 03:39 PM
 
Point taken. Supposedly gcc 3.1 will have better support for PPC targets.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:33 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2