💾 Archived View for mirrors.apple2.org.za › archive › apple.cabi.net › Languages.Programming › OMF ›… captured on 2023-01-29 at 08:23:18.
-=-=-=-=-=-=-
Subject: v001SRC067: coff (OMF Disassembler) 02/09 Newsgroups: comp.sources.apple2 Approved: jac@paul.rutgers.edu Submitted-by: Albert Chin-A-Young (26285659t@servax.fiu.edu) Posting-number: Volume 1, Source:67 Archive-name: utility/gs/disassem/coff/part02 Architecture: ONLY_2gs Version-number: 1.1 =asm.s - lst off - -* UNIX coff utility -* 65816 OMF disassembler -* -* 1990-1992, tao Developer Project - - rel - xc - xc - mx %00 - - put coff.h ;global defines - put x.data ;data externals - put x.general ;general externals - put x.gsos ;GS/OS i/o externals - put x.omf ;OMF parser externals - put x.output ;output externals - put x.structure ;data structure externals - - put 4/gsos.h ;GS/OS defines - put 4/memory.h ;memory manager defines - put 4/resource.h ;resouce manager defines - put 4/texttool.h ;text tool defines - put 4/env.h ;run-time environment settings - - use coff.mac ;macro definitions - use 4/datatype.mac ;HLL data types - use 4/env.mac ;run-time environment macros - - -* dp $9x-$cx taken - -************************************************** -* display header for asm disassembly. * -************************************************** -display_header_asm ent -]segname_handle = $f0 ;handle to segment name -]segname_ptr = $f4 -]segname_len = $f8 ;length of segment name - - ldx @omf+`segname+2 - ldy @omf+`segname - stx ]segname_handle+2 - sty ]segname_handle - ldy #2 - lda []segname_handle],y - sta ]segname_ptr+2 - lda []segname_handle] - sta ]segname_ptr - lda []segname_ptr] - sta ]segname_len - - lda ~assembler - cmp #MERLIN - bne :orca - lda #LONGA - jsr asm_status_bit - jsr print_offset - pei ]segname_ptr+2 - pei ]segname_ptr - pea #2 - pei ]segname_len - _TextWriteBlock - lda ]segname_len - cmp #12 - blt :0 - pea #' ' - _WriteChar - bra :1 -:0 pea #^blank_str - pea #blank_str - pea #0 - sec - lda #12 - sbc ]segname_len - pha - _TextWriteBlock -:1 pea #^EQU_asm - pea #EQU_asm - _WriteCString - pea #'*' - _WriteChar - bra :end - -:orca lda #LONGA - jsr asm_status_bit - lda #LONGI - jsr asm_status_bit - jsr print_offset - pei ]segname_ptr+2 - pei ]segname_ptr - pea #2 - pei ]segname_len - _TextWriteBlock - lda ]segname_len - cmp #12 - blt :2 - pea #' ' - _WriteChar - bra :3 -:2 pea #^blank_str - pea #blank_str - pea #0 - sec - lda #12 - sbc ]segname_len - pha - _TextWriteBlock -:3 lda @omf+`kind - and #DATA - cmp #DATA - bne :start - pea #^:data_str - pea #:data_str - _WriteCString - bra :end -:start pea #^:start_str - pea #:start_str - _WriteCString -:end put_cr - rts - -:data_str cStr 'data' -:start_str cStr 'start' - - -************************************************** -* display status of accumulator and index * -* registers (short/long). * -* ---------------------------------------------- * -* (input) * -* a - display accumulator or index status. * -************************************************** -asm_status_bit equ * -]status_bit = $e0 - - sta ]status_bit - - jsr print_offset - pea #^space_12 - pea #space_12 - _WriteCString - lda ~assembler - cmp #MERLIN - bne :orca - -:merlin pea #^:mx_str - pea #:mx_str - _WriteCString - ldx #'0' - lda }shorti - bne :test_shorta - ldx #'1' -:test_shorta phx - ldx #'0' - lda }shorta - bne :merlin_end - ldx #'1' -:merlin_end phx - _WriteChar - _WriteChar - put_cr - rts - -:orca lda ]status_bit - cmp #LONGA - bne :longi - pea #^:longa_str - pea #:longa_str - _WriteCString - lda }shorta - beq :longa_off - pea #^:off_str - pea #:off_str - bra :end -:longa_off pea #^:on_str - pea #:on_str - bra :end - -:longi pea #^:longi_str - pea #:longi_str - _WriteCString - lda }shorti - beq :longi_off - pea #^:off_str - pea #:off_str - bra :end -:longi_off pea #^:on_str - pea #:on_str - -:end _WriteCString - put_cr - rts - -:mx_str cStr 'mx %' -:longa_str cStr 'longa ' -:longi_str cStr 'longi ' -:on_str cStr 'on' -:off_str cStr 'off' - - -************************************************** -* parse CONST record for disassembling. * -* ---------------------------------------------- * -* (input) * -* a - record number. * -************************************************** -parse_CONST_asm ent -]count = $90 ;number of bytes to read -]edge = $94 ;right margin for output -]record = $96 ;record number -]opcode = $98 ;opcode to parse -]opcode_adr = $9a ;address of opcode data - - sta ]record - stz ]opcode - stz ]count+2 - stz ]count - - cmp #LCONST - bne :const - read_long ]count - clc - lda @omf+`displacement - adc #4 - sta @omf+`displacement - bcc :loop - inc @omf+`displacement+2 - bra :loop -:const sta ]count - -:loop lda ]count - ora ]count+2 - bne :print_opcode - rts -:print_opcode read_char ]opcode - pea #^space_12 ;indent to print opcode and operand - pea #space_12 - _WriteCString - lda ]opcode - asl - tax - lda ~opcodes,x - sta ]opcode_adr - ldy #`num_bytes ;parse opcode depending on number - lda (]opcode_adr),y ;of bytes it takes - cmp #1 - bne :2_bytes - lda ]opcode - jsr parse_opcode_1 - lda ]count - bne :0 - dec ]count+2 -:0 dec ]count - bra :end_loop -:2_bytes cmp #2 - bne :3_bytes - lda ]opcode - ldx ]count+2 - ldy ]count - jsr parse_opcode_2 - stx ]count+2 - sty ]count - bra :end_loop -:3_bytes cmp #3 - bne :4_bytes - lda ]opcode - ldx ]count+2 - ldy ]count - jsr parse_opcode_3 - stx ]count+2 - sty ]count - bra :end_loop -:4_bytes lda ]opcode - ldx ]count+2 - ldy ]count - jsr parse_opcode_4 - stx ]count+2 - sty ]count - -:end_loop lda }nooffset - beq :1 - brl :loop -:1 lda ]count+2 - ora ]count - beq :end - jsr print_offset - brl :loop -:end rts - - -************************************************** -* parse opcodes that accept 1-byte operands. * -* ---------------------------------------------- * -* (input) * -* a - opcode. * -************************************************** -parse_opcode_1 equ * -]opcode = $a0 ;opcode -]opcode_adr = $a2 ;pointer to information about opcode -]opcode_syntax = $a4 ;string syntax of opcode - - sta ]opcode - asl - tax - lda ~opcodes,x - sta ]opcode_adr - - pea #^parse_opcode_1 - clc - lda ]opcode_adr - adc #`syntax - pha - _WriteCString - - ldy #`mode - lda (]opcode_adr),y - cmp #ACCUMULATOR - bne :0 - lda ~assembler - cmp #ORCA - bne :0 - pea #'a' - bra :1 -:0 pea #' ' -:1 _WriteChar - - lda }hex - beq :2 - pea #^blank_str ;separate asm/hex-ascii output - pea #blank_str - pea #0 - pea #24 - _TextWriteBlock -:2 lda ]opcode - ora #$0100 - ldx #0 - txy - jsr print_hex_ascii - incr @omf+`displacement - incr @omf+`counter -:end rts - - -************************************************** -* parse opcodes that accept 2-byte operands. * -* ---------------------------------------------- * -* (input) * -* a - opcode. * -* x - HOW of number of bytes to disassemble. * -* y - LOW of number of bytes to disassemble. * -* (output) * -* x - HOW of number of bytes to disassemble. * -* y - LOW of number of bytes to disassemble. * -************************************************** -parse_opcode_2 equ * -]opcode = $a0 ;opcode -]count = $a2 ;number of bytes to disassemble -]operand = $a6 ;operand of opcode -]opcode_adr = $a8 ;pointer to information about opcode - - sta ]opcode - stx ]count+2 - sty ]count - stz ]operand - asl - tax - lda ~opcodes,x - sta ]opcode_adr - - ldy #`m ;test if operand affected by short - lda (]opcode_adr),y ;accumulator - beq :test_i - lda }shorta - beq :short -:test_i ldy #`i ;test if operand affected by short - lda (]opcode_adr),y ;indexes - bne :test_short - brl :print_opcode -:test_short lda }shorti - beq :short - brl :print_opcode -:short lda ]count+2 - bne :0 - lda ]count - cmp #3 - blt :3 -:0 incr #3;@omf+`displacement - incr #3;@omf+`counter - read_short ]operand ;because shorta or shorti is not - lda }tool ;active, read in two byte operand - beq :1 - lda ]opcode - cmp #LDX - bne :1 - pei ]count+2 - pei ]count - pei ]operand - pei ]opcode - jsr parse_stack - stx ]count+2 - sty ]count - bra :2 -:1 lda ]opcode - ldx ]operand - jsr print_opcode_3 -:2 sec - lda ]count - sbc #3 - tay - lda ]count+2 - sbc #0 - tax - rts - -:3 cmp #2 - beq :5 - clc - lda @omf+`counter - adc #3 - tax - lda @omf+`counter+2 - adc #0 - cmp @omf+`length+2 - blt :4 - cpx @omf+`length - beq :4 - blt :5 -:4 lda ]opcode - jsr parse_expr_asm - bra :6 -:5 lda ]opcode - ldx ]count - jsr print_byte -:6 ldx #0 - txy - rts - -:print_opcode lda ]count+2 - bne :8 - lda ]count - cmp #2 - blt :9 -:8 lda ]opcode - jsr print_opcode_2 - sec - lda ]count - sbc #2 - tay - lda ]count+2 - sbc #0 - tax - rts -:9 clc - lda @omf+`counter - adc #2 - tax - lda @omf+`counter+2 - adc #0 - cmp @omf+`length+2 - blt :10 - cpx @omf+`length - beq :10 - bge :11 -:10 lda ]opcode - jsr parse_expr_asm - bra :12 -:11 lda ]opcode - ldx ]count - jsr print_byte -:12 ldx #0 - txy - rts - - -************************************************** -* print opcodes that generate two bytes. * -* ---------------------------------------------- * -* (input) * -* a - opcode. * -************************************************** -print_opcode_2 equ * -]opcode = $b0 ;opcode -]operand = $b2 ;operand of opcode -]opcode_adr = $b4 ;pointer to information about opcode -]opcode_syntax = $b6 ;string syntax of opcode -]offset = $b8 ;offset into line - - sta ]opcode - stz ]operand - asl - tax - lda ~opcodes,x - sta ]opcode_adr - - read_char ]operand - ldy #`mode - lda (]opcode_adr),y - cmp #PC_RELATIVE - bne :2 - lda ]operand - cmp #$80 - bge :sub_operand -:add_operand clc - lda @omf+`counter - adc ]operand - bra :printf -:sub_operand sec ;@omf+`counter+($ff-]operand) - lda @omf+`counter - sbc #$100 - clc - adc ]operand -:printf inc - inc - tay - ldx #0 - clc - lda ]opcode_adr - adc #`syntax - jsr printf - stx ]offset - pea #^:space - pea #:space - _WriteCString - clc - lda #4 - adc ]offset - sta ]offset - ldx #'+' - lda ]operand - cmp #$80 - blt :print_char - ldx #'-' -:print_char phx - _WriteChar - inc ]offset - ldx ]operand - cpx #$80 - blt :print_operand - sec - lda #$100 - sbc ]operand - tax -:print_operand jsr print_fix_char_hex - inc ]offset - inc ]offset - pea #'}' - _WriteChar - inc ]offset - bra :print_hex - -:2 clc - lda ]opcode_adr - adc #`syntax - ldx ]operand+2 - ldy ]operand - jsr printf - stx ]offset - -:print_hex lda }hex - beq :3 - pea #^blank_str ;separate asm/hex-ascii output - pea #blank_str - pea #0 - sec - lda #32 - sbc ]offset - pha - _TextWriteBlock -:3 lda ]opcode - ora #$0200 - ldx #0 - ldy ]operand - jsr print_hex_ascii - lda ]opcode - cmp #REP - beq :parse_rep_sep - cmp #SEP - bne :4 - -:parse_rep_sep lda ]opcode - ldx ]operand - jsr parse_rep_sep - -:4 incr #2;@omf+`displacement - incr #2;@omf+`counter - rts - -:space cStr ' {' - - -************************************************** -* parse opcodes that accept 3-byte operands. * -* ---------------------------------------------- * -* (input) * -* a - opcode. * -* x - HOW of number of bytes to disassemble. * -* y - LOW of number of bytes to disassemble. * -* (output) * -* x - HOW of number of bytes to disassemble. * -* y - LOW of number of bytes to disassemble. * -************************************************** -parse_opcode_3 equ * -]opcode = $a0 ;opcode -]count = $a2 ;number of bytes to disassemble -]tmp_count = $a6 -]operand = $aa ;operand of opcode - - sta ]opcode - stx ]count+2 - sty ]count - - cpx #1 ;expand opcode only if 3 bytes - bge :print_opcode ;available - cpy #3 - bge :print_opcode - cpy #2 ;test if two bytes left in three-byte - beq :1 ;opcode/operand. if so, print bytes. - clc ;test if at end of OMF segment - lda @omf+`counter - adc #3 - tax - lda @omf+`counter+2 - adc #0 - cmp @omf+`length+2 - blt :0 - cpx @omf+`length - beq :0 - bge :1 -:0 lda ]opcode - jsr parse_expr_asm - bra :2 -:1 lda ]opcode - ldx ]count - jsr print_byte -:2 ldx #0 - txy - rts - -:print_opcode incr #3;@omf+`displacement - incr #3;@omf+`counter - read_short ]operand - lda }tool - beq :5 - lda ]opcode - cmp #JSR - bne :4 - lda ]operand - ldx ]count+2 - ldy ]count - jsr parse_inline_3 - stx ]tmp_count+2 - sty ]tmp_count - cpx ]count+2 - bne :3 - cpy ]count - bne :3 - lda ]opcode - ldx ]operand - jsr print_opcode_3 - bra :end -:3 ldx ]tmp_count+2 - ldy ]tmp_count - stx ]count+2 - sty ]count - bra :end -:4 lda ]opcode - cmp #PEA - bne :5 - pei ]count+2 - pei ]count - pei ]operand - pei ]opcode - jsr parse_stack - stx ]count+2 - sty ]count - bra :end -:5 lda ]opcode - ldx ]operand - jsr print_opcode_3 - -:end sec - lda ]count - sbc #3 - tay - lda ]count+2 - sbc #0 - tax - rts - - -************************************************** -* print opcodes that generate three bytes. * -* ---------------------------------------------- * -* (input) * -* a - opcode. * -* x - operand. * -************************************************** -print_opcode_3 equ * -]opcode = $b0 ;opcode -]operand = $b2 ;operand of opcode -]opcode_adr = $b4 ;pointer to information about opcode -]offset = $b6 ;offset into line -]ROM_ptr = $b8 ;pointer to ROM name - - sta ]opcode - stx ]operand - asl - tax - lda ~opcodes,x - sta ]opcode_adr - stz ]offset - - ldy #`mode - lda (]opcode_adr),y - cmp #ABSOLUTE - bne :pc_relative_long - lda }tool - bne :ROM_tool - brl :default -:ROM_tool ldx ]operand - ldy #0 - jsr name_ROM - stx ]ROM_ptr - sty ]ROM_ptr+2 - bcc :print_ROM - brl :default -:print_ROM phy - phx - pea #^print_opcode_3 - clc - lda ]opcode_adr - adc #`syntax - pha - pea #0 - pea #7 - _TextWriteBlock - _WriteString - lda []ROM_ptr] - and #$ff - clc - adc #7 - sta ]offset - brl :end - -:pc_relative_long cmp #PC_RELATIVE_LONG - bne :block_move - lda ]operand - bmi :sub_operand -:add_operand clc - lda @omf+`counter - adc ]operand - bra :printf -:sub_operand sec - lda @omf+`counter - sbc ]operand -:printf inc - inc - tay - ldx #0 - clc - lda ]opcode_adr - adc #`syntax - jsr printf - stx ]offset - pea #^:space - pea #:space - _WriteCString - ldx #'+' - lda ]operand - bpl :print_char - ldx #'-' -:print_char phx - _WriteChar - ldx ]operand - bpl :print_operand - sec - lda #$ffff - sbc ]operand - inc - tax -:print_operand lda #4 - jsr print_fix_short_hex - clc - lda ]offset - adc #10 - sta ]offset - pea #'}' - _WriteChar - brl :end - -:block_move cmp #BLOCK_MOVE - bne :immediate - pea #^print_opcode_3 - clc - lda ]opcode_adr - adc #`syntax - pha - _WriteCString - lda ]operand - xba - and #$ff - tax - jsr print_fix_char_hex - pea #',' - _WriteChar - pea #'