Date : Sun, 13 Mar 2005 20:32:38 +0000 (GMT)
From : "A.Weston" <a.weston2@...>
Subject: Re: Assembler bug
In <URL:news:local.misc> on Sun 06 Mar, Jonathan Graham Harston wrote:
> No, that's not an assembler bug, that's a programmer bug.
>
> If you do:
>
> LDA location
>
> How does the assembler know whether 'location' is a zero-page
> location or a 16-bit location on the first pass? It doesn't! Not
> unless you've defined it before the assembler gets to that line.
> It can't read your mind! It has to assume it's a 16-bit address,
> and so if after that line you do define it to be a zero-page
> address, on the second pass it will assemble it as a zero-page
> access and everything after that location will be one byte out.
> The lesson: Always define zero-page variable *before* you enter
> the assembly.
>
> This will not work:
>
> FOR P=0 TO 1
> P%=&900
> [OPT P*3
> LDA loc:ADC #1:RTS :\ How the hell do I know the size of loc?
> ]
> loc=&80
> NEXT
>
This would return a BASIC error though wouldn't it - not a bug in the
machine code?
> This will work:
>
> loc=&80
> FOR P=0 TO 1
> P%=&900
> [OPT P*3
> LDA loc:ADC #1:RTS :\ Ah! loc is a zero-page address
> ]:NEXT
>
> > when I moved the positions of assembled code about (which are called
> > from BASIC with various zero-page - &70-&8F IIRC - bytes set) they
> > didn't always work.
>
> Beware assembling code to run in zero page! All your forward
> references will be wrong, because on the first pass they will have
> been assembled as 16-bit addresses, viz:
>
> FOR P=1 TO 1
> P%=&0070:REM Let's run in zero page!
> [OPT P*3
> LDA loc1:STA loc2:RTS
> ..loc1:EQUB 0
> ..loc2:EQUB 0
> ]:NEXT
>
I didn't think of assembling code in zero page.
> On the first pass the assembler doesn't know where loc1 and loc2
> are, so assembles them as 16-bit accesses. On the second pass they
> will be correctly assembled as zero-page accesses, but using the
> value of loc1 and loc2 from the *first pass*!
>
> If you do ever assemble code to run in zero page, predefine your
> variables beforehand to an arbitary zero-page value, eg:
>
> loc1=0:loc2=0:REM Ensure these are in zero page
> FOR P=1 TO 1
> P%=&0070:REM Let's run in zero page!
> [OPT P*3
> LDA loc1:STA loc2:RTS
> ..loc1:EQUB 0
> ..loc2:EQUB 0
> ]:NEXT
>
> You may ask why the assembler does something so silly. But what
> else can it do? It can't read your mind!
>
More's the pity ;-)
A.Weston
--
Staffordshire, UK of GB&NI.