LESSON 4:

    If.. Then.. Else statements allow the program to branch off in many different directions.  ASM does not support these staments directly, but using CP along with JR/JP you can acheive the effect.
 
These are the instructions/ideas that will be covered in this lesson:
CP
IF... THEN... ELSE
 
CP
  CP simply sets the FLAG register (F) based on the subraction (A-s) back to list of instructions

 IF... THEN... ELSE
  To acheive an IF... THEN... ELSE statement(s) you must use a CP call followed by a JR, JP, or any other instruction that has conditionals (ret, call, ...)  Here are some examples:
  ld b,5        ;b=5
  ld a,5        ;a=5
 
  cp b          ;set flags based on (a-b)
  jr z,AequalsB ;if a=b -> a-b=0, therefore
                ;if the Zero flag is set JUMP to AequalsB
  :
  .
AequalsB: ;
  :
  .
  You can also string them together like this:
  ld b,6  ;.
  ld c,5  ;.
  ld a,5  ;Set the init values
 
  cp b    ;IF a=b
  jr z,AB ;THEN goto AB
  cp c    ;IF a=c
  jr z,AC ;THEN goto AC
  jr NONE ;ELSE goto NONE (if it didn't jump one of the other
          ; times then it must be an ELSE, NOTE:no conditia
          ; listed.  Just a straight 'jr NONE')
:
.
AB:

:
.
AC:
:
.
NONE:

  You've seen how to test for equality.  now for GREATER THAN, and LESS THAN:
ld b,7     ;.
ld a,3     ;Set up values

cp b               ;set flags based on (A-B)
jp p,AGreaterThanB ;IF (a-b) is positive then a>b
jp m,BGreaterThanA ;IF (a-b) is minus (negative) then b>a
jp AEqualsB        ;ELSE a=b
:
:
.

back to list of instructions
LESSON3      INDEX     LESSON5
This is the end of the lesson, I do not gaurantee any correctness of any statements made above.
© 1997 mindless productions