Chapter 9: Code generation

Conditional Statements .

. Compare instruction: sets the codes without actually computing the value

. Cmp X, Y sets condition codes to positive if X > Y and so on

if X < Y goto Z Cmp X, Y
  CJL Z
   

. maintain a condition code descriptor: tells the name that last set the condition codes

X =Y + Z Mov Y,R0
if X < 0 goto L Add Z, R0
  Mov R0, X
  CJN L

A compare instruction has the property that it sets the condition code without actually computing the value. That is, CMP x, y sets the condition code to positive if x > y, and so on. A conditional jump machine instruction makes the jump if a designated condition <, =, >, =, = or ≠ is met. For example, if x < y goto z could be implemented by

CMP x, y

CJ< z

If we are generating code for a machine with condition codes it is useful to maintain a condition-code descriptor as we generate code. This descriptor tells the name that last set the condition code, or the pair of names compared, if the condition code was last set that way. Thus we could implement

x := y + z

if x < 0 goto z

by

MOV Y,R0

ADD Z, R0

MOV R0, X

CJ< L