Numbers are often organized in columns and rows. Sometimes the data needs to be summed to calculate a total. The indexed method of addressing is ideal for implement- ing this summation. The first location in the list is set as the base address. Each subsequent instruction uses indexed-mode instructions to access the proper data to complete the summation.
Summing a List: UNTIL Method
An example of a summation program is shown in Figures 5.4 and 5.5. The objective of this program is to sum 16 numbers that are stored in sequential memory locations. The sum that is calculated will then be stored at the next memory location immediately following the list. The flowchart and code use the UNTIL method to accomplish this summation.
124
1. What kind of offset is used by all indexed-mode instructions?
2. If the address X register is $0023 and the offset = $62, what is the effective address? 3. Is there ever a situation where an instruction needs more than one effective
address? If so, when?
NOTE: The source code listing for this example and all subsequent examples in the text will include sequential line numbers as the first column of the file. These line numbers are present in all listing files produced by an assembler. They are simply used as unique identifiers for lines of code in the file.
The program starts by clearing the sum. The sum must start at zero, or the first addition would cause an erroneous result. Each number that is retrieved from memory will be added to the previous total already in AccA. The second step is to initialize the base address or the reference. This program uses the X register to hold the base address. It is loaded initially with $0000, which is the address of the first number to be summed. This com- pletes the steps necessary to initialize the program before it begins the actual summation. The summation starts by retrieving the first number from memory and adding it to the initial sum. The next step is to increment the base address and check to see if the
Summing a List Clear SUM Initialize Base Address in Index Register AGAIN Add number to previous sum Increment Base Address No Finished? Store Sum End Yes
0001 *Program to sum a list of numbers 0002 *using index mode - UNTIL method 0003
0004 0100 4f CLRA ;Clear SUM
0005 0101 ce 00 00 LDX #$0000 ;Initialize base address 0006 0104 ab 00 AGAIN ADDA $00,X ;Add number to previous sum 0007 0106 08 INX ;Increment base address 0008 0107 8c 00 10 CPX #$0010 ;Finished?
0009 010a 26 f8 BNE AGAIN ; If NOT, do again! 0010 010c a7 00 STAA $00,X ;Store SUM following list 0011 010e 3f SWI
Figure 5.4 Flowchart to Sum a List of Numbers: UNTIL Method
Figure 5.5 Code to Sum a List of Numbers: UNTIL Method
125
summation is finished. The ADDA instruction will always offset $00 bytes from the base address. This offset is fixed; however, as the base address can change in the X register, this instruction can access a different memory location each time it is executed in the loop.
The program must determine if the summation is finished. Since this program is summing 16 values, it can check to see if the base address has moved 16 locations through memory. The program uses a CPX instruction to compare the value in the X register to the address following the list that is being summed. It checks against this next address because the base pointer is incremented prior to the test. Thus, it always points to the next location to be summed, rather than the last location that was summed. When the value in the X register is less than the address ($0010), the Z flag will be cleared, indicating that the two numbers are not equal. Now the program is ready to perform the conditional branch test.
The conditional BNE instruction on line 9 is used to determine if another pass through the loop is needed. If the base address in the X register is still within the desired range (i.e., less than $0010), the program loops back and continues the summation. If the base address has advanced past the end of the list of numbers to be summed, the program does not continue to loop. The last time through the loop, the base address is equal to the address of the last value to be summed. It is then incremented by the INX instruction so that the base address is one greater than the last address of the list. When the CPX instruction is executed, the base address in the X register will be equal to the address ($0010). The subtraction of the two values by the CPX instruction will result in zero, and the Z flag in the CCR will be set, indicating an equal condition. The branch test for the BNE is “?Z = 0,” which will fail, and the program will fall through to the next instruction. At this point, the sum is stored in the next memory location and the program is complete.
NOTE: The program is terminated on the HC11 EVBU with an SWI instruction. The SWI instruction provides an output to the monitor indicating the execution has been completed. The SWI instruction is described in chapter 10.
Summing a List: WHILE Method
The previous summation example is repeated in this section, except that the WHILE method is used instead of the UNTIL method. The objective of the program is still to sum a list of 16 numbers that are stored in sequential memory locations. The sum that is calculated will then be stored at the next memory location immediately following the list. Figures 5.6 and 5.7 contain the flowchart and the program code for this method. The program starts by clearing the sum and initializing the base address in the same manner as the UNTIL version. AccA will be used for the sum, and the X register will be used for the base address. Consistent with the WHILE method, the first step of the summation in the loop is to perform a test (lines 6 and 7 of file). Remember, the WHILE loop will only be executed while the condition is true. Since this program is summing 16 values, it checks to see if the base address has moved 16 locations through 126
memory. The program uses a CPX instruction to compare the value in the X register to the address following the list ($0010). It checks against this next address because the base pointer is incremented each time through the loop. Thus, it always points to the next location to be summed, rather than the last location that was summed. The summation starts by retrieving the first number from memory and adding it to the initial sum. Then it increments the base address and loops back to the top of the WHILE loop. The last time through the loop, the base address is equal to the address of the last value to be summed. It is then incremented by the INX instruction so that it is equal to the address following the list. When the WHILE loop branches back and executes the CPX instruction, the base address in the X register will be equal to the address ($0010). The subtraction of the two values will result in zero, and the Z flag in
Summing a List Clear Sum Initialize Base Address in Index Register AGAIN Yes Finished? No Add number to previous sum Increment Base Address STORE Store Sum End
Figure 5.6 Flowchart to Sum a List of Numbers: WHILE Method
0001 *Program to sum a list of numbers 0002 *using index mode - WHILE method 0003
0004 0120 4f CLRA ;Clear SUM
0005 0121 ce 00 00 LDX #$0000 ;Initialize base address 0006 0124 8c 00 10 AGAIN CPX #$0010 ;Finished?
0007 0127 27 04 BEQ STORE ; If so, store result 0008 0129 ab 00 ADDA $00,X ;Else, Add number to sum 0009 012b 08 INX ;Increment base address 0010 012c 20 f7 BRA AGAIN ; do again!
0011 012e a7 00 STORE STAA $00,X ;Store SUM following list 0012 0130 3f SWI
Figure 5.7 Code to Sum a List of Numbers: WHILE Method
127
the CCR will be set. The branch test for the BEQ is “?Z = 1,” which will pass, and the program will branch to the STORE section of the code. At this point, the sum is stored in the next memory location and the program is complete.