next up previous
Next: 11 Register Windows Up: A Laboratory Manual for Previous: 9 The SPARC Instruction

10 Leaf Procedures on the SPARC

 

10.1 Goal

To introduce the calling conventions associated with leaf procedures on the SPARC.

10.2 Objectives

After completing this lab, you will be able to write assembly language programs that:

10.3 Discussion

This lab is the first of three labs that cover procedure calling conventions on the SPARC. In this lab we consider the conventions associated with leaf procedures: procedures that do not make calls to other procedures. In Lab 11 we consider the use of register windows on the SPARC. In Lab 12 we complete our coverage of procedure calling conventions by considering the standard calling conventions used by compilers.

10.3.1 Register usage

The SPARC Architecture Manual describes a class of procedures called ``optimized leaf procedures.'' As we have noted, a leaf procedure is a procedure that does not call any other procedures. Optimized refers to restrictions placed on the procedure's use of the registers.

Table 10.1 summarizes register uses for optimized leaf procedures. This table specifies which registers can be modified by a leaf procedure. Registers %r8-%r13 are used for parameters passed to the leaf procedure. The first parameter (i.e., the leftmost parameter) should be placed in %r8, the next in %r9, and so forth. Note that %r8 is used for the first parameter and the return value.

   table3637
Table 10.1: Register usage for optimized leaf procedures

Beyond the registers used to pass parameters to the leaf procedure, there are a few other conventions in Table 10.1 that are worth noting. Register %r1 can always be used as a temporary register and the caller cannot assume that it will retain its value across a call to a leaf procedure. We have frequently used this register in the sethi instructions used with the load and store instructions, and you can continue to do this in leaf procedures. The stack pointer is stored in %r14. We will discuss the use of the stack pointer when we consider stack based calling conventions in Lab 12. For the moment, note that a leaf procedure should not alter the value stored in register %r14. Finally, note that the return address (actually, the address of the call instruction used to call the leaf procedure) is stored in register %r15. A leaf procedure can alter this register; however, it will be difficult to return to the point of the call if you alter the value in%r15.

An optimized leaf procedure should only alter the values stored in registers %r1 and %r8-%r13. If the leaf procedure requires more local storage than these registers provide, or if the parameters do not fit in these registers, the leaf procedure cannot be implemented as an ``optimized'' leaf procedure. We will discuss the techniques use to implement other types of procedures in the next two labs.

10.3.2 Calling sequence

In assembly language, a procedure is a block of instructions. The first instruction in this block is labeled by the name of the procedure.

Table 10.2 summarizes the operations used to call and return from optimized leaf procedures. Like the branching operations introduced in Lab 3, these operations have a branch delay slot. The call and link operation saves the current value of the PC in %r15, updates the PC, and sets the nPC to the address specified in the call. The retl operation updates the PC and sets to nPC to the contents of %r15 plus eight. The ``plus eight'' is needed to skip over the call instruction and the instruction in the delay slot of the call. Examples 10.1 and 10.2 illustrate the use of these operations.

   table3653
Table 10.2: The call and retl operations


Example:   Write SPARC procedure that prints a NULL terminated string. The address of the string to print will be passed as the first parameter (i.e., in %r8).

        .text
! pr_str - print a null terminated string
!
! Parameters:   %r8 - pointer to string (initially)
!
! Temporaries:  %r8 - the character to be printed
!               %r9 - pointer to string
!

pr_str: mov %r8, %r9 ! we need %r8 for the "ta 1" pr_lp: ldub [%r9], %r8 ! load character cmp %r8, 0 ! check for null be pr_dn nop ta 1 ! print character ba pr_lp inc %r9 ! increment the pointer

pr_dn: retl ! return from leaf procedure nop ! (branch delay)



Example:   Write a SPARC assembly language fragment that calls the procedure presented in Example 10.1.

        .data
str:    .asciz  "Hello, World!$$n"

.text main: set str, %r8 ! setup the first argument call pr_str ! call print string nop ! (branch delay) end: ta 0 ! exit gracefully


10.3.3 Instruction Encodings

We have introduced two new instructions in this lab: call and retl. Figure 10.1 illustrates the format used to encode call instructions. This instruction encoding uses two fields. The first field holds the 2-bit value 01, the second holds a 30-bit displacement for the program counter. This field is encoded in the same fashion as the displacement field for the conditional branch instructions (see Lab 9 for more details).

   figure3681
Figure 10.1: Instruction format for call instructions


Example: Show how the call instruction in the following SPARC assembly code fragment is encoded.
        .text
main:   set     str, %r8        ! setup the first argument
        call    pr_string       ! call print string
        nop                     !   (branch delay)
        ta      0               ! exit gracefully

pr_string: mov %r8, %r9 ! we need %r8 for the "ta 1"

In this case, the target is 3 instructions from the call instruction, so the disp tex2html_wrap_inline5707 field is set to the 30-bit binary encoding of 3.

This leads to the following machine instruction:

picture3706

That is, 0100 0000 0000 0000 0000 0000 0000 0011 in binary, or 0x40000003.


The retl instruction is actually a synthetic instruction that is translated to a jmpl (jump and link) instruction. The jmpl instruction has two operands: an address, and a destination register. The address is similar to the addresses used in the load and store instructions; however, the brackets surrounding the address in the load and store instructions are omitted in the jmpl instruction. When a SPARC processor executes a jmpl instruction, it saves the address of the jmpl instruction in the destination register and sets the next program counter to the address specified in the instruction. Figure 10.2 illustrates the formats used to encode jmpl instructions.

   figure3721
Figure 10.2: Instruction formats for jmpl instructions

The retl instruction is translated to a jmpl instruction with the address set to %r15+8 and the destination register set to %r0. That is, the target of the branch is instruction after the branch delay slot of the call instruction (remember, call instruction saved its address in %r15), and the address of the retl instruction is discarded (saved in %r0).


Example: Show how a retl instruction is encoded.

A retl instruction is translated to the instruction jmpl  %r15+8, %r0. This instruction is encoded using the second format shown in Figure 10.2. Figure 9.3. As such, we must determine the values for the rd, rs tex2html_wrap_inline5421 , and siconst tex2html_wrap_inline4781 fields. The following table summarizes these encodings:

tabular3803

These encodings lead to the following machine instruction:

picture3809

That is, 1000 0001 1100 0011 1110 0000 0000 1000 in binary, or 0x81C3E008.


10.4 Summary

A leaf procedure is a procedure that never calls any other procedure. In this lab we have introduced the SPARC instructions used to write leaf procedures: call and retl. In the next two labs, we will examine more general procedure calling conventions.

10.5 Review Questions

  1. xxx

10.6 Exercises

  1. Write a SPARC assembly language program consisting of of a main program and a procedure, ``pr_octal'', that prints an unsigned integer in octal notation.
  2. Write a SPARC assembly language program consisting of of a main program and a procedure, ``pr_hex'', that prints an unsigned integer in hexadecimal notation.
  3. Write a SPARC procedure, called ``strcmp'', that compares two strings. Your procedure should accept two parameters, s1 and s2, both pointers to NULL terminated strings. Your procedure should return an integer based on the comparison. In particular,

    You should also provide a small driver to test your procedure.

  4. Write a SPARC procedure, called ``strchr'', that returns a pointer to the first occurrence of a character within a string. The first parameter should be a pointer to a string. The second parameter should be the character search for. If the character is not present in the string, the procedure should return NULL (i.e., 0).

    You should also provide a small driver to test your procedure.

  5. Write a procedure to draw a black pixel at an arbitrary (x, y) location on the GX device. The C declaration for the procedure would be:

    void draw_pixel(int x, int y)


next up previous
Next: 11 Register Windows Up: A Laboratory Manual for Previous: 9 The SPARC Instruction

Barney Maccabe
Mon Sep 2 20:51:56 MDT 1996