5.8 MAXIMUM: Using Conditional Instructions

From this point forward, virtually all of our example programs contain loops. We illustrate various techniques for the initialization and control of loops in those examples, and suggest other ways for your consideration through the exercises.

FORTRAN has an intrinsic function which can take as its input an indefinitely long list of numbers and then return as its computed result the most positive value from that list. Certain other high-level languages, including some implementations of C, may lack such a function. Historically, providing such capabilities to a high-level language was one of the common applications of assembly-language programming.

Figure 5-3 presents MAXIMUM, a program that places into register r8 the maximum value encountered during a scan of a list of integer values stored as quad words. We assume that we do not know the total number of values to be searched, but instead use the label endnum, which is just past the final value, as a limiting address. An address pointer (register r14) is incremented by 8 bytes (one quad word) each time through the loop until it reaches the limit (endnum) stored in register r15.

Before we allow this program to enter its loop, we need to initialize the comparison value to the most negative number that can be represented in 64 bits. Since we anticipate finding an algebraically greater value, we load a quad word value (symbol NEG) into register r8 as the first target to compare against.

When the program runs, each number in the list is loaded into register r9 and compared against the most recently found largest value in register r8. If the number in register r9 is larger, it replaces the value in register r8 and becomes the new largest value. At done, register r8 should then contain 0x63 (99), which was the most positive value in our list stored at num.

When designing or analyzing a loop, you should always be able to answer two questions: Why does this loop advance? What makes this loop stop? The loop in MAXIMUM advances because the ld8 instruction postincrements its fetch pointer. This loop stops as soon as that fetch pointer coincides with a fixed pointer set at the quad word address endnum that is just beyond the information unit containing the last quad word value in the list.

Figure 5-3 MAXIMUM: An illustration of conditional instructions
 // MAXIMUM       Scalar Product of N-vectors // This program locates the algebraically largest number // in the list of quad word values at 'num'.          NEG = 0x8000000000000000 // Most negative number          .data                    // Declare storage          .align  16               // Desired alignment num:      data8  -1,+3,+5,-77,+99,0,-17 endnum:                           // Next (unused) location          .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:  movl    r8  = NEG         // r8 = largest thus far         movl    r14 = num         // r14 -> current element         movl    r15 = endnum;;    // r15 -> end of list again:  cmp.geu p6,p0 = r14,r15   // When r14 >= r15         (p6) br.cond.spnt.few done  // we're done         ld8     r9 = [r14],8;;    // r9 = number to assess         cmp.gt  p7,p0 = r9,r8;;   // If new > previous    (p7) mov r8 = r9               //  remember new maximum         br.cond.sptk.few again    // Look for next element done:                             // Largest in r8 now         br.ret.sptk.many b0;;     // Back to command line         .endp   main              // Mark end of procedure 

Loop control in MAXIMUM is structured with the test at the top, much like a whiledo construct in a high-level language. When comparing addresses instead of values, it is essential to use an unsigned comparison, as emphasized previously (Section 5.2.2). If one were willing to forgo a check for lack of data, then the loop control could be restructured with the test at the bottom, much like a dountil construct. If the presence of some data were known, there would only need to be one branch instruction at the bottom leading back to the ld8 instruction. You should think about how that revision might speed up the program.

In a later chapter, we will show how to adapt routines like MAXIMUM to conform to standard conventions for procedure calls in high-level languages.



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