<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Sun, 06 Mar 2005 20:27:21 +0000
From   : jgh@... (Jonathan Graham Harston)
Subject: Re: Assembler bug

"A.Weston" <a.weston2@...> wrote:
> Here's an interesting bug I read in an old magazine:
> 
> If you define a zero page variable within assembler an listing then the
> assembler will assume this is a two-byte variable on the first pass and
> as a result any subsequent branch instructions will point to the wrong
> place.
 
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 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
 
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!
 
-- 
J.G.Harston - jgh@...                - mdfs.net/User/JGH
Badly formed email is deleted unseen as spam
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>