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

If/then and if/then/else statements have a regular form that can be turned into assembler code mechanically. The code to implement the following statement is given in lines 3 through 9 (assuming that the CNVZ bits were already set by a previous subtraction).

if (x > y)
     x = x - y;
else
     y = y - x;

   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             ;

This segment of code uses an optimization to produce shorter code -- it does not subtract a-b again since the results of this arithmetic operation done earlier are still valid, still in the condition bits.