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

The next program we will look at is a version of Euclid's algorithm for finding the greatest common divisor (gcd) of two integers. Here's the .lis file:

   0:  ;
   0:  ;  This algorithm is Euclid's algorithm for computing
   0:  ;  the GCD of two ints
   0:  ;
   0:  WHILE:     LOD   X         ; while (x != y) {
   1:             SUB   Y         ;
   2:             JZ    ENDWHILE  ;
   3:  IF:        JN    ELSE      ;      if (x > y)
   4:  THEN:      STD   X         ;           x = x - y;
   5:             JMP   ENDIF     ;
   6:  ELSE:      LOD   Y         ;      else
   7:             SUB   X         ;
   8:             STD   Y         ;           y = y - x;
   9:  ENDIF:     NOP             ;
  10:             JMP   WHILE     ; }
  11:  ENDWHILE:  HLT             ; /* stop */
  12:
  12:  =1000
1000:  X:         NUM   18        ; int x;
1001:  =2000
2000:  Y:         NUM   24        ; int y;