April 22, 2007 Fixed broken links.
January 26, 2007 Now that Apple has moved to an Intel chip there seems little reason to pursue this. It is still a very popular page on my main website, so I am adding it here so that people can use the comment section to add information and perhaps help others.
June 16th 2006 I received an email today telling the ‘hello world’ program was causing seg-faults on some computers and so I’m posting the corrected version along with the information on how to fix this seg-fault problem. We all know how much fun it is to hunt down seg-fault errors. It’s under off site and other examples below.
May 22 No time, no time!!! I received an email this week with an optimized version of strlen. I haven’t had a chance to do more than to save the email as a text file, but it is included here below for anyone who is interested. Send in examples, they will help others. I’ll post ‘em as I can.
June 13th 2006 I’m finally settled enough to start digging through old email and work on some things here. I added two example programs submitted by Christopher Aubut and a link to his site in case he adds more examples. There is also a nice ‘how to do reverse engineering on a mac’ intro on his site.
May 7, 2005 All is chaos as we relocate from Boston to Houston. However I’ve received several emails and will post stuff as time allows.
Finding assembly language ‘how tos’ and ‘tutorials’ hasn’t been easy. It would seem there are few assembly programmers and fewer of them using a ppc. If anyone has simple examples or other information send it to me timestocome@gmail.com and I’ll post them here.
Times to Come Examples
The usual, prints a string ‘Hello World’ to the screen
(compile…. gcc helloworld.s -o helloworld )
hello world
Jumpstart calls the fibonacci assembly program and catches the fibonacci return value. Also shows how to do a loop and counter.
(compile…. gcc jumpstart.c fibonacci.s -o fibonaccinumber )
fibonacci.s
jumpstart.c
Corrected version with comments Example of an if..else statement
ifelse.s
ResourcesHow to’s
Old issues of Mac Tech have several articles on how to program in PPC Assembly. This links direct to one, use the search tool and you’ll find several more articles.
PPC Shellcode Assembly A fun how to, explains incorporating assembly into C code and using gdb to debug code. You’ll never forget to check your buffer input again.
IBM Introduction to assembly on the PowerPC Has short intro and several ‘hello world’ examples, I couldn’t get the examples to work.
Beginners Guide PowerPC Assembly Language
Start here if you are new to assembly Balance of Power: Introducing PowerPC Assembly Language a light weight overview
PC Assembly Language (has tutorial and other info)
Introduction to Microcontroller notes has a nice section on ppc assembly with examples.
Uniformed.org has a short introduction to ppc assembly.
References
Art of Assembly Language Programming and HLA has several downloadable books on assembly
PowerPC Microprocessor Family: The Programming Environments for 32-Bit Microprocessors PDF manual with registers, op-codes and other information
PowerPC Assembly Quick Reference op-code chart
PowerPC Assembly QuickRef another op-code chart
OpenMac: References a list of Mac hardware docs on the net
PowerPC Technical Tidbits overview stack, registers etc
PowerPC Compiler’s Writer’s Guide TOC lots of info on the hardware and lots of assembly code snippets.
The PowerPC Architecture: A Programmer’s View article overview on the hardware
Mac OS X Assembler Guide op-codes, syntax, register info, addressing modes
a xhref=”http://hpcf.nersc.gov/vendor_docs/ibm/asm/alangref.htm” mce_href=”http://hpcf.nersc.gov/vendor_docs/ibm/asm/alangref.htm” >Assembler Language Reference op-codes, syntax, addressing, assembling and linking
Offsite and submitted examples
Hello World with seg-fault error removed and information on this particular seg-fault problem.
Original asm and condensed asm strlen
PSYBALAND has a tic-tac-toe and hex dump program posted.
Several examples of assembly including some ppc examples
PowerPC Numerics ( has C and Assembler )
Macintosh Freeware by Cliff Harris several PPC Assembly programs are here
PowerPC Architecture and Assembly Language a Simple Example a lecture on ppc assembly with simple examples
Tic-Tac-Toe the computer plays itself showing step by step each move it has made
Hex Dump this program takes a file as input and prints the corresponding hex
Tips
gcc cprogram.c -S
This will give you an assembly file cprogram.s of your c code Which you can then compile gcc -o cprogram cprogram.sSystem call numbers ( for register r0 ) can be found in /usr/include/sys/syscall.h
Tools
gdb The GNU debugger
Basic syntax
instruction destination, sourcesource register may have a displacement as well
command rxx, (rxx) ; displacement is 0
command rxx, 4(rxx) ;comments ‘#’ in first column, or ‘;’ anywhere
Instructions
Instructions can be from a register to memory, memory to a register, or between registers. Load and store instructions have access to memory. (li/stw )Immediate means use the value given in the instructionLabels are followed by colons ( Total: or FunctionOne: )
Adding a ‘.’ to an instruction applies it to the Condition Register exempt for cmp which always effects the Condition Register
1 response so far ↓
1 ljmacphee // Apr 21, 2008 at 6:21 am
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 “Hello, world!\n\0″
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 &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), “sc” calls don’t resume to the next immediate instruction unless there’s an error. By convention, the instruction immediately after a “sc” (srr + 0) is reserved for error handling. Upon success, the kernel will jump to (srr + 4), over the instruction immediately after the “sc”.
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 “li r0, 1″ would have done the trick and given the intended exit code, as it wasn’t actually exiting cleanly… it was calling sys_write again.
p
You must log in to post a comment.