LESSON3:

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

back to list of instructions


JR
  The ASM code is always processed sequently, oneinstruction after the next.  However, if you want to skip to a certainpart of the code, or go back and repeat a previous portion you must jumpthere.  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 enterJR 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 willcause an infinite loop.  The code between 'MyLabel' and 'jr MyLabel'will be repeated over and over.

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

  These conditions are based on how the F register isset.  Remeber it is the FLAG register. Here are some examples:
  ld a,5     ;a=5
Loop:        ;MyLOOP Label
  dec a      ;a=a-1
  jr nz,Loop ;if A is not zero then jump toLoop.
            ;(When the value of A changes F is ALWAYS updated)
  This loop will execute 5 times, it doesn't do anythingyet, 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, oneinstruction after the next.  However, if you want to skip to a certainpart 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, andJP.  There are a few differences that are discussed later, but theyare VERY IMPORTANT!
  To jump to a certain part of code you must enterJP 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 willcause an infinite loop.  The code between 'MyLabel' and 'jr MyLabel'will be repeated over and over.

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

  You can already see some differences between JR andJP.  JP alows the use of 4 more conditions! These conditions are basedon how the F register is set.  Remeber it is the FLAG register. Hereare some examples:
  ld a,5     ;a=5
Loop:        ;MyLOOP Label
  dec a      ;a=a-1
  jp p,Loop  ;if A is positive then jumpto Loop.
            ;(When the value of A changes F is ALWAYS updated)
  This loop will execute 6 times (a=5,4,3,2,1,0), itdoesn't do anything yet, but in a few lessons, we'll get it to do somthingcool, 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 translatedinto machine code it essential says 'jump this many bytes forward/backward',for this reason, it has a limited range.  If you plan on jumping furtherthen the range allowed by JR you  MUST use JP.  JP isan ABSOLUTE jump.  When translated into machine code it basicallysays 'jump to this location'.  The advantage of JR over JP is bothsize and speed in the final compiled program.
  Another difference, which we have already seenis that JP allows you to use more conditions.  JR does not supportP,M,PO, or PE.  I have not covered all these conditions, but I willin Future lessons/examples.

back to list of instructions


DJNZ
  DJNZ is a very helpful instruction.  It combinesthe [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 isb is NotZero
 NOTE: DJNZ always usesthe register B.  It decrements B,  checks if Bis nz, if so then jumps to the label.
 NOTE: If the valueof B is changed inside your loop code remeber to use PUSH/POP so your valueis 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 Bback!
  djnz Loop
back to list of instructions
LESSON2     INDEX     LESSON4
This is the end of the lesson, I do not gaurantee any correctnessof any statements made above.