CHAPTER SIX

SEARCHING

6.1. SEQUENTIAL SEARCHING

[397]

Program S (Sequential search). Assume that the keys Ki are stored as an array of octabyte values.

The following subroutine has three parameters: key LOC(K1); n N, the number of keys; and k K, the key we want to find. After a successful search, the subroutine returns the location of the key found; otherwise, it returns zero. For efficiency, register i is scaled by 8, the size of the table entries. Further, we subtract 8N, the table size, from i and add it to key. With this trick, we can replace the test iN by 8(iN) < 0 and control the loop with a single PBN instruction.

01:SearchSLi,n,31S1. Initialize.
02NEGi,i1i –8N, i 1.
03SUBUkey,key,i1key LOC(KN + 1).
04S2

Get The MMIX Supplement: Supplement to The Art of Computer Programming Volumes 1, 2, 3 by Donald E. Knuth now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.