UK Vintage Radio Repair and Restoration Powered By Google Custom Search Vintage Radio Service Data

Go Back   UK Vintage Radio Repair and Restoration Discussion Forum > Specific Vintage Equipment > Vintage Computers

Notices

Vintage Computers Any vintage computer systems, calculators, video games etc., but with an emphasis on 1980s and earlier equipment.

Reply
 
Thread Tools
Old 22nd Jun 2019, 4:50 pm   #21
Slothie
Pentode
 
Join Date: Apr 2018
Location: Newbury, Berkshire, UK.
Posts: 138
Default 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
It's more complicated than you would expect because it sets various flags depending on the type of byte retrieved and subtracts the ASCII offset from numeric digits, but its unclear why Microsoft chose to do it this way rather than just have the code in ROM and a pointer in ZP. It's entirely possible that they just transliterated the code directly from another processor that didn't do indirection [well] and couldn't be bothered to "optimse" it, but it does give the crafty user a way to inject extra code into this critical routine to, for instance, add extra keywords to the command interpreter etc.
Slothie is online now   Reply With Quote
Old 9th Jul 2019, 8:03 am   #22
julie_m
Dekatron
 
Join Date: May 2008
Location: Derby, UK.
Posts: 7,255
Default 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
In case you need context, it's called from a routine doing a bunch of comparisons on arrays of bytes starting at the addresses in fxb, fxb+1 and mvb, mvb+1; and it's doing them out-of-order. Hence the table of offsets indexed by X.
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments.
julie_m is offline   Reply With Quote
Old 9th Jul 2019, 9:30 am   #23
dominicbeesley
Octode
 
dominicbeesley's Avatar
 
Join Date: Nov 2004
Location: Hebden Bridge, West Yorkshire, UK.
Posts: 1,705
Default 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..
dominicbeesley is offline   Reply With Quote
Old 9th Jul 2019, 3:10 pm   #24
julie_m
Dekatron
 
Join Date: May 2008
Location: Derby, UK.
Posts: 7,255
Default 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
I'd still need to INX again sometime if I used offsets+1 in the second LDY -- and bear in mind there must be a conditional branch after the JSR that called this subroutine. The offsets are only 8 bits (actually, they're only 4 bits) but it's not worth unpacking 8 bytes into 16 for this. (The unpacking code would need 4 bytes for the LSR A if using the high nybble, two for the conditional branch around them and two more for the AND &F if using the low nybble -- and that's the saving wiped out before we've even done an LDA or a TAX!) The rest of the project makes very heavy use of bit-packed co-ordinates (12 bit signed X and Y values in 3 bytes); and in some places, uses robbed bits to specify a plot mode -- move, draw, triangle or close. But one byte saved per point plotted (and points saved through the use of "close") is worth it.

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.
julie_m is offline   Reply With Quote
Reply

Thread Tools



All times are GMT +1. The time now is 1:35 am.


All information and advice on this forum is subject to the WARNING AND DISCLAIMER located at https://www.vintage-radio.net/rules.html.
Failure to heed this warning may result in death or serious injury to yourself and/or others.


Powered by vBulletin®
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright ©2002 - 2019, Paul Stenning.