TITLE 'MAIN DISASSEMBLY LOOP'›SUBTTL 'File DSMAIN.AMA'›;›; This module disassembles one segment of the object code.›; It contains most of the fancier analysis logic in the›; program, and most of the unstructured code. Oh well...›;›PROC›DSMAINLDA #2›STA PASS›:DSMLPJSR CLRBUF ;Clear buffers›JSR LINADR;Send line addr to E:,P:›;›; See if there's a label for this instruction.›;›LDA STAR›STA ADRBUF›LDA STAR+1›STA ADRBUF+1›:PUTLBJSR GETLAB›BCS :BYTE1;If not, skip ahead.›LDA #1›JSR FLGLAB;Flag this label as "internal"›LDX #0 ;Index output byte 0.›JSR WRTLAB;Convert label nr to a ›;text label and insert it.›:BYTE1JSR GETBYT;Get byte 1 of instruction.›BCC :B1A;Get out if not there.›JMP :EXIT1›:B1ATAX›LDA MODES,X;Look at mode ID.›CMP #NOP;Is it an opcode?›BEQ :TEXT;No, handle as text.›LDY TEXCNT;Yes, check text count.›BEQ :NOTEX;No text present,›; so move on to disassembly.›STX OPSAV;Save the opcode.›JSR TEXOUT;Output a text instruction.›;›; Now we have to output a line address to E:,P:.›; Unfortunately the location counter is past the›; first byte, so we have to back it up one.›;›SEC›LDA STAR›SBC #1›STA STAR›LDA STAR+1›SBC #0›STA STAR+1›JSR LINADR›INC STAR ;Put counter back where it was›BNE :B1B›INC STAR+1›:B1BJSR CLRBUF;Clear the buffers.›LDX OPSAV;Recover the opcode.›:NOTEXSTX INSBUF;Put in instruction buffer.›INC INSPTR;Update the pointer.›LDA MODES,X;Get the mode byte.›AND #$60;Look at nr of operand bytes.›LSR A ;Shift it down.›LSR A›LSR A›LSR A›LSR A›STA OPRCNT;Move to operand count.›AND #$02;Set up booby trap flag›STA BTFLG›LDA OPRCNT›:OPLPBEQ :DSMBL;Here the Z bit must›; reflect contents of OPRCNT because we either›; shifted or decremented it to get here.›JSR GETBYT;Still need some oprs.›BCS :EXIT2;Out of bytes, this›;will become a text instruction.›LDX INSPTR›STA INSBUF,X;Put operand in buffer.›INC INSPTR›DEC OPRCNT›JMP :OPLP;Try for another opr.›;›; Now we have the instruction buffer full,›; so let's disassemble it.›;›:DSMBLLDA BTFLG;Check booby trap flag›BEQ :DSMB1›LDA INSBUF+2›BNE :DSMB1›;›; Aha! We have a 2-byte address instruction but›; the address is 0-page. We must be trying to›; disassemble a text sequence as an instruction.›; Let's put a stop to this crapola.›;›LDA #3›STA TEXCNT›JMP :TEXFL›:DSMB1JSR DSMINS ;Disassemble instruction;›JSR RECOUT ;Output it;›JMP :DSMLP;and do it all again.›;›; Here we load the byte into the instruction buffer as a›; text byte. It's in the X register.›:TEXTTXA›LDX TEXCNT;Tells where to put it.›STA INSBUF,X›INC TEXCNT›;›; If the next location is labeled we must›; bite off the text instruction here.›;›LDA STAR›STA ADRBUF›LDA STAR+1›LDA ADRBUF+1›JSR GETLAB›BCS :NOLAB;It's not labeled.›:TEXFLJSR TEXOUT›JMP :DSMLP;Back thru main loop.›:NOLABLDA TEXCNT;See how much text we have.›CMP #5 ;>=8 bytes?›BCS :TEXFL;Yes, wrap it up.›JMP :BYTE1;No, continue with text.›;›; Here we convert this aborted instruction to text.›;›:EXIT2LDX INSPTR;This is number of bytes in INSBUF.›STX TEXCNT;This makes INSBUF a text line.›:EXIT1LDA TEXCNT;Any text present?›BEQ :EXIT0;No, get out.›JSR TEXOUT;Yes, output it.›:EXIT0LDA MORSEG;Any more segments?›BEQ :BYE;No, drop out›;›; There is another segment to disassemble so we have›; to output a set-origin pseudo. But first we must›; see if we left a label hanging at the address that›; follows the end of the current segment. If so we›; just unflag it so it will be an external reference.›;›LDA LINEBF;Look at first buffer byte›CMP #'L';Start of a label?›BNE :EXIT;No, no label here›LDA #0;Yes, unflag it›JSR FLGLAB›:EXITJSR SETORG;Set the new origin›:BYESEC ;Indicate end of segment›RTS ;and split.›EPROC›;›