Is this buggy code?

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
rea5245
Posts: 6
Joined: 22 Sep 2019

Is this buggy code?

Post by rea5245 »

I have a SYM-1. It has a feature where it can draw text on an oscilloscope, and I'm trying to get it to work. I'm using the sample code from the manual, and I think it's buggy. But since I'm not experienced in 6502 programming, I was hoping someone could double check me.

I've attached a snapshot of the code below.

I think that the relative branch at line 0032 is wrong - it's going to jump into the middle of the instruction on line 0030. Likewise, the branch at line 0064 is wrong: it should probably be BNE *+5.

Am I right about this?

And a follow up question... in order to use this code, I need to alter some vectors that the SYM-1 monitor uses to display text. The manual explains how to do that manually from the keyboard. But if I wanted to do it programmatically, how would I? On a 16 bit processor, I'd load a register with the address of the code, then store that address in the vector location. E.g.:
LD D0,#TARGET
ST D0,VECTORLOCATION

On the 6502, I imagine I need to load the low byte of the target address, save it in the low byte of the vector, then repeat with the high byte. I'd like to do something like:
LDA #TARGET
STA VECTORLOCATION
LDA #TARGET>>8
STA VECTORLOCATION+1

But the SYM-1's assembler doesn't support shifting in expressions. It only allows addition and subtraction. Is there an elegant way to do this, or do I just need to load hexadecimal numbers, instead of using labels?

Thank you.
Attachments
oscope.png
Martin A
Posts: 197
Joined: 02 Jan 2016

Re: Is this buggy code?

Post by Martin A »

The code looks fine to me.

The jump into the middle of the LDX #$EA at line 0030 works because constant $EA is also the opcode for NOP. The actual delay code is

Code: Select all

NOP
DEX
BNE nop
The other the code assembled at line 0064 is D0 02, it jumps over the LDX #$CC at line 0065
With more lables in the assembler code you'd be looking at

Code: Select all

        LSR A
        BCS LIGHT
DARK:   LDX #&EC
        BNE COMMON ; branch always
LIGHT:  LDX #$CC
COMMON: STX PCR3
John West
Posts: 383
Joined: 03 Sep 2002

Re: Is this buggy code?

Post by John West »

rea5245 wrote:
I think that the relative branch at line 0032 is wrong - it's going to jump into the middle of the instruction on line 0030. Likewise, the branch at line 0064 is wrong: it should probably be BNE *+5.
That first branch is jumping into the middle of the instruction, but that's correct. The operand byte is $EA, which is a NOP instruction. By branching to this, they get an extra two cycles per iteration without making the code any longer. That sort of trickery was a lot more common in the early days, when memory was small and expensive.

The second branch just looks correct to me. In "*+4", the * refers to the address of this instruction, which is $0551. So *+4 is $0555, which is the STX instruction. It's loading X with a value, then jumping over the instruction that loads X with a different value.

They've missed an opportunity to save another byte here: if you replace BNE *+4 with .byte $2C, then the following LDX #$CC becomes the operand of a BIT instruction, and won't be executed as an LDX. That was also a common trick in the old days.
Quote:
LDA #TARGET
STA VECTORLOCATION
LDA #TARGET>>8
STA VECTORLOCATION+1
I don't know the SYM-1's assembler, but the usual syntax for this is

Code: Select all

    LDA #>TARGET
    STA VECTORLOCATION
    LDA #<TARGET
    STA VECTORLOCATION+1
The < and > operators give the high and low bytes of their argument respectively.
rea5245
Posts: 6
Joined: 22 Sep 2019

Re: Is this buggy code?

Post by rea5245 »

Thank you all. It's a good thing I asked. :-)
Post Reply