loop: ; this is a labelNOTE: Labels cannot be indented!
ld a,5
push af ;just some stuff
push bc
inc a
anotherlabel: ; this is another label
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=1NOTE: This code will cause an infinite loop. The code between 'MyLabel' and 'jr MyLabel' will be repeated over and over.
MyLabel: ;LABEL
inc a ;a=a+1
jr MyLabel ;jump to LABEL
In order to make jumps ONLY IF certain requirments are met, we use the following conditionals:
ld a,5 ;a=5This 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 :)
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)
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=1NOTE: This code will cause an infinite loop. The code between 'MyLabel' and 'jr MyLabel' will be repeated over and over.
MyLabel: ;LABEL
inc a ;a=a+1
jp MyLabel ;jump to LABEL
In order to make jumps ONLY IF certain requirments are met, we use the following conditionals:
ld a,5 ;a=5This 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 :)
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)
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.
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 executeNOTE: DJNZ always uses the register B. It decrements B, checks if B is nz, if so then jumps to the label.
Loop:
; all the cool loop stuff inside of here
djnz Loop ;b=b-1, jump is b is NotZero
ld a,0back to list of instructions
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