438 OPT, forward referencing and two-pass assembly Take a look at the following program: 10 DIM GAP 50 20 P%=GAP 30 address=&70 40 [ 50 LDA address \ Load accumulator with contents of address 60 CMP #0 \ Compare accumulator contents with zero 70 BEQ zero \ Jump to label if accumulator contents zero 80 STA address+1 \ Store accumulator contents at address+1 90 .zero RTS 100 ] In this program, line 70 is known as a ‘forward reference’, since it refers to a label which doesn’t appear until line 90. If the above program is run, the error message No such variable at line 70 will appear. This is because the assembler has not allocated an address to the label zero yet.
The reason for the error message is that a ‘two-pass assembly’ is required. The first pass allocates addresses to all labels; the second pass can then jump to the correct memory location when a ‘jump to label’ instruction is encountered. A two-pass assembly is controlled using the OPT command, as shown below: 10 DIM GAP 50 20 address=&70 30 FOR pass=0 TO 3 STEP 3 35 P%=GAP 40 [ 45 OPT pass 50 LDA address \ Load accumulator with contents of address 60 CMP #0 \ Compare accumulator contents with zero 70 BEQ zero \ Jump to label if accumulator contents zero 80 STA address+1 \ Store accumulator contents at address+1 90 .zero RTS