Friday, November 19, 2010

PowerPC Assembly Tutorial on AIX: Chapter 3, Calling Other Functions

Suppose you want your program to wait for the user to press a key before exiting, you would call the getchar() function which is exported by libc. Calling getchar from our program is rather straight forward, and all we have to do is to include the following line in our program:

        bl        .getchar

Note that in the above line, we have used .getchar instead of getchar. However, including this line alone in our program will not work, and in all probability, this program will just dump core. Do you know why?

We had seen that when we issue the bl instruction, the link register gets overwritten with the address of the instruction following the current one. Hence, after the bl instruction, the link register will contain the address of the following instruction (which is a part of .main). After returning from getchar(), when we issue the instruction blr from .main, we would not return to __start, as we would have over-written the link register set by __start when it issued the bl instruction.
How do we solve the problem? We create a stack frame for main, and save the link register in the frame.

Here is how the program would look like:

  1.     .set r0, 0
  2.     .set r1, 1
  3.     .set r3, 3

  4.     .extern     .getchar   # Tell the assembler that
  5.                                  # .getchar is an external symbol
  6.     .csect
  7.     .globl    .main
  8.     .main:
  9.         #### Function prolog begins ####
  10.         mflr    r0                 # Get the link register in r0
  11.         stw    r0, 8(r1)        # Save the link register
  12.         stwu    r1, -64(r1)    # Store the stack pointer, and
  13.                              # update. Create a frame of 64 bytes.
  14.         #### Function prolog ends ####

  15.         li    r3, 5
  16.         bl    .getchar
  17.         ori    r0, r0, 0          # No-op, required by loader after a 
  18.                                     # branch to an external function

  19.         #### Function epilog begins ####
  20.         addi    r1, r1, 64    # Restore the stack pointer
  21.         lwz    r0, 8(r1)      # Read the saved link register
  22.         mtlr    r0
  23.         #### Function epilog ends ####
  24.         blr

In the above program, line 5 tells the assembler that .getchar is an external symbol that is not present in the current file.

Load and store operations cannot be performed directly on the link register, and hence we have to copy the contents of the link register to another general purpose register before storing it. The mflr (move from link register) instruction takes as an argument another register, and copies the contents of the link register to the specified register.

In PowerPC, the convention is to use the general purpose register r1 as the stack pointer. In line 12, we save the value of the link register at an offset of 8 bytes from the stack pointer.

In line 13, we use the special instruction stwu (or store word and update) to advance the stack pointer and save the old stack pointer. In this line, stwu stores the value of r1, at the address r1-64, and then stores the value r1-64 in r1. Hence this single instruction allows us to do the two tasks of decrementing the stack pointer, and storing the old stack pointer at one go.

Having done this, we are ready to break into the main logic of the program. We use the bl instruction in line 18 to call getchar.

There are several special instructions, which the assembler treats specially. The instruction in line 19 is treated as a no-op. A no-op is required by loader after a call to an external function is made. We shall see why it is required later. xlc will not compile the program without the no-op. 'as' will not complain about it and compile the application.

Having done our job, we now have to restore the old values of the stack pointer (r1) and the link register. In line 23, we restore the stack pointer to its old value, by adding the immediate value 64 to it. We then load the stored link register value in r0 at line 24. We then use the mtlr (move to link register) to copy the contents of r0 to the link register. We then finish it by calling blr.

When we run this program, we see that it waits for us to enter something, and then returns.

So far, so good, but when happens when I check the exit value returned by this program?

$ ./a.out
97


We are no longer getting 5!!!

Just a note of caution, in this post, I have not followed the stack-linkage convention in its entirety, and have tried to simplify things and have only tried to capture the essence of stack-linkage. I will probably return to this topic in a later post.

No comments: