# Am i Mad?!: PowerPC Assembler



## hyph-n (Oct 26, 2001)

I'm a professional C/C++ programmer, but i fancy something that will stimulate the old grey matter......

.....i've done some Z80, 68000, & 8086 (spit) assembly in the past - i think it is about time i have a go with some PowerPC assembly 

Can anyone recommend an assembler & a good reference guide..?


----------



## whitesaint (Oct 31, 2001)

what do you mean?  Are you saying that you've been programming for 68K's for years and now you want to switch to programming for PowerPC's????

-whitesaint


----------



## AdmiralAK (Oct 31, 2001)

He probably means that hge did 68k assembler but never learn PPC


----------



## hyph-n (Oct 31, 2001)

I actually mean that i have done some 68k on an Amiga 500/1200/2000/4000 using the DevPac(?) assembler, more specifically using the 68000/68020/68030.... but that was a long time ago (10+ years!) Also did some Z80 on a ZX Spectrum 48 (which would have been around 1982!)

I'm not scared of Assembly, I understand 
_generally_ how CPUs work (registers etc..)..

What I need is a good guide to how the PowerPC (74xx?) is constructed & a reference of the Instruction Set - Also, all of the previous CPUs I've hacked have been CISC, whereas PowerPC is RISC - what practical difference does that make?

Assuming that i can get the reference material, is there a defacto Assembler to use (console based is cool, but can live with GUI solution as well)

...


----------



## blb (Oct 31, 2001)

> _Originally posted by hyph-n _
> *Assuming that i can get the reference material, is there a defacto Assembler to use (console based is cool, but can live with GUI solution as well)
> 
> ... *



When you install the developer tools, you will have an assembler installed.  For info on it, just do a *man as*.


----------



## hyph-n (Nov 1, 2001)

great stuff! - I had already installed the Developer Tools & have indeed got the 'as' command 

-cheers!-

guess all i need now is the reference material


----------



## iconara (May 28, 2002)

I realise that this was posted long ago, but I wanted to answer anyway...


according to my SE/CS-teachers, it's virtually impossible to write assembler for PPC as for any true RISC-processor, because of the minimal instruction set and the fact that the processor doesn't execute the instructions in a somewhat non-deterministic manner. it would be nice though to see some code...


theo


----------



## hyph-n (May 28, 2002)

thanks for the top-tip iconara....

... guess i must be Mad then?! ;-)

.... i'll update you when i've written something... (even if it just adds 2 integers together!)


----------



## smeger (May 28, 2002)

I'd be interested in seeing some working code, too.  I thought I needed to write some awhile ago and found a few resources, which I've now lost.  I recall that apple had a page explaining the mnemonics, as did motorola.  Google is your friend.

By the way, I wound up not needing assembly, so this panned out to nothing for me.  Whooo....


----------



## fintler (May 29, 2002)

an easy way to get a feel for ppc asm is to compile a simple C app with gcc -static -S bleh.c ..it'll give you a .s file with the (sometimes obfscuated) att syntax ppc asm that you can simplify and make faster (not usually possible unless you're really smart)


----------



## Krevinek (Jun 6, 2002)

Well, writing PPC Asm is actually nicer than writing in 68k or x86 Asm (IMO, I have tinkered a bit, back in the 601/603/604 heyday), BUT... the teacher who said writing it was nearly impossible got a little confused.

It is EASY to write PPC Asm... writing GOOD PPC Asm is nearly impossible (but doable). These days a C compiler can produce faster code for a P4, Athlon, or PPC than a human can at the asm level. The difference between the two only starts adding up and being noticeable in the larger apps though.


----------



## ladavacm (Jun 11, 2002)

> _Originally posted by Krevinek _
> *Well, writing PPC Asm is actually nicer than writing in 68k or x86 Asm (IMO, I have tinkered a bit, back in the 601/603/604 heyday), BUT... the teacher who said writing it was nearly impossible got a little confused.*



Talking out of his rear end would be closer to the truth.



> *It is EASY to write PPC Asm... writing GOOD PPC Asm is nearly impossible (but doable). These days a C compiler can produce faster code for a P4, Athlon, or PPC than a human can at the asm level. *



You are talking about a *good, optimizing with deep and intimate knowledge of target processor internal pipeline architecture* C compiler here. Sadly, gcc is not such a compiler.  TANSTAAFL, I guess.


----------



## iconara (Jun 11, 2002)

ladavacm: I have never seen the acronym "TANSTAAFL" before, I'm curious.

I was under the illusion that GCC was the best there is, is this not so? (My world is falling apart =)    ).


theo


----------



## Krevinek (Jun 11, 2002)

I still stand by the fact that a C compiler can produce better code than most humans these days. GCC needs work, yes (especially for a pure RISC chip like the PPC series) but the issue comes down to general optimization.

A human tends to think in linear terms, even some of the better programmers. With multiple Integer, Floating Point, and (possibly) Vector units available in the recent G4s, it becomes real difficult to keep that all straight AND think non-linearly. 

As I also said, the difference becomes more noticeable as the project gets larger, as you have more lines of code to tweak and analyze in a fashion that you don't have to do with C.

Asm isn't bad for small projects, but I have seen some time-critical apps (written for MetroWerk's compiler) gain a little better performance by moving from Asm to highly speed-optimized C.

All this is academic of course, since programmers are lazy and leave stuff unoptimized...


----------



## iconara (Jun 12, 2002)

I hear that wordperfect was written mostly in assembler... and fast it was, but not in development.... (may it's soul rest in peace)



has anyone tried Java-assembler, that's one cool thing (never seen it though), talk about poinlessity! 



coolness == pointlessity


----------



## Krevinek (Jun 12, 2002)

Also note that WordPerfect was written before the age of multiple instruction units and the pipeline-crazy development of the past few years.


----------



## Grecy (Jun 17, 2002)

I am amazed at some of the reply's here..

People saying that its impossible to write PPC assembler... 

Thats mental. Alitvec is unreal.

You guys should checkout forums.3ivx.com and www.macdvd.org/cgi-bin/Ultimate.cgi where all they do is talk about writing more altivec to get a few for fps when decoding/encoding MPEG2 and MPEG4 stuff.

checkout MacMPEG2Decoder (MM2D) on Sourceforge.. there are files and files of Altivec (mostly for the iDICT routines)

Talk to Stux (lead programmer of 3vix) about Altivec.. he's been optmizing the encoder of the codec for ages now, says he writes nothing but Altivec.

There is a reference page somewhere at apple.com that explains every altivec command and what they do.. can't remember it tho, see if you can find it.

hope this keeps you interested.
-Dan


----------



## iconara (Jun 19, 2002)

well Altivec is not PPC. They are two separate beings (PPC is the CPU, Altivec is a slave/helper processor). 

You cannot write more efficient PPC assembler than GCC, but you can probably hack good Altivec assembler if you're into it.

It's not just PPC assembler in particular, it's assembler for any RISC-processor (excluding Pentium, since it runs CISC instructions). You can still hack 68K for PPC, as far as I know, but the instructions are emulated and won't run faster than a Quadra...



Theo


----------



## lethe (Jun 26, 2002)

> _Originally posted by iconara _
> *ladavacm: I have never seen the acronym "TANSTAAFL" before, I'm curious.
> 
> I was under the illusion that GCC was the best there is, is this not so? (My world is falling apart =)    ).
> ...



*T*here *A*in t *N*o *S*uch *T*hing *A*s *A **F*ree *L*unch

ladavacm, i was also under the impression that gcc was the finest compiler ever.  is that only true for x86?  is it even true at all?  is gcc not designed for RISC processors?  i was also under the impression that the RISC/CISC designation was anachronistic and no longer accurate.  i m no expert though

i know that motorola released a patch for gcc to give it altivec support.  i ve also seen that a lot of altivec is being merged into the main tree.  i would expect apple to also want to do a lot of work on gcc to make it work well on the PPC architecture, including, but not limited to, improving altivec support.  

so is this the case?

and, if there really is no such thing as a free lunch, what is a good compiler for PPC or altivec?  the only other compiler that i have ever used is codewarrior.  does it stack up?


----------



## ladavacm (Jun 27, 2002)

> _Originally posted by lethe _
> *
> 
> There Ain t No Such Thing As A Free Lunch
> ...



gcc is a far cry from a good optimizing compiler; the only compiler that I know to be generating worse code is Visual C++ version 5 (the last version at which I have actually taken a deep look)--I cannot comment on others.
gcc is (un)fortunately, free.  It also pretty much killed the market for UNIX (C) compilers.  It is adequate for most tasks.

By todays terms, the internal architecture of gcc is quite antiquated. It (at least, used to) models an unlimited register machine which is closer to CISC than RISC (if, indeed, there ever were such things; not even the original SPARC was really RISC).  With the current processors being more akin to data flow engines emulating CISC or RISC, one really has to know the internals of every CPU generation to keep internal execution units as busy as possible at all times, even though all these generations (claim to) implement the same external programming model.  gcc tries to implement such optimizations at a relatively late stage, with insufficient knowledge of the CPU internals (these being the crown jewels of the respective manufacturers)

Improving gcc has become somewhat difficult, and is hampered by the gcc's design goal to be easily re-targettable (i.e. it must be possible to support many different CPUs; therefore the inability to support them really well).

I cannot comment on CodeWarrior, since I have never owned or used it; you must have guessed so far that my background did not include Macs until they became UNIX  boxes  But, I would expect Motorola being able to create better compiler than gcc, especially because they are using it for SPEC benchmark suite.


----------



## lethe (Jun 28, 2002)

i m speculating, but perhaps would sun s C compiler be an example of a good compiler?  it is written specifically for one OS, for an OS that exists mostly for only one processor family, and it is written by the same company that engineered the processor.  so based on lavacdmn statements, i would bet that the solaris c compiler would be able to substantially outperform gcc on sparc/solaris.   

i wonder if this is the case.  it just so happens that i have a sparc/solaris box, but it only has gcc installed.  i could get solaris cc though, since my university has a site license.  maybe i could try some back of the envelope benchmarks....


----------



## ladavacm (Jun 28, 2002)

> _Originally posted by lethe _
> *i m speculating, but perhaps would sun s C compiler be an example of a good compiler?  it is written specifically for one OS, for an OS that exists mostly for only one processor family, and it is written by the same company that engineered the processor.  so based on lavacdmn statements, i would bet that the solaris c compiler would be able to substantially outperform gcc on sparc/solaris.
> 
> i wonder if this is the case.  it just so happens that i have a sparc/solaris box, but it only has gcc installed.  i could get solaris cc though, since my university has a site license.  maybe i could try some back of the envelope benchmarks.... *



It does, according to my measurements about 2 years ago.  In some cases, it is up to 4 times faster, especially on newest SPARC generations, some of which are not even correctly supported by gcc (i.e. incorrect code is generated)


----------



## lethe (Jun 28, 2002)

if gcc killed off most other UNIX c compilers because it is free, then i would think that all those computer companies would have a vested interest in making it good at doing whatever architecture they use.  for example, i ll bet apple has a whole little department working on improving gcc for OSX/PPC, since it is the compiler they use to compile their OS.  if enough different people did this, then it could become a great compiler for every architecture.


with so much money behind it, don t you think it would eventually start becoming a good compiler?

on the other hand, perhaps its structure isn t very modular, and doesn t lend itself well to large rewrites or overhauls, and if it is already adequate enough for most people, then why bother.


----------



## ladavacm (Jul 1, 2002)

> _Originally posted by lethe _
> if gcc killed off most other UNIX c compilers because it is free, then i would think that all those computer companies would have a vested interest in making it good at doing whatever architecture they use.  for example, i ll bet apple has a whole little department working on improving gcc for OSX/PPC, since it is the compiler they use to compile their OS.  if enough different people did this, then it could become a great compiler for every architecture.


I am sorry if I implied abundance of financial support for gcc; in fact, such support is apparently very limited.




> with so much money behind it, don t you think it would eventually start becoming a good compiler?


Except that this money could never be recovered; this has to do with the particular license under which gcc is released.  Therefore, not that much money is actually invested.  If at all any serious money is spent, it is recovered through hardware sales.



> on the other hand, perhaps its structure isn t very modular, and doesn t lend itself well to large rewrites or overhauls, and if it is already adequate enough for most people, then why bother. [/B]


It is quite modular, but not modular enough.  It does not model data-flow architectures well, hence the lack of deep optimizations.  It's design is relatively antiquated, dating to late 80ies.  gcc's internals would have to be completely rewritten to bring it up to the modern CPU architectures; this is a lot of work with bleek chances for financial gains to the company undertaking such work.  HW vendors still build their own compilers, but due to the market size, they charge an arm, a leg and both kidneys for them


----------

