P DS 104
Q DS 1
R DS 1
CLD
JSR INIT
LDX #31
L1 TXA
PHA
LDA #0
STA Q
LDX #104
L2 TXA
JSR MUL
PHA
LDA Q
PHA
LDA #10
STA Q
LDA P-1,X
JSR MUL
STA R
PLA
ADC Q
STA Q
PLA
ADC R
STX R
ASL R
DEC R
JSR DIV
STA P-1,X
DEX
BNE L2
LDA #10
STA R
LDA #0
JSR DIV
STA P
LDA Q
EOR #48
JSR OUTPUT
PLA
TAX
CPX #31
BNE L3
LDA #46
JSR OUTPUT
L3 DEX
BNE L1
RTS
INIT
LDA #2
LDX #103
I1 STA P,X
DEX
BPL I1
RTS
MUL
STA R
LDA #0
LDY #8
LSR Q
M1 BCC M2
CLC
ADC R
M2 ROR
ROR Q
DEY
BNE M1
RTS
DIV
LDY #8
ASL Q
D1 ROL
BCS D2
CMP R
BCC D3
D2 SBC R
SEC
D3 ROL Q
DEY
BNE D1
RTS
Ohh I see , excellent. Oh well thats now a demo binary for the Symbiosys operating system. I had most trouble finding out the SYM1 RAE rol and ror work on the accumulator.
Believe it or not, the program just converts from one base to another.
BigDumbDinosaur wrote:
A comment would die from sheer loneliness in that code.
In this case, it was intentionally uncommented. (Everything else I've written is uncommented because I didn't feel like it .) I intended for it to be in the spirit of the old BASIC one and two liner programs that you'd see in magazines back in the day -- short, cryptic, a little mysterious, and hopefully amusing. I wanted people to see what it did by trying it rather than looking at the code.
Ok, will be a brat and post a library. The pinball I threw some light code. All the routines arent tested yet, but some do work, I just forget which ones. Enjoy!
It takes a few minutes to produce the first output character on visual6502 - I've arranged it so the working variables are in the first few lines of memory and therefore visible. You'll need to press the FastForward button to run the simulation at reasonable speed. The program is located at &1372 because I assembled it on a BBC emulator and that's where it landed.
There's a (somewhat mathematical) paper here explaining the algorithm - which also gives us a clue as to how far we can get with 8-bit bytes, and how much further we might get with 16-bit bytes. The mul and div routines would need some 8-counts adjusting to 16, at least.
Crikey! Cheers Ed! I don't think I'd've had a chance in hell of reverse engineering that and getting any closer to how it worked.
Very clever though... and a technique I've never heard of before.
And there was me thinking that it must use some previously unknown secret feature of the 6502 that made it start doing arithmetic in base pi (bit 5 of the status register for example...)
I wouldn't have found that paper either, if it weren't for some happy coincidences and a very long-running conversation with a friend I see about every ten years! I didn't do any reversing, just some baffled gazing and some battling with an emulator which, it turns out, was working perfectly but just not bothering to flush its output. I recognised the paper as the right stuff when I saw the line of 2's.
I notice, I think, that Bruce cut off the output just before the point where the algorithm needs an extra wrinkle. Which is only fair - he had a deadline!
Jeremy Gibbons has more ideas on this topic, but it's a bit lofty.