435 Note that in this addressing mode the Y register is used as the index (X cannot be). The ‘zero page’ and ‘wraparound’ comments given above still apply. An example of its use is shown below. The program shown sets 128 memory locations to &FF, starting at the address contained in locations &80 (low byte) and &81 (high byte): 10 ?&80=&00:?&81=&28 20 DIM GAP 50 30 P%=GAP 40 [ 50 LDY #0 \ set loop index to zero 60 LDA #&FF \ set value to be loaded 70 .loop STA (&80),Y \ ?(&2800+Y)=&FF, base addr. in &80 and &81 80 INY \ Y=Y+1 90 CPY #128 \ end of loop reached?
100 BNE loop \ if not, go to loop 110 RTS 120 ] 130 CALL GAP (Lines 20, 30 and 130 are explained below.) Relative addressing Relative addressing is the addressing mode used by assembly language branch instructions. In the above example, the mnemonic at line 100 is such an instruction. If the condition tested at line 100 is not satisfied, ie if Y=128, the next instruction to be executed will be the one being pointed to by the program counter – the instruction at line 110. If however the condition is satisfied, ie if Y 128, the processor will decrement the program counter so that it points to the insruction labelled by loop , and this instruction will be the next one to be executed.
In the above case, the program counter has to be decremented by 7 (this will become apparent from a careful study of the machine code printed on the screen when the above program is RUN ). The address of the instruction labelled by loop is -7, relative to the address held in the program counter. This relative addressing is carried out by the assembler, and normally goes unnoticied by the programmer. There are, however, limits to the number of bytes which can be jumped forward or back; a branch of up to 127 bytes forward or 128 bytes back from the Program counter value (at the time the branch instruction is being executed) is allowed.
Care should therefore be taken that labels are not too far from the instructions that branch to them, otherwise an ‘Out of range’ error will result. (Remember that each instruction may be 1, 2 or 3 bytes long.)