From 2090a4fd8ee743b7a3095c354bb0fbc14dbb1495 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Sat, 30 May 2020 12:43:08 -0400 Subject: Refactored the assembler a bit more, in order to remove code duplication. The assembler now uses pre-tokens, which are smaller tokens, that make up the larger tokens. --- programs/subasm.s | 137 ++++-------------------------------------------------- programs/utils.s | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 128 deletions(-) create mode 100644 programs/utils.s (limited to 'programs') diff --git a/programs/subasm.s b/programs/subasm.s index e5d620f..86a8837 100644 --- a/programs/subasm.s +++ b/programs/subasm.s @@ -5,6 +5,8 @@ MAX_SYM = $800 ; Max symbol size. +.include "utils.s" + .org incl ; String Constants. asm_name: @@ -150,9 +152,8 @@ cmd_srt: .word run .word set -; Hex character table. -hex_char: - .byte "0123456789ABCDEF" +; Data entry point for utility subroutines. +util_data: ; Token table. @@ -283,131 +284,6 @@ shcmd_end: ply #2 ; Get back the screen buffer position. rts ; End of chk_shcmd. -print_hex: - pha #8 ; Preserve the hex value. - and #0 ; Reset A. - ldb #1 ; Set the second pointer - lda.w #hex_char ; to the start of hex character table. - jsr set_ptr ; - inb ; Set the third pointer - lda.d #hex_str ; to the end of hex string buffer. - clc ; Do a non carrying add. - adc #$10 ; - jsr set_ptr ; - ldb #0 ; Reset B. - pla #8 ; Get the hex value back. -pnthex_lp: - pha #8 ; Preserve the hex value. - and #$F ; Mask the lowest nibble. - phy #2 ; Preserve the screen buffer position. - tay ; Get the index for the hex digit. - lda (ptr2), y ; Get the hex digit. - dec ptr3 ; Decrement the string pointer. - sta (ptr3) ; Save the hex digit character in the string. - ply #2 ; Get back the screen buffer position. - pla #8 ; Get the hex value back. -pnthex_lp1: - cpx #1 ; Is the digit count less than one? - bcc pnthex_lp2 ; Yes, so don't decrement the digit count. - dex ; No, but was the digit count zero, when decremented? - beq pnthex_end ; Yes, so we're done. - jmp pnthex_lp3 ; No, so get the next nibble. -pnthex_lp2: - ldb #1 ; Enable auto digit count. -pnthex_lp3: - lsr #4 ; No, but is the next nibble, a zero? - beq pnthex_lp4 ; Yes, so check if auto digit count is enabled. - jmp pnthex_lp ; No, so print the next digit. -pnthex_lp4: - cpb #1 ; Is auto digit count enabled? - beq pnthex_end ; Yes, so we're done. - jmp pnthex_lp ; No, so keep printing more digits. -pnthex_end: - rts ; End of print_hex. - -charcpy: - ldx idx3 ; Get the string index. - sta strbuf, x ; Save it in the string buffer. - inc idx3 ; Increment the string index. - rts ; End of charcpy. - -print_hi: - and #0 ; Reset A. - sta idx3 ; Clear the string index. - lda #'$' ; Print the hex delimiter. - jsr charcpy ; - lda.q idx0 ; Get the masked address. - ldx #$10 ; Set digit count to 16. - jsr print_hex ; Print the address. - lda.q hex_str ; Get the lower half of the string. - sta.q strbuf+1 ; Save it in the string buffer. - lda.q hex_str+8 ; Get the upper half of the string. - sta.q strbuf+9 ; Save it in the string buffer. - ldx #$11 ; Add 16 to the index. - stx idx3 ; - lda #':' ; Print a colon. - jsr charcpy ; - lda # ' ' ; Print a space. - jsr charcpy ; - rts ; End of print_hi. - -print_lo: - lda #0 ; Reset A. - sta idx3 ; Clear the string index. -pntlo_lp: - ldx #2 ; Set digit count to 2. - pha #1 ; Preserve the nibble offset. - jsr print_hex ; Print the low nibble offset. - lda.w (ptr3) ; Get the two digits. - jsr charcpy ; Copy the first digit. - lsr #8 ; Copy the next digit. - jsr charcpy ; - pla #1 ; Get the nibble offset back. - inc ; Increment the offset. - cmp #$10 ; Are we at the last offset? - bcs pntlo_end ; Yes, so we're done. -pntlo_lp1: - pha #1 ; No, so preserve the nibble offset. - lda #' ' ; Add a space to the string buffer. - jsr charcpy ; - pla #1 ; Get the nibble offset back. - jmp pntlo_lp ; Keep looping. -pntlo_end: - inx ; Increment the index by one. - lda #0 ; Null terminate the string buffer. - sta strbuf, x ; - tax ; Reset X. - lda.d #strbuf ; Print the string buffer. - jsr print_str ; - rts ; End of print_lo. - -print_chunk: - ldx #0 ; Reset X. - phy #2 ; Preserve the screen buffer index. - txy ; Copy the byte index to it. -pntchnk_lp: - and #0 ; Reset A. - ldx #2 ; Set the digit count to 2. - lda (idx0), y ; Get the byte at that address. - jsr print_hex ; Print the byte. - lda.w (ptr3) ; Get the two digits. - jsr charcpy ; Copy the first digit. - lsr #8 ; Copy the next digit. - jsr charcpy ; - iny ; Increment the byte index. - cpy #$10 ; Have we read 16 bytes? - beq pntchnk_end ; Yes, so we're done. - lda #' ' ; No, so add a soace to the string buffer. - jsr charcpy ; - jmp pntchnk_lp ; Keep looping. -pntchnk_end: - ply #2 ; Get the screen buffer index back. - inx ; Increment the index by one. - and #0 ; Null terminate the string. - sta strbuf, x ; - tax ; Reset X. - sta idx3 ; Clear the string index. - rts ; End of print_chunk. viewmem: lda.q prg_cnt ; Get the program counter. @@ -469,3 +345,8 @@ set: nop ; set_end: rts ; End of set. + + +; Entry point for utility subroutines. +utils: + diff --git a/programs/utils.s b/programs/utils.s new file mode 100644 index 0000000..a6a14f3 --- /dev/null +++ b/programs/utils.s @@ -0,0 +1,136 @@ +; Utility subroutines for SuBAsm. + +.org util_data +; Hex character table. +hex_char: + .byte "0123456789ABCDEF" + +.org utils + +print_hi: + and #0 ; Reset A. + sta idx3 ; Clear the string index. + lda #'$' ; Print the hex delimiter. + jsr charcpy ; + lda.q idx0 ; Get the masked address. + ldx #$10 ; Set digit count to 16. + jsr print_hex ; Print the address. + lda.q hex_str ; Get the lower half of the string. + sta.q strbuf+1 ; Save it in the string buffer. + lda.q hex_str+8 ; Get the upper half of the string. + sta.q strbuf+9 ; Save it in the string buffer. + ldx #$11 ; Add 16 to the index. + stx idx3 ; + lda #':' ; Print a colon. + jsr charcpy ; + lda # ' ' ; Print a space. + jsr charcpy ; + rts ; End of print_hi. + +print_lo: + lda #0 ; Reset A. + sta idx3 ; Clear the string index. +pntlo_lp: + ldx #2 ; Set digit count to 2. + pha #1 ; Preserve the nibble offset. + jsr print_hex ; Print the low nibble offset. + lda.w (ptr3) ; Get the two digits. + jsr charcpy ; Copy the first digit. + lsr #8 ; Copy the next digit. + jsr charcpy ; + pla #1 ; Get the nibble offset back. + inc ; Increment the offset. + cmp #$10 ; Are we at the last offset? + bcs pntlo_end ; Yes, so we're done. +pntlo_lp1: + pha #1 ; No, so preserve the nibble offset. + lda #' ' ; Add a space to the string buffer. + jsr charcpy ; + pla #1 ; Get the nibble offset back. + jmp pntlo_lp ; Keep looping. +pntlo_end: + inx ; Increment the index by one. + lda #0 ; Null terminate the string buffer. + sta strbuf, x ; + tax ; Reset X. + lda.d #strbuf ; Print the string buffer. + jsr print_str ; + rts ; End of print_lo. + +print_chunk: + ldx #0 ; Reset X. + phy #2 ; Preserve the screen buffer index. + txy ; Copy the byte index to it. +pntchnk_lp: + and #0 ; Reset A. + ldx #2 ; Set the digit count to 2. + lda (idx0), y ; Get the byte at that address. + jsr print_hex ; Print the byte. + lda.w (ptr3) ; Get the two digits. + jsr charcpy ; Copy the first digit. + lsr #8 ; Copy the next digit. + jsr charcpy ; + iny ; Increment the byte index. + cpy #$10 ; Have we read 16 bytes? + beq pntchnk_end ; Yes, so we're done. + lda #' ' ; No, so add a soace to the string buffer. + jsr charcpy ; + jmp pntchnk_lp ; Keep looping. +pntchnk_end: + ply #2 ; Get the screen buffer index back. + inx ; Increment the index by one. + and #0 ; Null terminate the string. + sta strbuf, x ; + tax ; Reset X. + sta idx3 ; Clear the string index. + rts ; End of print_chunk. + + +print_hex: + pha #8 ; Preserve the hex value. + and #0 ; Reset A. + ldb #1 ; Set the second pointer + lda.w #hex_char ; to the start of hex character table. + jsr set_ptr ; + inb ; Set the third pointer + lda.d #hex_str ; to the end of hex string buffer. + clc ; Do a non carrying add. + adc #$10 ; + jsr set_ptr ; + ldb #0 ; Reset B. + pla #8 ; Get the hex value back. +pnthex_lp: + pha #8 ; Preserve the hex value. + and #$F ; Mask the lowest nibble. + phy #2 ; Preserve the screen buffer position. + tay ; Get the index for the hex digit. + lda (ptr2), y ; Get the hex digit. + dec ptr3 ; Decrement the string pointer. + sta (ptr3) ; Save the hex digit character in the string. + ply #2 ; Get back the screen buffer position. + pla #8 ; Get the hex value back. +pnthex_lp1: + cpx #1 ; Is the digit count less than one? + bcc pnthex_lp2 ; Yes, so don't decrement the digit count. + dex ; No, but was the digit count zero, when decremented? + beq pnthex_end ; Yes, so we're done. + jmp pnthex_lp3 ; No, so get the next nibble. +pnthex_lp2: + ldb #1 ; Enable auto digit count. +pnthex_lp3: + lsr #4 ; No, but is the next nibble, a zero? + beq pnthex_lp4 ; Yes, so check if auto digit count is enabled. + jmp pnthex_lp ; No, so print the next digit. +pnthex_lp4: + cpb #1 ; Is auto digit count enabled? + beq pnthex_end ; Yes, so we're done. + jmp pnthex_lp ; No, so keep printing more digits. +pnthex_end: + rts ; End of print_hex. + + +charcpy: + ldx idx3 ; Get the string index. + sta strbuf, x ; Save it in the string buffer. + inc idx3 ; Increment the string index. + rts ; End of charcpy. -- cgit v1.2.3-13-gbd6f