; 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. @loop: 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 @end ; Yes, so we're done. @loop1: 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 @loop ; Keep looping. @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. @loop: 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 @end ; Yes, so we're done. lda #' ' ; No, so add a soace to the string buffer. jsr charcpy ; jmp @loop ; Keep looping. @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. @loop: 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. @isauto: cpx #1 ; Is the digit count less than one? bcc @auto ; Yes, so don't decrement the digit count. dex ; No, but was the digit count zero, when decremented? beq @end ; Yes, so we're done. jmp @next ; No, so get the next nibble. @auto: ldb #1 ; Enable auto digit count. @next: lsr #4 ; Is the next nibble, a zero? beq @isauto1 ; Yes, so check if auto digit count is enabled. jmp @loop ; No, so print the next digit. @isauto1: cpb #1 ; Is auto digit count enabled? beq @end ; Yes, so we're done. jmp @loop ; No, so keep printing more digits. @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.