Section 9.4: A program with a loop (Frame 4)                     [prev][home][next]

The way comparisons are done in most computers is to subtract one number from the other and then test various conditions. Two numbers will be equal only if the difference in the two numbers is 0, i.e. the result of subtraction is 0, in which case the Z condition bit will be set. This is why JZ is used in this program. Here's the skeleton of a while loop that continues execution if x != y:

   0:  WHILE:     LOD   X         ; while (x != y) {
   1:             SUB   Y         ;
   2:             JZ    ENDWHILE  ;
             body of the while loop
  10:             JMP   WHILE     ; }
  11:  ENDWHILE:  etc.

Lines 0, 1 and 2 test if x equals y. If so, a jump beyond the end of the while loop is taken. It would be perfectly feasible to do this another way, although given the fact that there is no JNZ (jump if not Zero) instruction in the CSC-1 computer, this is the shortest code to accomplish this task.