|
Vintage Computers Any vintage computer systems, calculators, video games etc., but with an emphasis on 1980s and earlier equipment. |
|
Thread Tools |
22nd Jun 2019, 4:50 pm | #21 |
Octode
Join Date: Apr 2018
Location: Newbury, Berkshire, UK.
Posts: 1,287
|
Re: Fun with 6502 Assembler
The BASIC on the commodore PET (and probably most others with Microsofts 6502 BASIC) has a small routine copied into zero page RAM that gets the next byte of the program code:
. Code:
C:00c2 E6 C9 INC $C9 .C:00c4 D0 02 BNE $00C8 .C:00c6 E6 CA INC $CA .C:00c8 AD 00 04 LDA $0400 .C:00cb C9 3A CMP #$3A .C:00cd B0 0A BCS $00D9 .C:00cf C9 20 CMP #$20 .C:00d1 F0 EF BEQ $00C2 .C:00d3 38 SEC .C:00d4 E9 30 SBC #$30 .C:00d6 38 SEC .C:00d7 E9 D0 SBC #$D0 .C:00d9 60 RTS |
9th Jul 2019, 8:03 am | #22 |
Dekatron
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
|
Re: Fun with 6502 Assembler
Spot the schoolgirl error:
Code:
.dotest LDY offsets,X LDA (fxb),Y INX LDY offsets,X CMP (mvb),Y PHP \ save C,N,Z flags INX \ will trash comparison result PLP \ restore real result RTS
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments. |
9th Jul 2019, 9:30 am | #23 |
Octode
Join Date: Nov 2004
Location: Hebden Bridge, West Yorkshire, UK.
Posts: 1,885
|
Re: Fun with 6502 Assembler
Move the INX before the CMP?
Other comments: Could use offsets+1 in the second LDY Could split offsets table in to a low byte and hi byte table then only one inx needed. I've been doing a lot of 6502 assembler recently. Mainly to allow me to load operating system and basic rom images into my frankenstein bbc micro with an optional 6809, z80 or 68008 main processor. I prefer 6809 coding usually but the limitations of the 6502 often force the use of cunning to get the job done. I had a fun few hours yesterday squeezing the flash rom programming routines down to a single page.. |
9th Jul 2019, 3:10 pm | #24 |
Dekatron
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
|
Re: Fun with 6502 Assembler
Ding, ding, we have a winner! This is 2 bytes shorter and 7 cycles faster:
Code:
.dotest LDY offsets,X INX LDA (fxb),Y LDY offsets,X INX CMP (mvb),Y RTS Splitting the table of offsets would be a slap in the face for anybody trying to reverse-engineer the code in future. (I do plan to publish the Source Code, but you never know .....) So I'm holding off that technique unless and until the code ever gets one byte the wrong side of running on a model B!
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments. |
27th Jul 2019, 3:54 pm | #25 |
Dekatron
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
|
Re: Fun with 6502 Assembler
If you have a number of pointers stored in Zero Page, each representing the base address of a fixed-size record up to 256 bytes, you probably use something like
Code:
LDA (pcb),Y Now to move on to the next vertex, we need to add 3 to value stored at pcb and pcb+1; and we need to add 4 to the value stored at acb and acb+1. Naïvely, we might write Code:
.next_vertex CLC LDA pcb ADC #3 STA pcb LDA pcb+1 ADC #0 STA pcb+1 CLC LDA acb ADC #4 STA acb LDA acb+1 ADC #0 STA acb+1 RTS Code:
.next_vertex LDX #pcb CLC JSR add_three LDX #acb .add_four SEC \ this will add an extra one .add_three LDA #3 .add_A_X ADC 0,X STA 0,X LDA #0 ADC 1,X STA 1,X RTS Note that we don't need to increase X to deal with the high byte; we just offset from 1 instead. Other pointers at different addresses in ZP probably will have different record sizes; so we just need a construct like Code:
.next_shape LDX #shp \ shape pointer LDA #23 \ each "shape" record is 23 bytes long CLC BCC add_A_X \ saves over JSR followed by RTS
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments. |
29th Jul 2019, 11:04 am | #26 |
Octode
Join Date: Nov 2004
Location: Hebden Bridge, West Yorkshire, UK.
Posts: 1,885
|
Re: Fun with 6502 Assembler
shave another couple of bytes and speed up with an early exit
Code:
ADC #xx STA 0,X BCC ex INC 1,X ex: RTS |