Information Technology Reference
In-Depth Information
Figure 2.13
SORT subroutine
as well as the richness of the instruction set of the machine used. Note also that the
use of the autoincrement addressing in Example 2 has led to a decrease in the
number of instructions used to perform the same task.
Example 3
This example illustrates the use of a subroutine, SORT, to sort
N values in ascending order (Fig. 2.13). The numbers are originally stored in a
list starting at location 1000. The sorted values are also stored in the same list
and again starting at location 1000. The subroutine sorts the data using the well-
known “Bubble Sort” technique. The content of register R 3 is checked at the end
of every loop to find out whether the list is sorted or not.
Example 4
This example illustrates the use of a subroutine, SEARCH, to search
for a value VAL in a list of N values (Fig. 2.14). We assume that the list is not orig-
inally sorted and therefore a brute force search is used. In this search, the value VAL
is compared with every element in the list from top to bottom. The content of register
R 3 is used to indicate whether VAL was found. The first element of the list is located
at address 1000.
Example 5
This example illustrates the use of a subroutine, SEARCH, to search
for a value VAL in a list of N values (as in Example 4) (Fig. 2.15). Here, we make use
of the stack to send the parameters VAL and N.
Search WWH ::




Custom Search