Difference between revisions of "Disassembler"

From BeebWiki
Jump to: navigation, search
m (1 revision)
m (1 revision)
(No difference)

Revision as of 01:13, 8 March 2015

Here is a simple disassembler program - it is rather slow and long - and is not optimised, but it works reasonably well.

   10 REM Disassemble M/C Routine
   20 MODE 128:S%=0:F%=0
   30 ONERRORIFERL=40 GOTO 40 ELSE IFERL=50 GOTO50 ELSE IF ERL=60 OR ERL=30 INPUT"Bad Filename - Please Re-enter: "f$:CLS:OSCLI"*SPOOL "+f$:GOTO70
   40 PRINT''"Start Address : ";:INPUT""a$:S%=EVAL(a$)
   50 PRINT'"Finish Address : ";:INPUT""a$:F%=EVAL(a$)
   55 *FX229,1
   60 PRINT'"Save to File? (Y/N)":REPEATA$=GET$:UNTILA$="Y"ORA$="N":IFA$="Y":INPUT'"Enter Filename: "f$:CLS:OSCLI"*SPOOL "+f$
   65 *FX229,0
   70 ONERROROFF:IFF%=0 F%=&FFFFFFF
   90 CLS
  100 IFA$="Y" ON ERROR IF ERR=17:OSCLI"*SPOOL":PRINT':REPORT:END
  110 FORI%=S% TO F%
  120 W%=?I%
  130 GOSUB180
  140 NEXT
  150 IFA$="Y":OSCLI"*SPOOL"
  160 END
  180 IF W%=&69 PROCPNT("ADC#",100):RETURN
  190 IF W%=&65 PROCPNT("ADC ",100):RETURN
  200 IF W%=&75 PROCPNT("ADC ",101):RETURN
  210 IF W%=&6D PROCPNT("ADC ",200):RETURN
  220 IF W%=&7D PROCPNT("ADC ",201):RETURN
  230 IF W%=&79 PROCPNT("ADC ",202):RETURN
  240 IF W%=&61 PROCPNT("ADC ",203):RETURN
  250 IF W%=&71 PROCPNT("ADC ",105):RETURN
  260 IF W%=&29 PROCPNT("AND#",100):RETURN
  270 IF W%=&25 PROCPNT("AND ",100):RETURN
  280 IF W%=&35 PROCPNT("AND ",101):RETURN
  290 IF W%=&2D PROCPNT("AND ",200):RETURN
  300 IF W%=&3D PROCPNT("AND ",201):RETURN
  310 IF W%=&39 PROCPNT("AND ",202):RETURN
  320 IF W%=&21 PROCPNT("AND ",103):RETURN
  330 IF W%=&31 PROCPNT("AND ",104):RETURN
  340 IF W%=&0A PROCPNT("ASL ",2):RETURN
  350 IF W%=&06 PROCPNT("ASL ",100):RETURN
  360 IF W%=&16 PROCPNT("ASL ",101):RETURN
  370 IF W%=&0E PROCPNT("ASL ",200):RETURN
  380 IF W%=&1E PROCPNT("ASL ",201):RETURN
  390 IF W%=&30 PROCPNT("BMI ",102):RETURN
  400 IF W%=&D0 PROCPNT("BNE ",102):RETURN
  410 IF W%=&10 PROCPNT("BPL ",102):RETURN
  420 IF W%=&00 PROCPNT("BRK ",1):RETURN
  430 IF W%=&90 PROCPNT("BCC ",102):RETURN
  440 IF W%=&B0 PROCPNT("BCS ",102):RETURN
  450 IF W%=&F0 PROCPNT("BEQ ",102):RETURN
  460 IF W%=&24 PROCPNT("BIT ",100):RETURN
  470 IF W%=&2C PROCPNT("BIT ",200):RETURN
  480 IF W%=&50 PROCPNT("BVC ",102):RETURN
  490 IF W%=&70 PROCPNT("BVS ",102):RETURN
  500 IF W%=&18 PROCPNT("CLC ",1):RETURN
  510 IF W%=&D8 PROCPNT("CLD ",1):RETURN
  520 IF W%=&58 PROCPNT("CLI ",1):RETURN
  530 IF W%=&B8 PROCPNT("CLV ",1):RETURN
  540 IF W%=&C9 PROCPNT("CMP#",100):RETURN
  550 IF W%=&C5 PROCPNT("CMP ",100):RETURN
  560 IF W%=&D5 PROCPNT("CMP ",101):RETURN
  570 IF W%=&CD PROCPNT("CMP ",200):RETURN
  580 IF W%=&DD PROCPNT("CMP ",201):RETURN
  590 IF W%=&D9 PROCPNT("CMP ",202):RETURN
  600 IF W%=&C1 PROCPNT("CMP ",103):RETURN
  610 IF W%=&D1 PROCPNT("CMP ",105):RETURN
  620 IF W%=&E0 PROCPNT("CPX#",100):RETURN
  630 IF W%=&E4 PROCPNT("CPX ",100):RETURN
  640 IF W%=&EC PROCPNT("CPX ",200):RETURN
  650 IF W%=&C0 PROCPNT("CPY#",100):RETURN
  660 IF W%=&C4 PROCPNT("CPY ",100):RETURN
  670 IF W%=&CC PROCPNT("CPY ",200):RETURN
  680 IF W%=&C6 PROCPNT("DEC ",100):RETURN
  690 IF W%=&D6 PROCPNT("DEC ",101):RETURN
  700 IF W%=&DE PROCPNT("DEC ",201):RETURN
  710 IF W%=&CA PROCPNT("DEX ",1):RETURN
  720 IF W%=&88 PROCPNT("DEY ",1):RETURN
  730 IF W%=&49 PROCPNT("EOR#",100):RETURN
  740 IF W%=&45 PROCPNT("EOR ",100):RETURN
  750 IF W%=&55 PROCPNT("EOR ",101):RETURN
  760 IF W%=&4D PROCPNT("EOR ",200):RETURN
  770 IF W%=&5D PROCPNT("EOR ",201):RETURN
  780 IF W%=&59 PROCPNT("EOR ",202):RETURN
  790 IF W%=&41 PROCPNT("EOR ",103):RETURN
  800 IF W%=&51 PROCPNT("EOR ",105):RETURN
  810 IF W%=&E6 PROCPNT("INC ",100):RETURN
  820 IF W%=&F6 PROCPNT("INC ",101):RETURN
  830 IF W%=&EE PROCPNT("INC ",200):RETURN
  840 IF W%=&FE PROCPNT("INC ",201):RETURN
  850 IF W%=&E8 PROCPNT("INX ",1):RETURN
  860 IF W%=&C8 PROCPNT("INY ",1):RETURN
  870 IF W%=&4C PROCPNT("JMP ",200):RETURN
  880 IF W%=&6C PROCPNT("JMP ",205):RETURN
  890 IF W%=&20 PROCPNT("JSR ",200):RETURN
  900 IF W%=&A9 PROCPNT("LDA#",100):RETURN
  910 IF W%=&A5 PROCPNT("LDA ",100):RETURN
  920 IF W%=&B5 PROCPNT("LDA ",101):RETURN
  930 IF W%=&AD PROCPNT("LDA ",200):RETURN
  940 IF W%=&BD PROCPNT("LDA ",201):RETURN
  950 IF W%=&B9 PROCPNT("LDA ",202):RETURN
  960 IF W%=&A1 PROCPNT("LDA ",103):RETURN
  970 IF W%=&B1 PROCPNT("LDA ",105):RETURN
  980 IF W%=&A2 PROCPNT("LDX#",100):RETURN
  990 IF W%=&A6 PROCPNT("LDX ",100):RETURN
 1000 IF W%=&B6 PROCPNT("LDX ",106):RETURN
 1010 IF W%=&AE PROCPNT("LDX ",200):RETURN
 1020 IF W%=&BE PROCPNT("LDX ",202):RETURN
 1030 IF W%=&A0 PROCPNT("LDY#",100):RETURN
 1040 IF W%=&A4 PROCPNT("LDY ",100):RETURN
 1050 IF W%=&B4 PROCPNT("LDY ",101):RETURN
 1060 IF W%=&AC PROCPNT("LDY ",200):RETURN
 1070 IF W%=&BC PROCPNT("LDY ",201):RETURN
 1080 IF W%=&4A PROCPNT("LSR ",2):RETURN
 1090 IF W%=&46 PROCPNT("LSR ",100):RETURN
 1100 IF W%=&56 PROCPNT("LSR ",101):RETURN
 1110 IF W%=&4E PROCPNT("LSR ",200):RETURN
 1120 IF W%=&5E PROCPNT("LSR ",201):RETURN
 1130 IF W%=&EA PROCPNT("NOP ",1):RETURN
 1140 IF W%=&09 PROCPNT("ORA#",100):RETURN
 1150 IF W%=&05 PROCPNT("ORA ",100):RETURN
 1160 IF W%=&15 PROCPNT("ORA ",101):RETURN
 1170 IF W%=&0D PROCPNT("ORA ",200):RETURN
 1180 IF W%=&1D PROCPNT("ORA ",201):RETURN
 1190 IF W%=&19 PROCPNT("ORA ",202):RETURN
 1200 IF W%=&01 PROCPNT("ORA ",103):RETURN
 1210 IF W%=&11 PROCPNT("ORA ",105):RETURN
 1220 IF W%=&48 PROCPNT("PHA ",1):RETURN
 1230 IF W%=&08 PROCPNT("PHP ",1):RETURN
 1240 IF W%=&68 PROCPNT("PLA ",1):RETURN
 1250 IF W%=&28 PROCPNT("PLP ",1):RETURN
 1260 IF W%=&2A PROCPNT("ROL ",2):RETURN
 1270 IF W%=&26 PROCPNT("ROL ",100):RETURN
 1280 IF W%=&36 PROCPNT("ROL ",101):RETURN
 1290 IF W%=&2E PROCPNT("ROL ",200):RETURN
 1300 IF W%=&3E PROCPNT("ROL ",201):RETURN
 1310 IF W%=&6A PROCPNT("ROR ",2):RETURN
 1320 IF W%=&66 PROCPNT("ROR ",100):RETURN
 1330 IF W%=&76 PROCPNT("ROR ",101):RETURN
 1340 IF W%=&6E PROCPNT("ROR ",200):RETURN
 1350 IF W%=&7E PROCPNT("ROR ",201):RETURN
 1360 IF W%=&40 PROCPNT("RTI ",1):RETURN
 1370 IF W%=&60 PROCPNT("RTS ",1):RETURN
 1380 IF W%=&E9 PROCPNT("SBC#",100):RETURN
 1390 IF W%=&E5 PROCPNT("SBC ",100):RETURN
 1400 IF W%=&F5 PROCPNT("SBC ",101):RETURN
 1410 IF W%=&ED PROCPNT("SBC ",200):RETURN
 1420 IF W%=&FD PROCPNT("SBC ",201):RETURN
 1430 IF W%=&F9 PROCPNT("SBC ",202):RETURN
 1440 IF W%=&E1 PROCPNT("SBC ",103):RETURN
 1450 IF W%=&F1 PROCPNT("SBC ",105):RETURN
 1460 IF W%=&38 PROCPNT("SEC ",1):RETURN
 1470 IF W%=&F8 PROCPNT("SED ",1):RETURN
 1480 IF W%=&78 PROCPNT("SEI ",1):RETURN
 1490 IF W%=&85 PROCPNT("STA ",100):RETURN
 1500 IF W%=&95 PROCPNT("STA ",101):RETURN
 1510 IF W%=&8D PROCPNT("STA ",200):RETURN
 1520 IF W%=&9D PROCPNT("STA ",201):RETURN
 1530 IF W%=&99 PROCPNT("STA ",202):RETURN
 1540 IF W%=&81 PROCPNT("STA ",103):RETURN
 1550 IF W%=&91 PROCPNT("STA ",105):RETURN
 1560 IF W%=&86 PROCPNT("STX ",100):RETURN
 1570 IF W%=&96 PROCPNT("STX ",106):RETURN
 1580 IF W%=&8E PROCPNT("STX ",200):RETURN
 1590 IF W%=&84 PROCPNT("STY ",100):RETURN
 1600 IF W%=&94 PROCPNT("STY ",101):RETURN
 1610 IF W%=&8C PROCPNT("STY ",200):RETURN
 1620 IF W%=&AA PROCPNT("TAX ",1):RETURN
 1630 IF W%=&A8 PROCPNT("TAY ",1):RETURN
 1640 IF W%=&BA PROCPNT("TSX ",1):RETURN
 1650 IF W%=&8A PROCPNT("TXA ",1):RETURN
 1660 IF W%=&9A PROCPNT("TXS ",1):RETURN
 1670 IF W%=&98 PROCPNT("TYA ",1):RETURN
 1680 IF W%=&89 PROCPNT("BIT#",100):RETURN
 1690 IF W%=&34 PROCPNT("BIT ",101):RETURN
 1700 IF W%=&3C PROCPNT("BIT ",201):RETURN
 1710 IF W%=&80 PROCPNT("BRA ",102):RETURN
 1720 IF W%=&3A PROCPNT("DEC ",2):RETURN
 1730 IF W%=&1A PROCPNT("INC ",2):RETURN
 1740 IF W%=&DA PROCPNT("PHX ",1):RETURN
 1750 IF W%=&5A PROCPNT("PHY ",1):RETURN
 1760 IF W%=&FA PROCPNT("PLX ",1):RETURN
 1770 IF W%=&7A PROCPNT("PLY ",1):RETURN
 1780 IF W%=&64 PROCPNT("STZ ",100):RETURN
 1790 IF W%=&74 PROCPNT("STZ ",101):RETURN
 1800 IF W%=&9C PROCPNT("STZ ",200):RETURN
 1810 IF W%=&9E PROCPNT("STZ ",201):RETURN
 1820 IF W%=&14 PROCPNT("TRB ",100):RETURN
 1830 IF W%=&1C PROCPNT("TRB ",200):RETURN
 1840 IF W%=&04 PROCPNT("TSB ",100):RETURN
 1850 IF W%=&0C PROCPNT("TSB ",200):RETURN
 1860 IF W%=&0F PROCPNT("BBR0",206):RETURN
 1870 IF W%=&1F PROCPNT("BBR1",206):RETURN
 1880 IF W%=&2F PROCPNT("BBR2",206):RETURN
 1890 IF W%=&3F PROCPNT("BBR3",206):RETURN
 1900 IF W%=&4F PROCPNT("BBR4",206):RETURN
 1910 IF W%=&5F PROCPNT("BBR5",206):RETURN
 1920 IF W%=&6F PROCPNT("BBR6",206):RETURN
 1930 IF W%=&7F PROCPNT("BBR7",206):RETURN
 1940 IF W%=&8F PROCPNT("BBS0",206):RETURN
 1950 IF W%=&9F PROCPNT("BBS1",206):RETURN
 1960 IF W%=&AF PROCPNT("BBS2",206):RETURN
 1970 IF W%=&BF PROCPNT("BBS3",206):RETURN
 1980 IF W%=&CF PROCPNT("BBS4",206):RETURN
 1990 IF W%=&DF PROCPNT("BBS5",206):RETURN
 2000 IF W%=&EF PROCPNT("BBS6",206):RETURN
 2010 IF W%=&FF PROCPNT("BBS7",206):RETURN
 2020 IF W%=&07 PROCPNT("RB0 ",100):RETURN
 2030 IF W%=&17 PROCPNT("RB1 ",100):RETURN
 2040 IF W%=&27 PROCPNT("RB2 ",100):RETURN
 2050 IF W%=&37 PROCPNT("RB3 ",100):RETURN
 2060 IF W%=&47 PROCPNT("RB4 ",100):RETURN
 2070 IF W%=&57 PROCPNT("RB5 ",100):RETURN
 2080 IF W%=&67 PROCPNT("RB6 ",100):RETURN
 2090 IF W%=&77 PROCPNT("RB7 ",100):RETURN
 2100 IF W%=&87 PROCPNT("SB0 ",100):RETURN
 2110 IF W%=&97 PROCPNT("SB1 ",100):RETURN
 2120 IF W%=&A7 PROCPNT("SB2 ",100):RETURN
 2130 IF W%=&B7 PROCPNT("SB3 ",100):RETURN
 2140 IF W%=&C7 PROCPNT("SB4 ",100):RETURN
 2150 IF W%=&D7 PROCPNT("SB5 ",100):RETURN
 2160 IF W%=&E7 PROCPNT("SB6 ",100):RETURN
 2170 IF W%=&F7 PROCPNT("SB7 ",100):RETURN
 2180 IF W%=&7C PROCPNT("JMP ",203):RETURN
 2190 IF W%=&12 PROCPNT("ORA ",107):RETURN
 2200 IF W%=&32 PROCPNT("AND ",107):RETURN
 2210 IF W%=&52 PROCPNT("EOR ",107):RETURN
 2220 IF W%=&72 PROCPNT("ADC ",107):RETURN
 2230 IF W%=&92 PROCPNT("STA ",107):RETURN
 2240 IF W%=&B2 PROCPNT("LDA ",107):RETURN
 2250 IF W%=&D2 PROCPNT("CMP ",107):RETURN
 2260 IF W%=&F2 PROCPNT("SBC ",107):RETURN
 3390 PROCPNT("xxx ",0)
 3400 RETURN
 4000 DEFPROCPNT(M$,T%)
 4010 PRINT~I%;" ";
 4020 H0%=?I%:H0C%=H0%:IFH0%<32 ORH0%>126 H0C%=32
 4030 PRINT CHR$H0C%;
 4040 H1%=?(I%+1):H1C%=H1%:IFH1%<32 ORH1%>126 H1C%=32
 4050 IF T%>99 PRINT CHR$H1C%; ELSE PRINT" ";
 4060 H2%=?(I%+2):H2C%=H2%:IFH2%<32 ORH2%>126 H2C%=32
 4070 IF T%>199 PRINT CHR$H2C%; ELSE PRINT" ";
 4080 PRINT "  ";
 4090 PRINT FNd0(H0%,3);
 4095 PRINT" ";
 4100 IF T%>99 PRINT FNd0(H1%,3); ELSE PRINT"   ";
 4105 PRINT" ";
 4110 IF T%>199 PRINT FNd0(H2%,3); ELSE PRINT"   ";
 4111 PRINT" ";FNh0(H0%,2);
 4112 IF T%>99 PRINT " ";FNh0(H1%,2); ELSE PRINT"   ";
 4113 IF T%>199 PRINT " ";FNh0(H2%,2); ELSE PRINT"   ";
 4115 PRINT"  "; M$;
 4118 IF T%=1 PRINT:ENDPROC
 4119 IF T%=2 PRINT"A":ENDPROC
 4200 IF T%=100 PRINT"&";FNh0(H1%,2);:I%=I%+1:ENDPROC
 4210 IF T%=101 PRINT"&";FNh0(H1%,2);",X":I%=I%+1:ENDPROC
 4220 IF T%=200 PRINT"&";FNh0(H2%,2);FNh0(H1%,2);:I%=I%+2:ENDPROC
 4230 IF T%=201 PRINT"&";FNh0(H2%,2);FNh0(H1%,2);",X":I%=I%+2:ENDPROC
 4240 IF T%=202 PRINT"&";FNh0(H2%,2);FNh0(H1%,2);",Y":I%=I%+2:ENDPROC
 4250 IF T%=203 PRINT"(&";FNh0(H2%,2);FNh0(H1%,2);",X)":I%=I%+2:ENDPROC
 4255 IF T%=206 PRINT" &";FNh0(H1%,2);" ";:I%=I%+1:T%=102:H1%=H2%
 4260 IF T%=102 AND H1%<129 BRAN%=H1%
 4270 IF T%=102 AND H1%>128 BRAN%=-(256-H1%)
 4280 IF T%=102 PRINT;BRAN%;"   --> &";:BRAT%=I%+2+BRAN%:PRINTFNh0(BRAT%,2):I%=I%+1:ENDPROC
 4290 IF T%=204 PRINT"(&";FNh0(H2%,2);FNh0(H1%,2);"),Y":I%=I%+2:ENDPROC
 4300 IF T%=103 PRINT"(&";FNh0(H1%,2);",X)":I%=I%+1:ENDPROC
 4310 IF T%=104 PRINT"(&";FNh0(H1%,2);",Y)":I%=I%+1:ENDPROC
 4320 IF T%=105 PRINT"(&";FNh0(H1%,2);"),Y":I%=I%+1:ENDPROC
 4330 IF T%=205 PRINT"(&";FNh0(H2%,2);FNh0(H1%,2);")":I%=I%+2:ENDPROC
 4340 IF T%=106 PRINT"&";FNh0(H1%,2);",Y":I%=I%+1:ENDPROC
 4350 IF T%=107 PRINT"(&";FNh0(H1%,2);")":I%=I%+1:ENDPROC
 4990 PRINT "Invalid Code"
 4999 ENDPROC
 5000 DEFFNh0(A%,N%)=RIGHT$("0000000"+STR$~A%,N%)
 5010 DEFFNd0(A%,N%)=RIGHT$("0000000"+STR$~A%,N%)