<GEEK MODE>
Want to learn how hardware works? want to be an ubergeek? learn assembly!
Yesterday was learn assembly day the Slakinski household, so I set forth to learn more about the PowerPC architecture and assembly language in general.
Below is a simple "Hello, World!" program, which is standard for anyone learning a new language... I hope further programs I write do more ;)
.data msg: .ascii "hello, world!n" len = . - msg .text .globl _main _main: li r0,4 ; call sys_write li r3,1 ; set sys_call to write to stdout (screen) lis r4,hi16(msg) ; load the top 16 bits of msg ori r4,r4,lo16(msg) ; load the bottom 16 bits of msg li r5,len ; length of msg sc ; call kernel li r0, 0x01 ; call sys_exit li r3, 0x00 ; exit with code 0 sc ; call kernel
The comments should explain everything but if not here is a bit more detail (since I'm new at this, please correct me if I say anything wrong) First I setup the message that I want the program to send. I do this by setting up the label "msg" and defining an ASCII value "hello, world!" and find the strings length. Next I enter the main program (_main) and instruct it to use the kernels' function called sys_write, next I tell ssy_write that I want to write to stdout, or in other words I want it to write to the terminal screen. The next two lines load the string into memory - why in two parts I don't know - maybe I have to load it in 16 bits at a time, which could be time consuming. Then I pass how long the message is into r5 (notice that r0 was what kernel function to call and r3 through to r5 have been arguments to r0, if so, good because thats what they are), The last step is to call the kernel so it can perform the above instruction, which it does nicely.
The last 3 lines are the exit code. Call the kernel function sys_exit with the parameter 0. Now when the program exits it will have an error code of 0, which indicates a nice clean exit of the program.
For a complete list of sys calls, check out /usr/include/sys/syscall.h
</GEEK MODE>