Thursday, November 18, 2010

PowerPC Assembly Tutorial on AIX: Chapter 1, The first steps

Its been quite some time since I last posted. In the next few posts, I'll try to present an introduction to PowerPC assembly on AIX.
The motivation for this comes from my personal experience trying to program in assembly on AIX. I found plenty of documentation on the instruction set, assembler directives etc. However, what I couldn't find was a step-by-step tutorial on how to write basic assembly programs. True, there were some developerworks articles, but the code presented in those articles hardly ever worked.
My endeavour is to present a primer into PowerPC assembly programming on AIX. Most of my programs will be sub-optimal, and simplistic. The goal is not to write perfect programs - rather to get someone started on PowerPC assembly programming on AIX, so that he can go on from here and take advantage of the large amount of material available on the web on PowerPC programming.

The first program, usually written in any programming language, is the hello world program. However, writing a hello world program, in assembly is certainly not the easiest first. We will start with a much simpler program. A program that does nothing, or, almost nothing. The program just exits with an exit value.

The default extension of an assembly program is .s.

  1.         #File. 1_1.s
  2.         .csect
  3.         .globl .main
  4.         .main:
  5.                 li 3, 5
  6.                 blr
In this tutorial, we will use the xlc compiler to compile our first assembly program. $ cc 1_1.s And now, onto running this program: $ ./a.out $ echo $? 5 The first line in this program is a comment. Comments start with a #. A Comment can be placed anywhere in a line. Any text after the # in a line is ignored by the assembler. The second line in the program tells the assembler that this is a csect, or a relocatable module. We will learn more about csects in section .
The third line tells the assembler that .main is a global symbol, and other objects can link to it.

Line 4 is a label named .main. The assembler recognizes that this is a label by the colon following the label name. Line 3 and line 4 work together to signify that .main is a global symbol, and its address is specified by the label ine line 4.

In PowerPC, the convention for a function to return a value is to store it in register 3. Line 5 loads the value 5 into the register 3. 'li' is a load instruction, and loads an immediate value into a register.

In AIX, whenever a binary is run, the function __start is automatically executed. __start then calls the symbol .main.

In PowerPC, whenever one function calls another, it does so by executing the instruction bl, or branch and link. bl stores the address of the next instruction to be executed in the link register, and then branches to the specified address.
Therefore, when the callee function returns, it should start executing the instruction whose address is specified in the link register. While returning, the callee simply executes blr (branch to link register) instruction, which automatically loads the contents of the contents of the link register into the program counter and starts executing it.

More posts on PowerPC assembly to come in the following weeks.

No comments: