4.4 HEXNUM: Using Arithmetic Instructions

We now illustrate Itanium arithmetic instructions with a brief program (Figure 4-3), which, like our program for computing a table of squares, consists of a simple sequence of instructions without branching. Suppose that we have stored three hexadecimal digits in three quad words starting at address H1. Further suppose that we want to combine these digits into a single value in register r20.

Figure 4-3 HEXNUM: An illustration of arithmetic instructions
 // HEXNUM           Number Conversion // This program will convert the positive number expressed // by 3 digits in hexadecimal stored as quad words beginning // at H1 into a value in register r20 for inspection.          .data                    // Declare storage          .align  8                // Desired alignment H1:      data8   0x3,0xa,0x2      // Interpret as "3a2"          .text   // Section for code          .align  32               // Desired alignment          .global main             // These three lines          .proc   main             //  mark the mandatory main:                             //   'main' program entry          .body                    // Now we really begin... first:   mov     r20 = 0;;        // r20 = value computed          addl    r14 = @gprel(H1),gp;;  // Point to storage          ld8     r21 = [r14];;    //       for 1st hex digit          shladd  r20 = r20,4,r21;; // Value = 16*prev + next          add     r14 = 8,r14;;    // Advance input pointer          ld8     r21 = [r14];;    //  to 2nd hex digit          shladd  r20 = r20,4,r21;; // Value = 16*prev + next          add     r14 = 8,r14;;    // Advance input pointer          ld8     r21 = [r14];;    //  to 3rd hex digit          shladd  r20 = r20,4,r21;; // Value = 16*prev + next                                   // We could continue... done:    mov     r8 = 0;;         // Signal all is normal          br.ret.sptk.many b0      // Back to command line          .endp   main             // Mark end of procedure 

An algorithm to accomplish this task just needs to apply the weighting scheme that we outlined in Chapter 1, using successive powers of the radix. The most significant digit is multiplied by the radix, the next most significant digit is added, and this process is continued multiplying the intermediate result by the radix and then adding the value of the next digit until all the digits have been processed. For the necessary multiplication of values by 16, we use an Itanium shift left and add (shladd) instruction.

In the SQUARES program, we used the Itanium st8 (quad word store) instruction without full explanation. The HEXNUM program similarly introduces the Itanium ld8 (quad word load) instruction, which is explained later in this chapter.

The program defines the symbolic address H1 where the sequence of values of hexadecimal digits is located in the data section. Register r14 is chosen to be a moving pointer to that list of values, first computed relative to the global pointer gp and subsequently incremented by the explicit addition of eight address units at each stage to point to the next quad word storage unit. The assembler notation [r14] indicates that the processor will use the current contents of r14 as the address pointer for each ld8 operation.

Register r20 is chosen as the accumulator for this calculation. As the initial contents of r20 are unknown, it must be initialized to zero before being used as an accumulator.

On a Linux system, we can stop the program at the label done and inspect the contents of register r20 using the commands:

 L> gcc -Wall -O0 -o bin/hexnum hexnum.s L> gdb bin/hexnum [messages deleted here] (gdb) break done Breakpoint 1 at 0x4000000000000570 (gdb) run Starting program: /home/user/bin/hexnum Breakpoint 1, 0x4000000000000570 in done () (gdb) p/d $r20 $1 = 930 (gdb) q The program is running.  Exit anyway? (y or n) y L> 

We obtained the anticipated result, since 3x162 + 10x161 + 2x160 = 930 (decimal).



ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net