Help with a bit of assembly

Discuss all aspects of programming here. From 8-bit through to modern architectures.
User avatar
geraldholdsworth
Posts: 303
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Help with a bit of assembly

Postby geraldholdsworth » Wed Apr 05, 2017 12:14 pm

Hi all,

I have the following code, which I'd like to convert to Pascal (Delphi), but I need to get it straight in my head what the code is actually doing. I've a rough idea of what it is doing overall (using my limited knowledge of 6502 assembler, and referencing the Acorn Electron User Guide), but it's the detailed breakdown I'm needing help with.
Here's the first part. I know that this issues a couple of *FX commands, and copies some code down to &100, then runs it. I'm not totally clear why &FF is put onto the stack to start with.

Code: Select all

2266 A2 FF    LDX #&FF   
2268 9A       TXS       
2269 A9 C8    LDA #&C8   
226B A2 02    LDX #&02   
226D A0 00    LDY #&00   
226F 20 F4 FF JSR &FFF4  *FX200,2
2272 A9 8C    LDA #&8C   
2274 20 F4 FF JSR &FFF4  *FX140,2
2277 A9 66    LDA #&66   
2279 85 02    STA &02   
227B A9 23    LDA #&23   
227D 85 03    STA &03    Put &2366 into locations &02/&03
227F A2 00    LDX #&00   Move &B2 bytes from &228F to &100
2281 BD 8F 22 LDA &228F,X
2284 9D 00 01 STA &0100,X
2287 E8       INX       
2288 E0 B2    CPX #&B2   
228A D0 F5    BNE &2281 
228C 4C 00 01 JMP &0100

Then, the copied code at &100:

Code: Select all

0100 A9 0C    LDA #&0C   
0102 85 01    STA &01   
0104 A0 00    LDY #&00   
0106 84 00    STY &00   
0108 20 2F 01 JSR &012F 
010B 85 04    STA &04   
010D 20 2F 01 JSR &012F 
0110 48       PHA       
0111 29 7F    AND #&7F   
0113 85 05    STA &05 
0115 68       PLA       
0116 30 65    BMI &017D 
0118 10 71    BPL &018B 
011A A5 01    LDA &01   
011C 10 EA    BPL &0108 
011E A2 00    LDX #&00         Issues some VDU commands (stored at &195):
0120 BD 95 01 LDA &0195,X      VDU19,0,0,0;0;0;0
0123 30 07    BMI &012C        VDU19,1,4,0;0;0;0
0125 E8       INX              VDU19,2,2,0;0;0;0
0126 20 EE FF JSR &FFEE        VDU19,3,3,0;0;0;0
0129 4C 20 01 JMP &0120 
012C 4C A0 0C JMP &0CA0      Jump into the code copied to &C00 (from where?)

012F B1 02    LDA (&02),Y      Decode function (what is it doing?)
0131 48       PHA
0132 29 55    AND #&55
0134 0A       ASL A     
0135 85 07    STA &07
0137 68       PLA       
0138 29 AA    AND #&AA
013A 4A       LSR A     
013B 05 07    ORA &07
013D 48       PHA 
013E 98       TYA
013F 91 02    STA (&02),Y   
0141 E6 02    INC &02 
0143 D0 02    BNE &0147
0145 E6 03    INC &03
0147 68       PLA
0148 60       RTS

0149 C9 7A    CMP #&7A   
014B D0 11    BNE &015E 
014D A6 01    LDX &01   
014F E0 58    CPX #&58   
0151 90 0B    BCC &015E 
0153 A2 55    LDX #&55   
0155 A5 00    LDA &00   
0157 29 01    AND #&01   
0159 F0 02    BEQ &015D 
015B A2 AA    LDX #&AA   
015D 8A       TXA       
015E 91 00    STA (&00),Y
0160 E6 00    INC &00   
0162 D0 02    BNE &0166 
0164 E6 01    INC &01   
0166 24 01    BIT &01   
0168 30 10    BMI &017A 
016A A5 04    LDA &04   
016C 38       SEC       
016D E9 01    SBC #&01   
016F 85 04    STA &04   
0171 A5 05    LDA &05   
0173 E9 00    SBC #&00   
0175 85 05    STA &05   
0177 05 04    ORA &04   
0179 60       RTS       
017A A9 00    LDA #&00   
017C 60       RTS
       
017D 20 2F 01 JSR &012F 
0180 85 06    STA &06   
0182 A5 06    LDA &06   
0184 20 49 01 JSR &0149 
0187 D0 F9    BNE &0182 
0189 F0 8F    BEQ &011A
 
018B 20 2F 01 JSR &012F 
018E 20 49 01 JSR &0149 
0191 D0 F8    BNE &018B 
0193 F0 85    BEQ &011A

I'm not too worried about the code which is copied to &C00, but I am interested in what it is doing to the data loaded from &2366 to &7FFF (I think)

Incidentally, this was written for the Acorn Electron, with the data loaded from ADFS, but appears to work just as well on a BBC Master (in BeebEm).

Cheers,

Gerald.
Gerald Holdsworth
Repton Resource Page
www.reptonresourcepage.co.uk

User avatar
Rich Talbot-Watkins
Posts: 1117
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca

Re: Help with a bit of assembly

Postby Rich Talbot-Watkins » Wed Apr 05, 2017 1:10 pm

That's a strange bit of code. So what it looks like it's doing is decompressing a block of data from &2366-&7FFF to &C00-&7FFF.

There are various blocks in the compressed data which either represent a single byte repeated (&017D) or just copied as-is (&018B). The length of each block comes from the two calls to &012F at &0108. There's a special concession made for when the data is being written to screen memory (between &5800-&7FFF). In this case, if the byte is &7A, it is substituted for either &55 or &AA (depending on whether the screen address is odd or even) - this will display a checkerboard type pattern. Combined with the run-length encoding, this becomes a more efficient way to compress a screen whose background has a lot of checkerboard in it (just create a run-length encoded block with &7A as the data).

The routine at &012F seems basically unnecessary - it just swaps bits 0, 2, 4, 6 with bits 1, 3, 5, 7. I guess it's a strange type of obfuscation, but it serves no other purpose (and the data could be obfuscated better than that, as loading it into screen memory will still show up sprite/screen data reasonably clearly).

Which game is this from anyway?

User avatar
geraldholdsworth
Posts: 303
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Re: Help with a bit of assembly

Postby geraldholdsworth » Wed Apr 05, 2017 3:07 pm

Rich Talbot-Watkins wrote:So what it looks like it's doing is decompressing a block of data from &2366-&7FFF to &C00-&7FFF.

I hadn't considered it to being decompressed. Explains why the computer keep throwing a wobbly everytime I ran the routine (with an RTS instead of JMP&CA0) - I just thought it overwrote page &C, and couldn't work out why this would upset everything.
Rich Talbot-Watkins wrote:either represent a single byte repeated (&017D) or just copied as-is (&018B)

How does it tell whether to decompress or copy as is?
Rich Talbot-Watkins wrote:I guess it's a strange type of obfuscation

That's what I thought it might be...just couldn't work out how.
Rich Talbot-Watkins wrote:There's a special concession made for when the data is being written to screen memory (between &5800-&7FFF). In this case, if the byte is &7A, it is substituted for either &55 or &AA (depending on whether the screen address is odd or even) - this will display a checkerboard type pattern.

Makes sense.

Thank you Rich - think I might have enough info to knock something up...well, knock something up well enough for what I need to do.
Rich Talbot-Watkins wrote:Which game is this from anyway?

It's from a version of Repton 2 on the Acorn Electron. It's odd...I have an ssd, which is uncompressed and unencoded, and an adf, which is this one. But if the data is compressed, it kind of makes sense as it was probably written for the Plus 3 with PAGE at &1D00.
Gerald Holdsworth
Repton Resource Page
www.reptonresourcepage.co.uk

User avatar
Rich Talbot-Watkins
Posts: 1117
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca

Re: Help with a bit of assembly

Postby Rich Talbot-Watkins » Wed Apr 05, 2017 3:45 pm

geraldholdsworth wrote:How does it tell whether to decompress or copy as is?

It gets it from the top bit of the unswizzled block length (BPL or BMI at &0116).

geraldholdsworth wrote:
Rich Talbot-Watkins wrote:There's a special concession made for when the data is being written to screen memory (between &5800-&7FFF). In this case, if the byte is &7A, it is substituted for either &55 or &AA (depending on whether the screen address is odd or even) - this will display a checkerboard type pattern.

Makes sense.

And now we know it's from Repton 2, it's fairly clear to see that it uses the checkboard background on the left hand side status:
Image

So that saves the size of the file, which would help when loading from tape. And on disc, as you say, it would allow the file to be loaded without trampling ADFS workspace, and then decompressed to occupy most of the memory.

User avatar
geraldholdsworth
Posts: 303
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Re: Help with a bit of assembly

Postby geraldholdsworth » Wed Apr 05, 2017 5:39 pm

Just noticed that the Superior Collection Volume 3 that Colin posted the other day is also the same as this one.
Gerald Holdsworth
Repton Resource Page
www.reptonresourcepage.co.uk


Return to “programming”

Who is online

Users browsing this forum: No registered users and 1 guest