LESSON 3:

    Loops are one of the basic building blocks in programming, there are many uses for loops, and many ways to implement them.
 
These are the instructions/ideas that will be covered in this lesson:
LABELS
JR
JP
DIFFERENCES BETWEEN JR AND JP
DJNZ
 
LABELS
  Labels are a way to define a certain area in the program.  To define labels, you must first enter the name of the label then a ":".  Here are some examples:
loop:         ; this is a label
  ld a,5
  push af     ;just some stuff
  push bc
  inc a
anotherlabel: ; this is another label
   NOTE: Labels cannot be indented!

back to list of instructions


JR
  The ASM code is always processed sequently, one instruction after the next.  However, if you want to skip to a certain part of the code, or go back and repeat a previous portion you must jump there.  There are 2 different ways to JUMP to another part of code.  JR, and JP.  There are a few differences that are discussed later, but they are VERY IMPORTANT!
  To jump to a certain part of code you must enter JR LABELNAME.  Here are some examples:

  ld a,1      ;a=1
MyLabel:      ;LABEL
  inc a       ;a=a+1
  jr MyLabel  ;jump to LABEL
 
  NOTE: This code will cause an infinite loop.  The code between 'MyLabel' and 'jr MyLabel' will be repeated over and over.

  In order to make jumps ONLY IF certain requirments are met, we use the following conditionals:

  These conditions are based on how the F register is set.  Remeber it is the FLAG register. Here are some examples:
  ld a,5     ;a=5
Loop:        ;My LOOP Label
  dec a      ;a=a-1
  jr nz,Loop ;if A is not zero then jump to Loop.
             ;(When the value of A changes F is ALWAYS updated)
  This loop will execute 5 times, it doesn't do anything yet, but in a few lessons, we'll get it to do somthing cool, don't worry :)

back to list of instructions

JP
  The ASM code is always processed sequently, one instruction after the next.  However, if you want to skip to a certain part of the code, or back to a previous portion you must jump there.  There are 2 different ways to JUMP to another part of code.  JR, and JP.  There are a few differences that are discussed later, but they are VERY IMPORTANT!
  To jump to a certain part of code you must enter JP LABELNAME.  Here are some examples:

  ld a,1      ;a=1
MyLabel:      ;LABEL
  inc a       ;a=a+1
  jp MyLabel  ;jump to LABEL
 
  NOTE: This code will cause an infinite loop.  The code between 'MyLabel' and 'jr MyLabel' will be repeated over and over.

  In order to make jumps ONLY IF certain requirments are met, we use the following conditionals:

  You can already see some differences between JR and JP.  JP alows the use of 4 more conditions! These conditions are based on how the F register is set.  Remeber it is the FLAG register. Here are some examples:
  ld a,5     ;a=5
Loop:        ;My LOOP Label
  dec a      ;a=a-1
  jp p,Loop  ;if A is positive then jump to Loop.
             ;(When the value of A changes F is ALWAYS updated)
  This loop will execute 6 times (a=5,4,3,2,1,0), it doesn't do anything yet, but in a few lessons, we'll get it to do somthing cool, don't worry :)

back to list of instructions


DIFFERENCES BETWEEN JR/JP
  The Main difference bettween the 2 types of jumps (jr, jp) is the JR is a RELETIVE jump.  When translated into machine code it essential says 'jump this many bytes forward/backward', for this reason, it has a limited range.  If you plan on jumping further then the range allowed by JR you  MUST use JP.  JP is an ABSOLUTE jump.  When translated into machine code it basically says 'jump to this location'.  The advantage of JR over JP is both size and speed in the final compiled program.
  Another difference, which we have already seen is that JP allows you to use more conditions.  JR does not support P,M,PO, or PE.  I have not covered all these conditions, but I will in Future lessons/examples.

back to list of instructions


DJNZ
  DJNZ is a very helpful instruction.  It combines the [DEC -> JP nz,label] sequence used so often in loops.

  ld b,15      ;b=15, the number of times for the loop to execute
Loop:
  ; all the cool loop stuff inside of here
  djnz Loop    ;b=b-1, jump is b is NotZero
 NOTE: DJNZ always uses the register B.  It decrements B,  checks if B is nz, if so then jumps to the label.
 NOTE: If the value of B is changed inside your loop code remeber to use PUSH/POP so your value is not destroyed.
  ld a,0
  ld b,15
Loop:
  push bc  ;adds it to the stack
  ld b,4   ;b=4
  add a,b  ;a=a+b, a=a+4
  pop bc   ;gets our value of B back!
  djnz Loop
back to list of instructions
LESSON2      INDEX     LESSON4
This is the end of the lesson, I do not gaurantee any correctness of any statements made above.
© 1997 mindless productions