next up previous
Next: 6 Traps Up: 5.3 Assembly Language Examples Previous: 5.3.2 Loops

5.3.3 Conditionals

``If'' statements are also done by using JMP instructions. Multiple conditions can be tested by using several JMP instructions. Here are some examples of how to do ``if'' statements:

Pascal:

  IF (x = 10) 
   THEN
    BEGIN
      ...    (* then part 1 *)
    END
   ELSE
    BEGIN
      ...    (* else part 1 *)
    END;

  IF ((x<>10) AND (y>20))
   THEN
    BEGIN
      ...    (* then part 2 *)
    END;

C++:

  if (x == 10) {
    ...      // then part 1
  }
  else {
    ...      // else part 1
  }

  if ((x!=10) && (y>20)) {
    ...      // then part 2
  }

Assembly:

  LOAD r1, X      ; load the variable x into r1
  SET r2, 10      ; set r2 to immediate value 10
  CMP r1, r2
  JMP NEQ, ELSE1  ; if r1 <> r2, then jump to the else part, otherwise do the then part
  ...        ; then part 1
  JUMP END1        ; jump over the else part
ELSE1:
  ...        ; else part 1
END1:

  LOAD r1, X      ; load the variable x into r1
  SET r2, 10      ; set r2 to immediate value 10
  CMP r1, r2
  JMP NEQ, TEST2  ; if x <> 10 go on to next test
  JUMP END2       ; otherwise skip past then part 2
TEST2:
  LOAD r1, Y      ; load the variable y into r1
  SET r2, 20      ; set r2 to immediate value 20
  CMP r1, r2
  JMP GT, THEN2   ; if y > 20 go on to then part 2
  JUMP END2       ; otherwise skip past
THEN2:
  ...        ; then part 2
END2:



James Clingenpeel
Thu Feb 29 18:00:38 MST 1996